libstdc++
basic_string.h
Go to the documentation of this file.
00001 // Components for manipulating sequences of characters -*- C++ -*-
00002 
00003 // Copyright (C) 1997-2017 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/basic_string.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{string}
00028  */
00029 
00030 //
00031 // ISO C++ 14882: 21 Strings library
00032 //
00033 
00034 #ifndef _BASIC_STRING_H
00035 #define _BASIC_STRING_H 1
00036 
00037 #pragma GCC system_header
00038 
00039 #include <ext/atomicity.h>
00040 #include <ext/alloc_traits.h>
00041 #include <debug/debug.h>
00042 
00043 #if __cplusplus >= 201103L
00044 #include <initializer_list>
00045 #endif
00046 
00047 #if __cplusplus > 201402L
00048 # include <string_view>
00049 #endif
00050 
00051 
00052 namespace std _GLIBCXX_VISIBILITY(default)
00053 {
00054 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00055 
00056 #if _GLIBCXX_USE_CXX11_ABI
00057 _GLIBCXX_BEGIN_NAMESPACE_CXX11
00058   /**
00059    *  @class basic_string basic_string.h <string>
00060    *  @brief  Managing sequences of characters and character-like objects.
00061    *
00062    *  @ingroup strings
00063    *  @ingroup sequences
00064    *
00065    *  @tparam _CharT  Type of character
00066    *  @tparam _Traits  Traits for character type, defaults to
00067    *                   char_traits<_CharT>.
00068    *  @tparam _Alloc  Allocator type, defaults to allocator<_CharT>.
00069    *
00070    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
00071    *  <a href="tables.html#66">reversible container</a>, and a
00072    *  <a href="tables.html#67">sequence</a>.  Of the
00073    *  <a href="tables.html#68">optional sequence requirements</a>, only
00074    *  @c push_back, @c at, and @c %array access are supported.
00075    */
00076   template<typename _CharT, typename _Traits, typename _Alloc>
00077     class basic_string
00078     {
00079       typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
00080         rebind<_CharT>::other _Char_alloc_type;
00081       typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
00082 
00083       // Types:
00084     public:
00085       typedef _Traits                                   traits_type;
00086       typedef typename _Traits::char_type               value_type;
00087       typedef _Char_alloc_type                          allocator_type;
00088       typedef typename _Alloc_traits::size_type         size_type;
00089       typedef typename _Alloc_traits::difference_type   difference_type;
00090       typedef typename _Alloc_traits::reference         reference;
00091       typedef typename _Alloc_traits::const_reference   const_reference;
00092       typedef typename _Alloc_traits::pointer           pointer;
00093       typedef typename _Alloc_traits::const_pointer     const_pointer;
00094       typedef __gnu_cxx::__normal_iterator<pointer, basic_string>  iterator;
00095       typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
00096                                                         const_iterator;
00097       typedef std::reverse_iterator<const_iterator>     const_reverse_iterator;
00098       typedef std::reverse_iterator<iterator>           reverse_iterator;
00099 
00100       ///  Value returned by various member functions when they fail.
00101       static const size_type    npos = static_cast<size_type>(-1);
00102 
00103     private:
00104       // type used for positions in insert, erase etc.
00105 #if __cplusplus < 201103L
00106       typedef iterator __const_iterator;
00107 #else
00108       typedef const_iterator __const_iterator;
00109 #endif
00110 
00111 #if __cplusplus > 201402L
00112       // A helper type for avoiding boiler-plate.
00113       typedef basic_string_view<_CharT, _Traits> __sv_type;
00114 
00115       template<typename _Tp, typename _Res>
00116         using _If_sv = enable_if_t<
00117           __and_<is_convertible<const _Tp&, __sv_type>,
00118                  __not_<is_convertible<const _Tp*, const basic_string*>>,
00119                  __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
00120           _Res>;
00121 
00122       // Allows an implicit conversion to __sv_type.
00123       static __sv_type
00124       _S_to_string_view(__sv_type __svt) noexcept
00125       { return __svt; }
00126 
00127       // Wraps a string_view by explicit conversion and thus
00128       // allows to add an internal constructor that does not
00129       // participate in overload resolution when a string_view
00130       // is provided.
00131       struct __sv_wrapper
00132       {
00133         explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
00134         __sv_type _M_sv;
00135       };
00136 #endif
00137 
00138       // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
00139       struct _Alloc_hider : allocator_type // TODO check __is_final
00140       {
00141 #if __cplusplus < 201103L
00142         _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
00143         : allocator_type(__a), _M_p(__dat) { }
00144 #else
00145         _Alloc_hider(pointer __dat, const _Alloc& __a)
00146         : allocator_type(__a), _M_p(__dat) { }
00147 
00148         _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
00149         : allocator_type(std::move(__a)), _M_p(__dat) { }
00150 #endif
00151 
00152         pointer _M_p; // The actual data.
00153       };
00154 
00155       _Alloc_hider      _M_dataplus;
00156       size_type         _M_string_length;
00157 
00158       enum { _S_local_capacity = 15 / sizeof(_CharT) };
00159 
00160       union
00161       {
00162         _CharT           _M_local_buf[_S_local_capacity + 1];
00163         size_type        _M_allocated_capacity;
00164       };
00165 
00166       void
00167       _M_data(pointer __p)
00168       { _M_dataplus._M_p = __p; }
00169 
00170       void
00171       _M_length(size_type __length)
00172       { _M_string_length = __length; }
00173 
00174       pointer
00175       _M_data() const
00176       { return _M_dataplus._M_p; }
00177 
00178       pointer
00179       _M_local_data()
00180       {
00181 #if __cplusplus >= 201103L
00182         return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
00183 #else
00184         return pointer(_M_local_buf);
00185 #endif
00186       }
00187 
00188       const_pointer
00189       _M_local_data() const
00190       {
00191 #if __cplusplus >= 201103L
00192         return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf);
00193 #else
00194         return const_pointer(_M_local_buf);
00195 #endif
00196       }
00197 
00198       void
00199       _M_capacity(size_type __capacity)
00200       { _M_allocated_capacity = __capacity; }
00201 
00202       void
00203       _M_set_length(size_type __n)
00204       {
00205         _M_length(__n);
00206         traits_type::assign(_M_data()[__n], _CharT());
00207       }
00208 
00209       bool
00210       _M_is_local() const
00211       { return _M_data() == _M_local_data(); }
00212 
00213       // Create & Destroy
00214       pointer
00215       _M_create(size_type&, size_type);
00216 
00217       void
00218       _M_dispose()
00219       {
00220         if (!_M_is_local())
00221           _M_destroy(_M_allocated_capacity);
00222       }
00223 
00224       void
00225       _M_destroy(size_type __size) throw()
00226       { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
00227 
00228       // _M_construct_aux is used to implement the 21.3.1 para 15 which
00229       // requires special behaviour if _InIterator is an integral type
00230       template<typename _InIterator>
00231         void
00232         _M_construct_aux(_InIterator __beg, _InIterator __end,
00233                          std::__false_type)
00234         {
00235           typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
00236           _M_construct(__beg, __end, _Tag());
00237         }
00238 
00239       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00240       // 438. Ambiguity in the "do the right thing" clause
00241       template<typename _Integer>
00242         void
00243         _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
00244         { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
00245 
00246       void
00247       _M_construct_aux_2(size_type __req, _CharT __c)
00248       { _M_construct(__req, __c); }
00249 
00250       template<typename _InIterator>
00251         void
00252         _M_construct(_InIterator __beg, _InIterator __end)
00253         {
00254           typedef typename std::__is_integer<_InIterator>::__type _Integral;
00255           _M_construct_aux(__beg, __end, _Integral());
00256         }
00257 
00258       // For Input Iterators, used in istreambuf_iterators, etc.
00259       template<typename _InIterator>
00260         void
00261         _M_construct(_InIterator __beg, _InIterator __end,
00262                      std::input_iterator_tag);
00263 
00264       // For forward_iterators up to random_access_iterators, used for
00265       // string::iterator, _CharT*, etc.
00266       template<typename _FwdIterator>
00267         void
00268         _M_construct(_FwdIterator __beg, _FwdIterator __end,
00269                      std::forward_iterator_tag);
00270 
00271       void
00272       _M_construct(size_type __req, _CharT __c);
00273 
00274       allocator_type&
00275       _M_get_allocator()
00276       { return _M_dataplus; }
00277 
00278       const allocator_type&
00279       _M_get_allocator() const
00280       { return _M_dataplus; }
00281 
00282     private:
00283 
00284 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
00285       // The explicit instantiations in misc-inst.cc require this due to
00286       // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
00287       template<typename _Tp, bool _Requires =
00288                !__are_same<_Tp, _CharT*>::__value
00289                && !__are_same<_Tp, const _CharT*>::__value
00290                && !__are_same<_Tp, iterator>::__value
00291                && !__are_same<_Tp, const_iterator>::__value>
00292         struct __enable_if_not_native_iterator
00293         { typedef basic_string& __type; };
00294       template<typename _Tp>
00295         struct __enable_if_not_native_iterator<_Tp, false> { };
00296 #endif
00297 
00298       size_type
00299       _M_check(size_type __pos, const char* __s) const
00300       {
00301         if (__pos > this->size())
00302           __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
00303                                        "this->size() (which is %zu)"),
00304                                    __s, __pos, this->size());
00305         return __pos;
00306       }
00307 
00308       void
00309       _M_check_length(size_type __n1, size_type __n2, const char* __s) const
00310       {
00311         if (this->max_size() - (this->size() - __n1) < __n2)
00312           __throw_length_error(__N(__s));
00313       }
00314 
00315 
00316       // NB: _M_limit doesn't check for a bad __pos value.
00317       size_type
00318       _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
00319       {
00320         const bool __testoff =  __off < this->size() - __pos;
00321         return __testoff ? __off : this->size() - __pos;
00322       }
00323 
00324       // True if _Rep and source do not overlap.
00325       bool
00326       _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
00327       {
00328         return (less<const _CharT*>()(__s, _M_data())
00329                 || less<const _CharT*>()(_M_data() + this->size(), __s));
00330       }
00331 
00332       // When __n = 1 way faster than the general multichar
00333       // traits_type::copy/move/assign.
00334       static void
00335       _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
00336       {
00337         if (__n == 1)
00338           traits_type::assign(*__d, *__s);
00339         else
00340           traits_type::copy(__d, __s, __n);
00341       }
00342 
00343       static void
00344       _S_move(_CharT* __d, const _CharT* __s, size_type __n)
00345       {
00346         if (__n == 1)
00347           traits_type::assign(*__d, *__s);
00348         else
00349           traits_type::move(__d, __s, __n);
00350       }
00351 
00352       static void
00353       _S_assign(_CharT* __d, size_type __n, _CharT __c)
00354       {
00355         if (__n == 1)
00356           traits_type::assign(*__d, __c);
00357         else
00358           traits_type::assign(__d, __n, __c);
00359       }
00360 
00361       // _S_copy_chars is a separate template to permit specialization
00362       // to optimize for the common case of pointers as iterators.
00363       template<class _Iterator>
00364         static void
00365         _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
00366         {
00367           for (; __k1 != __k2; ++__k1, (void)++__p)
00368             traits_type::assign(*__p, *__k1); // These types are off.
00369         }
00370 
00371       static void
00372       _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
00373       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
00374 
00375       static void
00376       _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
00377       _GLIBCXX_NOEXCEPT
00378       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
00379 
00380       static void
00381       _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
00382       { _S_copy(__p, __k1, __k2 - __k1); }
00383 
00384       static void
00385       _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
00386       _GLIBCXX_NOEXCEPT
00387       { _S_copy(__p, __k1, __k2 - __k1); }
00388 
00389       static int
00390       _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
00391       {
00392         const difference_type __d = difference_type(__n1 - __n2);
00393 
00394         if (__d > __gnu_cxx::__numeric_traits<int>::__max)
00395           return __gnu_cxx::__numeric_traits<int>::__max;
00396         else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
00397           return __gnu_cxx::__numeric_traits<int>::__min;
00398         else
00399           return int(__d);
00400       }
00401 
00402       void
00403       _M_assign(const basic_string&);
00404 
00405       void
00406       _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
00407                 size_type __len2);
00408 
00409       void
00410       _M_erase(size_type __pos, size_type __n);
00411 
00412     public:
00413       // Construct/copy/destroy:
00414       // NB: We overload ctors in some cases instead of using default
00415       // arguments, per 17.4.4.4 para. 2 item 2.
00416 
00417       /**
00418        *  @brief  Default constructor creates an empty string.
00419        */
00420       basic_string()
00421       _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
00422       : _M_dataplus(_M_local_data())
00423       { _M_set_length(0); }
00424 
00425       /**
00426        *  @brief  Construct an empty string using allocator @a a.
00427        */
00428       explicit
00429       basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
00430       : _M_dataplus(_M_local_data(), __a)
00431       { _M_set_length(0); }
00432 
00433       /**
00434        *  @brief  Construct string with copy of value of @a __str.
00435        *  @param  __str  Source string.
00436        */
00437       basic_string(const basic_string& __str)
00438       : _M_dataplus(_M_local_data(),
00439                     _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
00440       { _M_construct(__str._M_data(), __str._M_data() + __str.length()); }
00441 
00442       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00443       // 2583. no way to supply an allocator for basic_string(str, pos)
00444       /**
00445        *  @brief  Construct string as copy of a substring.
00446        *  @param  __str  Source string.
00447        *  @param  __pos  Index of first character to copy from.
00448        *  @param  __a  Allocator to use.
00449        */
00450       basic_string(const basic_string& __str, size_type __pos,
00451                    const _Alloc& __a = _Alloc())
00452       : _M_dataplus(_M_local_data(), __a)
00453       {
00454         const _CharT* __start = __str._M_data()
00455           + __str._M_check(__pos, "basic_string::basic_string");
00456         _M_construct(__start, __start + __str._M_limit(__pos, npos));
00457       }
00458 
00459       /**
00460        *  @brief  Construct string as copy of a substring.
00461        *  @param  __str  Source string.
00462        *  @param  __pos  Index of first character to copy from.
00463        *  @param  __n  Number of characters to copy.
00464        */
00465       basic_string(const basic_string& __str, size_type __pos,
00466                    size_type __n)
00467       : _M_dataplus(_M_local_data())
00468       {
00469         const _CharT* __start = __str._M_data()
00470           + __str._M_check(__pos, "basic_string::basic_string");
00471         _M_construct(__start, __start + __str._M_limit(__pos, __n));
00472       }
00473 
00474       /**
00475        *  @brief  Construct string as copy of a substring.
00476        *  @param  __str  Source string.
00477        *  @param  __pos  Index of first character to copy from.
00478        *  @param  __n  Number of characters to copy.
00479        *  @param  __a  Allocator to use.
00480        */
00481       basic_string(const basic_string& __str, size_type __pos,
00482                    size_type __n, const _Alloc& __a)
00483       : _M_dataplus(_M_local_data(), __a)
00484       {
00485         const _CharT* __start
00486           = __str._M_data() + __str._M_check(__pos, "string::string");
00487         _M_construct(__start, __start + __str._M_limit(__pos, __n));
00488       }
00489 
00490       /**
00491        *  @brief  Construct string initialized by a character %array.
00492        *  @param  __s  Source character %array.
00493        *  @param  __n  Number of characters to copy.
00494        *  @param  __a  Allocator to use (default is default allocator).
00495        *
00496        *  NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
00497        *  has no special meaning.
00498        */
00499       basic_string(const _CharT* __s, size_type __n,
00500                    const _Alloc& __a = _Alloc())
00501       : _M_dataplus(_M_local_data(), __a)
00502       { _M_construct(__s, __s + __n); }
00503 
00504       /**
00505        *  @brief  Construct string as copy of a C string.
00506        *  @param  __s  Source C string.
00507        *  @param  __a  Allocator to use (default is default allocator).
00508        */
00509       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
00510       : _M_dataplus(_M_local_data(), __a)
00511       { _M_construct(__s, __s ? __s + traits_type::length(__s) : __s+npos); }
00512 
00513       /**
00514        *  @brief  Construct string as multiple characters.
00515        *  @param  __n  Number of characters.
00516        *  @param  __c  Character to use.
00517        *  @param  __a  Allocator to use (default is default allocator).
00518        */
00519       basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
00520       : _M_dataplus(_M_local_data(), __a)
00521       { _M_construct(__n, __c); }
00522 
00523 #if __cplusplus >= 201103L
00524       /**
00525        *  @brief  Move construct string.
00526        *  @param  __str  Source string.
00527        *
00528        *  The newly-created string contains the exact contents of @a __str.
00529        *  @a __str is a valid, but unspecified string.
00530        **/
00531       basic_string(basic_string&& __str) noexcept
00532       : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
00533       {
00534         if (__str._M_is_local())
00535           {
00536             traits_type::copy(_M_local_buf, __str._M_local_buf,
00537                               _S_local_capacity + 1);
00538           }
00539         else
00540           {
00541             _M_data(__str._M_data());
00542             _M_capacity(__str._M_allocated_capacity);
00543           }
00544 
00545         // Must use _M_length() here not _M_set_length() because
00546         // basic_stringbuf relies on writing into unallocated capacity so
00547         // we mess up the contents if we put a '\0' in the string.
00548         _M_length(__str.length());
00549         __str._M_data(__str._M_local_data());
00550         __str._M_set_length(0);
00551       }
00552 
00553       /**
00554        *  @brief  Construct string from an initializer %list.
00555        *  @param  __l  std::initializer_list of characters.
00556        *  @param  __a  Allocator to use (default is default allocator).
00557        */
00558       basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
00559       : _M_dataplus(_M_local_data(), __a)
00560       { _M_construct(__l.begin(), __l.end()); }
00561 
00562       basic_string(const basic_string& __str, const _Alloc& __a)
00563       : _M_dataplus(_M_local_data(), __a)
00564       { _M_construct(__str.begin(), __str.end()); }
00565 
00566       basic_string(basic_string&& __str, const _Alloc& __a)
00567       noexcept(_Alloc_traits::_S_always_equal())
00568       : _M_dataplus(_M_local_data(), __a)
00569       {
00570         if (__str._M_is_local())
00571           {
00572             traits_type::copy(_M_local_buf, __str._M_local_buf,
00573                               _S_local_capacity + 1);
00574             _M_length(__str.length());
00575             __str._M_set_length(0);
00576           }
00577         else if (_Alloc_traits::_S_always_equal()
00578             || __str.get_allocator() == __a)
00579           {
00580             _M_data(__str._M_data());
00581             _M_length(__str.length());
00582             _M_capacity(__str._M_allocated_capacity);
00583             __str._M_data(__str._M_local_buf);
00584             __str._M_set_length(0);
00585           }
00586         else
00587           _M_construct(__str.begin(), __str.end());
00588       }
00589 
00590 #endif // C++11
00591 
00592       /**
00593        *  @brief  Construct string as copy of a range.
00594        *  @param  __beg  Start of range.
00595        *  @param  __end  End of range.
00596        *  @param  __a  Allocator to use (default is default allocator).
00597        */
00598 #if __cplusplus >= 201103L
00599       template<typename _InputIterator,
00600                typename = std::_RequireInputIter<_InputIterator>>
00601 #else
00602       template<typename _InputIterator>
00603 #endif
00604         basic_string(_InputIterator __beg, _InputIterator __end,
00605                      const _Alloc& __a = _Alloc())
00606         : _M_dataplus(_M_local_data(), __a)
00607         { _M_construct(__beg, __end); }
00608 
00609 #if __cplusplus > 201402L
00610       /**
00611        *  @brief  Construct string from a substring of a string_view.
00612        *  @param  __t   Source object convertible to string view.
00613        *  @param  __pos The index of the first character to copy from __t.
00614        *  @param  __n   The number of characters to copy from __t.
00615        *  @param  __a   Allocator to use.
00616        */
00617       template<typename _Tp, typename = _If_sv<_Tp, void>>
00618         basic_string(const _Tp& __t, size_type __pos, size_type __n,
00619                      const _Alloc& __a = _Alloc())
00620         : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
00621 
00622       /**
00623        *  @brief  Construct string from a string_view.
00624        *  @param  __t  Source object convertible to string view.
00625        *  @param  __a  Allocator to use (default is default allocator).
00626        */
00627       template<typename _Tp, typename = _If_sv<_Tp, void>>
00628         explicit
00629         basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
00630         : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
00631 
00632       /**
00633        *  @brief  Only internally used: Construct string from a string view
00634        *          wrapper.
00635        *  @param  __svw  string view wrapper.
00636        *  @param  __a  Allocator to use.
00637        */
00638       explicit
00639       basic_string(__sv_wrapper __svw, const _Alloc& __a)
00640       : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
00641 #endif // C++17
00642 
00643       /**
00644        *  @brief  Destroy the string instance.
00645        */
00646       ~basic_string()
00647       { _M_dispose(); }
00648 
00649       /**
00650        *  @brief  Assign the value of @a str to this string.
00651        *  @param  __str  Source string.
00652        */
00653       basic_string&
00654       operator=(const basic_string& __str)
00655       {
00656 #if __cplusplus >= 201103L
00657         if (_Alloc_traits::_S_propagate_on_copy_assign())
00658           {
00659             if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
00660                 && _M_get_allocator() != __str._M_get_allocator())
00661               {
00662                 // Propagating allocator cannot free existing storage so must
00663                 // deallocate it before replacing current allocator.
00664                 if (__str.size() <= _S_local_capacity)
00665                   {
00666                     _M_destroy(_M_allocated_capacity);
00667                     _M_data(_M_local_data());
00668                     _M_set_length(0);
00669                   }
00670                 else
00671                   {
00672                     const auto __len = __str.size();
00673                     auto __alloc = __str._M_get_allocator();
00674                     // If this allocation throws there are no effects:
00675                     auto __ptr = _Alloc_traits::allocate(__alloc, __len + 1);
00676                     _M_destroy(_M_allocated_capacity);
00677                     _M_data(__ptr);
00678                     _M_capacity(__len);
00679                     _M_set_length(__len);
00680                   }
00681               }
00682             std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
00683           }
00684 #endif
00685         return this->assign(__str);
00686       }
00687 
00688       /**
00689        *  @brief  Copy contents of @a s into this string.
00690        *  @param  __s  Source null-terminated string.
00691        */
00692       basic_string&
00693       operator=(const _CharT* __s)
00694       { return this->assign(__s); }
00695 
00696       /**
00697        *  @brief  Set value to string of length 1.
00698        *  @param  __c  Source character.
00699        *
00700        *  Assigning to a character makes this string length 1 and
00701        *  (*this)[0] == @a c.
00702        */
00703       basic_string&
00704       operator=(_CharT __c)
00705       {
00706         this->assign(1, __c);
00707         return *this;
00708       }
00709 
00710 #if __cplusplus >= 201103L
00711       /**
00712        *  @brief  Move assign the value of @a str to this string.
00713        *  @param  __str  Source string.
00714        *
00715        *  The contents of @a str are moved into this string (without copying).
00716        *  @a str is a valid, but unspecified string.
00717        **/
00718       // PR 58265, this should be noexcept.
00719       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00720       // 2063. Contradictory requirements for string move assignment
00721       basic_string&
00722       operator=(basic_string&& __str)
00723       noexcept(_Alloc_traits::_S_nothrow_move())
00724       {
00725         if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
00726             && !_Alloc_traits::_S_always_equal()
00727             && _M_get_allocator() != __str._M_get_allocator())
00728           {
00729             // Destroy existing storage before replacing allocator.
00730             _M_destroy(_M_allocated_capacity);
00731             _M_data(_M_local_data());
00732             _M_set_length(0);
00733           }
00734         // Replace allocator if POCMA is true.
00735         std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
00736 
00737         if (!__str._M_is_local()
00738             && (_Alloc_traits::_S_propagate_on_move_assign()
00739               || _Alloc_traits::_S_always_equal()))
00740           {
00741             pointer __data = nullptr;
00742             size_type __capacity;
00743             if (!_M_is_local())
00744               {
00745                 if (_Alloc_traits::_S_always_equal())
00746                   {
00747                     __data = _M_data();
00748                     __capacity = _M_allocated_capacity;
00749                   }
00750                 else
00751                   _M_destroy(_M_allocated_capacity);
00752               }
00753 
00754             _M_data(__str._M_data());
00755             _M_length(__str.length());
00756             _M_capacity(__str._M_allocated_capacity);
00757             if (__data)
00758               {
00759                 __str._M_data(__data);
00760                 __str._M_capacity(__capacity);
00761               }
00762             else
00763               __str._M_data(__str._M_local_buf);
00764           }
00765         else
00766             assign(__str);
00767         __str.clear();
00768         return *this;
00769       }
00770 
00771       /**
00772        *  @brief  Set value to string constructed from initializer %list.
00773        *  @param  __l  std::initializer_list.
00774        */
00775       basic_string&
00776       operator=(initializer_list<_CharT> __l)
00777       {
00778         this->assign(__l.begin(), __l.size());
00779         return *this;
00780       }
00781 #endif // C++11
00782 
00783 #if __cplusplus > 201402L
00784       /**
00785        *  @brief  Set value to string constructed from a string_view.
00786        *  @param  __svt  An object convertible to string_view.
00787        */
00788      template<typename _Tp>
00789        _If_sv<_Tp, basic_string&>
00790        operator=(const _Tp& __svt)
00791        { return this->assign(__svt); }
00792 
00793       /**
00794        *  @brief  Convert to a string_view.
00795        *  @return A string_view.
00796        */
00797       operator __sv_type() const noexcept
00798       { return __sv_type(data(), size()); }
00799 #endif // C++17
00800 
00801       // Iterators:
00802       /**
00803        *  Returns a read/write iterator that points to the first character in
00804        *  the %string.
00805        */
00806       iterator
00807       begin() _GLIBCXX_NOEXCEPT
00808       { return iterator(_M_data()); }
00809 
00810       /**
00811        *  Returns a read-only (constant) iterator that points to the first
00812        *  character in the %string.
00813        */
00814       const_iterator
00815       begin() const _GLIBCXX_NOEXCEPT
00816       { return const_iterator(_M_data()); }
00817 
00818       /**
00819        *  Returns a read/write iterator that points one past the last
00820        *  character in the %string.
00821        */
00822       iterator
00823       end() _GLIBCXX_NOEXCEPT
00824       { return iterator(_M_data() + this->size()); }
00825 
00826       /**
00827        *  Returns a read-only (constant) iterator that points one past the
00828        *  last character in the %string.
00829        */
00830       const_iterator
00831       end() const _GLIBCXX_NOEXCEPT
00832       { return const_iterator(_M_data() + this->size()); }
00833 
00834       /**
00835        *  Returns a read/write reverse iterator that points to the last
00836        *  character in the %string.  Iteration is done in reverse element
00837        *  order.
00838        */
00839       reverse_iterator
00840       rbegin() _GLIBCXX_NOEXCEPT
00841       { return reverse_iterator(this->end()); }
00842 
00843       /**
00844        *  Returns a read-only (constant) reverse iterator that points
00845        *  to the last character in the %string.  Iteration is done in
00846        *  reverse element order.
00847        */
00848       const_reverse_iterator
00849       rbegin() const _GLIBCXX_NOEXCEPT
00850       { return const_reverse_iterator(this->end()); }
00851 
00852       /**
00853        *  Returns a read/write reverse iterator that points to one before the
00854        *  first character in the %string.  Iteration is done in reverse
00855        *  element order.
00856        */
00857       reverse_iterator
00858       rend() _GLIBCXX_NOEXCEPT
00859       { return reverse_iterator(this->begin()); }
00860 
00861       /**
00862        *  Returns a read-only (constant) reverse iterator that points
00863        *  to one before the first character in the %string.  Iteration
00864        *  is done in reverse element order.
00865        */
00866       const_reverse_iterator
00867       rend() const _GLIBCXX_NOEXCEPT
00868       { return const_reverse_iterator(this->begin()); }
00869 
00870 #if __cplusplus >= 201103L
00871       /**
00872        *  Returns a read-only (constant) iterator that points to the first
00873        *  character in the %string.
00874        */
00875       const_iterator
00876       cbegin() const noexcept
00877       { return const_iterator(this->_M_data()); }
00878 
00879       /**
00880        *  Returns a read-only (constant) iterator that points one past the
00881        *  last character in the %string.
00882        */
00883       const_iterator
00884       cend() const noexcept
00885       { return const_iterator(this->_M_data() + this->size()); }
00886 
00887       /**
00888        *  Returns a read-only (constant) reverse iterator that points
00889        *  to the last character in the %string.  Iteration is done in
00890        *  reverse element order.
00891        */
00892       const_reverse_iterator
00893       crbegin() const noexcept
00894       { return const_reverse_iterator(this->end()); }
00895 
00896       /**
00897        *  Returns a read-only (constant) reverse iterator that points
00898        *  to one before the first character in the %string.  Iteration
00899        *  is done in reverse element order.
00900        */
00901       const_reverse_iterator
00902       crend() const noexcept
00903       { return const_reverse_iterator(this->begin()); }
00904 #endif
00905 
00906     public:
00907       // Capacity:
00908       ///  Returns the number of characters in the string, not including any
00909       ///  null-termination.
00910       size_type
00911       size() const _GLIBCXX_NOEXCEPT
00912       { return _M_string_length; }
00913 
00914       ///  Returns the number of characters in the string, not including any
00915       ///  null-termination.
00916       size_type
00917       length() const _GLIBCXX_NOEXCEPT
00918       { return _M_string_length; }
00919 
00920       ///  Returns the size() of the largest possible %string.
00921       size_type
00922       max_size() const _GLIBCXX_NOEXCEPT
00923       { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
00924 
00925       /**
00926        *  @brief  Resizes the %string to the specified number of characters.
00927        *  @param  __n  Number of characters the %string should contain.
00928        *  @param  __c  Character to fill any new elements.
00929        *
00930        *  This function will %resize the %string to the specified
00931        *  number of characters.  If the number is smaller than the
00932        *  %string's current size the %string is truncated, otherwise
00933        *  the %string is extended and new elements are %set to @a __c.
00934        */
00935       void
00936       resize(size_type __n, _CharT __c);
00937 
00938       /**
00939        *  @brief  Resizes the %string to the specified number of characters.
00940        *  @param  __n  Number of characters the %string should contain.
00941        *
00942        *  This function will resize the %string to the specified length.  If
00943        *  the new size is smaller than the %string's current size the %string
00944        *  is truncated, otherwise the %string is extended and new characters
00945        *  are default-constructed.  For basic types such as char, this means
00946        *  setting them to 0.
00947        */
00948       void
00949       resize(size_type __n)
00950       { this->resize(__n, _CharT()); }
00951 
00952 #if __cplusplus >= 201103L
00953       ///  A non-binding request to reduce capacity() to size().
00954       void
00955       shrink_to_fit() noexcept
00956       {
00957 #if __cpp_exceptions
00958         if (capacity() > size())
00959           {
00960             try
00961               { reserve(0); }
00962             catch(...)
00963               { }
00964           }
00965 #endif
00966       }
00967 #endif
00968 
00969       /**
00970        *  Returns the total number of characters that the %string can hold
00971        *  before needing to allocate more memory.
00972        */
00973       size_type
00974       capacity() const _GLIBCXX_NOEXCEPT
00975       {
00976         return _M_is_local() ? size_type(_S_local_capacity)
00977                              : _M_allocated_capacity;
00978       }
00979 
00980       /**
00981        *  @brief  Attempt to preallocate enough memory for specified number of
00982        *          characters.
00983        *  @param  __res_arg  Number of characters required.
00984        *  @throw  std::length_error  If @a __res_arg exceeds @c max_size().
00985        *
00986        *  This function attempts to reserve enough memory for the
00987        *  %string to hold the specified number of characters.  If the
00988        *  number requested is more than max_size(), length_error is
00989        *  thrown.
00990        *
00991        *  The advantage of this function is that if optimal code is a
00992        *  necessity and the user can determine the string length that will be
00993        *  required, the user can reserve the memory in %advance, and thus
00994        *  prevent a possible reallocation of memory and copying of %string
00995        *  data.
00996        */
00997       void
00998       reserve(size_type __res_arg = 0);
00999 
01000       /**
01001        *  Erases the string, making it empty.
01002        */
01003       void
01004       clear() _GLIBCXX_NOEXCEPT
01005       { _M_set_length(0); }
01006 
01007       /**
01008        *  Returns true if the %string is empty.  Equivalent to 
01009        *  <code>*this == ""</code>.
01010        */
01011       bool
01012       empty() const _GLIBCXX_NOEXCEPT
01013       { return this->size() == 0; }
01014 
01015       // Element access:
01016       /**
01017        *  @brief  Subscript access to the data contained in the %string.
01018        *  @param  __pos  The index of the character to access.
01019        *  @return  Read-only (constant) reference to the character.
01020        *
01021        *  This operator allows for easy, array-style, data access.
01022        *  Note that data access with this operator is unchecked and
01023        *  out_of_range lookups are not defined. (For checked lookups
01024        *  see at().)
01025        */
01026       const_reference
01027       operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
01028       {
01029         __glibcxx_assert(__pos <= size());
01030         return _M_data()[__pos];
01031       }
01032 
01033       /**
01034        *  @brief  Subscript access to the data contained in the %string.
01035        *  @param  __pos  The index of the character to access.
01036        *  @return  Read/write reference to the character.
01037        *
01038        *  This operator allows for easy, array-style, data access.
01039        *  Note that data access with this operator is unchecked and
01040        *  out_of_range lookups are not defined. (For checked lookups
01041        *  see at().)
01042        */
01043       reference
01044       operator[](size_type __pos)
01045       {
01046         // Allow pos == size() both in C++98 mode, as v3 extension,
01047         // and in C++11 mode.
01048         __glibcxx_assert(__pos <= size());
01049         // In pedantic mode be strict in C++98 mode.
01050         _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
01051         return _M_data()[__pos];
01052       }
01053 
01054       /**
01055        *  @brief  Provides access to the data contained in the %string.
01056        *  @param __n The index of the character to access.
01057        *  @return  Read-only (const) reference to the character.
01058        *  @throw  std::out_of_range  If @a n is an invalid index.
01059        *
01060        *  This function provides for safer data access.  The parameter is
01061        *  first checked that it is in the range of the string.  The function
01062        *  throws out_of_range if the check fails.
01063        */
01064       const_reference
01065       at(size_type __n) const
01066       {
01067         if (__n >= this->size())
01068           __throw_out_of_range_fmt(__N("basic_string::at: __n "
01069                                        "(which is %zu) >= this->size() "
01070                                        "(which is %zu)"),
01071                                    __n, this->size());
01072         return _M_data()[__n];
01073       }
01074 
01075       /**
01076        *  @brief  Provides access to the data contained in the %string.
01077        *  @param __n The index of the character to access.
01078        *  @return  Read/write reference to the character.
01079        *  @throw  std::out_of_range  If @a n is an invalid index.
01080        *
01081        *  This function provides for safer data access.  The parameter is
01082        *  first checked that it is in the range of the string.  The function
01083        *  throws out_of_range if the check fails.
01084        */
01085       reference
01086       at(size_type __n)
01087       {
01088         if (__n >= size())
01089           __throw_out_of_range_fmt(__N("basic_string::at: __n "
01090                                        "(which is %zu) >= this->size() "
01091                                        "(which is %zu)"),
01092                                    __n, this->size());
01093         return _M_data()[__n];
01094       }
01095 
01096 #if __cplusplus >= 201103L
01097       /**
01098        *  Returns a read/write reference to the data at the first
01099        *  element of the %string.
01100        */
01101       reference
01102       front() noexcept
01103       {
01104         __glibcxx_assert(!empty());
01105         return operator[](0);
01106       }
01107 
01108       /**
01109        *  Returns a read-only (constant) reference to the data at the first
01110        *  element of the %string.
01111        */
01112       const_reference
01113       front() const noexcept
01114       {
01115         __glibcxx_assert(!empty());
01116         return operator[](0);
01117       }
01118 
01119       /**
01120        *  Returns a read/write reference to the data at the last
01121        *  element of the %string.
01122        */
01123       reference
01124       back() noexcept
01125       {
01126         __glibcxx_assert(!empty());
01127         return operator[](this->size() - 1);
01128       }
01129 
01130       /**
01131        *  Returns a read-only (constant) reference to the data at the
01132        *  last element of the %string.
01133        */
01134       const_reference
01135       back() const noexcept
01136       {
01137         __glibcxx_assert(!empty());
01138         return operator[](this->size() - 1);
01139       }
01140 #endif
01141 
01142       // Modifiers:
01143       /**
01144        *  @brief  Append a string to this string.
01145        *  @param __str  The string to append.
01146        *  @return  Reference to this string.
01147        */
01148       basic_string&
01149       operator+=(const basic_string& __str)
01150       { return this->append(__str); }
01151 
01152       /**
01153        *  @brief  Append a C string.
01154        *  @param __s  The C string to append.
01155        *  @return  Reference to this string.
01156        */
01157       basic_string&
01158       operator+=(const _CharT* __s)
01159       { return this->append(__s); }
01160 
01161       /**
01162        *  @brief  Append a character.
01163        *  @param __c  The character to append.
01164        *  @return  Reference to this string.
01165        */
01166       basic_string&
01167       operator+=(_CharT __c)
01168       {
01169         this->push_back(__c);
01170         return *this;
01171       }
01172 
01173 #if __cplusplus >= 201103L
01174       /**
01175        *  @brief  Append an initializer_list of characters.
01176        *  @param __l  The initializer_list of characters to be appended.
01177        *  @return  Reference to this string.
01178        */
01179       basic_string&
01180       operator+=(initializer_list<_CharT> __l)
01181       { return this->append(__l.begin(), __l.size()); }
01182 #endif // C++11
01183 
01184 #if __cplusplus > 201402L
01185       /**
01186        *  @brief  Append a string_view.
01187        *  @param __svt  An object convertible to string_view to be appended.
01188        *  @return  Reference to this string.
01189        */
01190       template<typename _Tp>
01191         _If_sv<_Tp, basic_string&>
01192         operator+=(const _Tp& __svt)
01193         { return this->append(__svt); }
01194 #endif // C++17
01195 
01196       /**
01197        *  @brief  Append a string to this string.
01198        *  @param __str  The string to append.
01199        *  @return  Reference to this string.
01200        */
01201       basic_string&
01202       append(const basic_string& __str)
01203       { return _M_append(__str._M_data(), __str.size()); }
01204 
01205       /**
01206        *  @brief  Append a substring.
01207        *  @param __str  The string to append.
01208        *  @param __pos  Index of the first character of str to append.
01209        *  @param __n  The number of characters to append.
01210        *  @return  Reference to this string.
01211        *  @throw  std::out_of_range if @a __pos is not a valid index.
01212        *
01213        *  This function appends @a __n characters from @a __str
01214        *  starting at @a __pos to this string.  If @a __n is is larger
01215        *  than the number of available characters in @a __str, the
01216        *  remainder of @a __str is appended.
01217        */
01218       basic_string&
01219       append(const basic_string& __str, size_type __pos, size_type __n)
01220       { return _M_append(__str._M_data()
01221                          + __str._M_check(__pos, "basic_string::append"),
01222                          __str._M_limit(__pos, __n)); }
01223 
01224       /**
01225        *  @brief  Append a C substring.
01226        *  @param __s  The C string to append.
01227        *  @param __n  The number of characters to append.
01228        *  @return  Reference to this string.
01229        */
01230       basic_string&
01231       append(const _CharT* __s, size_type __n)
01232       {
01233         __glibcxx_requires_string_len(__s, __n);
01234         _M_check_length(size_type(0), __n, "basic_string::append");
01235         return _M_append(__s, __n);
01236       }
01237 
01238       /**
01239        *  @brief  Append a C string.
01240        *  @param __s  The C string to append.
01241        *  @return  Reference to this string.
01242        */
01243       basic_string&
01244       append(const _CharT* __s)
01245       {
01246         __glibcxx_requires_string(__s);
01247         const size_type __n = traits_type::length(__s);
01248         _M_check_length(size_type(0), __n, "basic_string::append");
01249         return _M_append(__s, __n);
01250       }
01251 
01252       /**
01253        *  @brief  Append multiple characters.
01254        *  @param __n  The number of characters to append.
01255        *  @param __c  The character to use.
01256        *  @return  Reference to this string.
01257        *
01258        *  Appends __n copies of __c to this string.
01259        */
01260       basic_string&
01261       append(size_type __n, _CharT __c)
01262       { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
01263 
01264 #if __cplusplus >= 201103L
01265       /**
01266        *  @brief  Append an initializer_list of characters.
01267        *  @param __l  The initializer_list of characters to append.
01268        *  @return  Reference to this string.
01269        */
01270       basic_string&
01271       append(initializer_list<_CharT> __l)
01272       { return this->append(__l.begin(), __l.size()); }
01273 #endif // C++11
01274 
01275       /**
01276        *  @brief  Append a range of characters.
01277        *  @param __first  Iterator referencing the first character to append.
01278        *  @param __last  Iterator marking the end of the range.
01279        *  @return  Reference to this string.
01280        *
01281        *  Appends characters in the range [__first,__last) to this string.
01282        */
01283 #if __cplusplus >= 201103L
01284       template<class _InputIterator,
01285                typename = std::_RequireInputIter<_InputIterator>>
01286 #else
01287       template<class _InputIterator>
01288 #endif
01289         basic_string&
01290         append(_InputIterator __first, _InputIterator __last)
01291         { return this->replace(end(), end(), __first, __last); }
01292 
01293 #if __cplusplus > 201402L
01294       /**
01295        *  @brief  Append a string_view.
01296        *  @param __svt  An object convertible to string_view to be appended.
01297        *  @return  Reference to this string.
01298        */
01299       template<typename _Tp>
01300         _If_sv<_Tp, basic_string&>
01301         append(const _Tp& __svt)
01302         {
01303           __sv_type __sv = __svt;
01304           return this->append(__sv.data(), __sv.size());
01305         }
01306 
01307       /**
01308        *  @brief  Append a range of characters from a string_view.
01309        *  @param __svt  An object convertible to string_view to be appended from.
01310        *  @param __pos The position in the string_view to append from.
01311        *  @param __n   The number of characters to append from the string_view.
01312        *  @return  Reference to this string.
01313        */
01314       template<typename _Tp>
01315         _If_sv<_Tp, basic_string&>
01316         append(const _Tp& __svt, size_type __pos, size_type __n = npos)
01317         {
01318           __sv_type __sv = __svt;
01319           return _M_append(__sv.data()
01320                            + __sv._M_check(__pos, "basic_string::append"),
01321                            __sv._M_limit(__pos, __n));
01322         }
01323 #endif // C++17
01324 
01325       /**
01326        *  @brief  Append a single character.
01327        *  @param __c  Character to append.
01328        */
01329       void
01330       push_back(_CharT __c)
01331       {
01332         const size_type __size = this->size();
01333         if (__size + 1 > this->capacity())
01334           this->_M_mutate(__size, size_type(0), 0, size_type(1));
01335         traits_type::assign(this->_M_data()[__size], __c);
01336         this->_M_set_length(__size + 1);
01337       }
01338 
01339       /**
01340        *  @brief  Set value to contents of another string.
01341        *  @param  __str  Source string to use.
01342        *  @return  Reference to this string.
01343        */
01344       basic_string&
01345       assign(const basic_string& __str)
01346       {
01347         this->_M_assign(__str);
01348         return *this;
01349       }
01350 
01351 #if __cplusplus >= 201103L
01352       /**
01353        *  @brief  Set value to contents of another string.
01354        *  @param  __str  Source string to use.
01355        *  @return  Reference to this string.
01356        *
01357        *  This function sets this string to the exact contents of @a __str.
01358        *  @a __str is a valid, but unspecified string.
01359        */
01360       basic_string&
01361       assign(basic_string&& __str)
01362       noexcept(_Alloc_traits::_S_nothrow_move())
01363       {
01364         // _GLIBCXX_RESOLVE_LIB_DEFECTS
01365         // 2063. Contradictory requirements for string move assignment
01366         return *this = std::move(__str);
01367       }
01368 #endif // C++11
01369 
01370       /**
01371        *  @brief  Set value to a substring of a string.
01372        *  @param __str  The string to use.
01373        *  @param __pos  Index of the first character of str.
01374        *  @param __n  Number of characters to use.
01375        *  @return  Reference to this string.
01376        *  @throw  std::out_of_range if @a pos is not a valid index.
01377        *
01378        *  This function sets this string to the substring of @a __str
01379        *  consisting of @a __n characters at @a __pos.  If @a __n is
01380        *  is larger than the number of available characters in @a
01381        *  __str, the remainder of @a __str is used.
01382        */
01383       basic_string&
01384       assign(const basic_string& __str, size_type __pos, size_type __n)
01385       { return _M_replace(size_type(0), this->size(), __str._M_data()
01386                           + __str._M_check(__pos, "basic_string::assign"),
01387                           __str._M_limit(__pos, __n)); }
01388 
01389       /**
01390        *  @brief  Set value to a C substring.
01391        *  @param __s  The C string to use.
01392        *  @param __n  Number of characters to use.
01393        *  @return  Reference to this string.
01394        *
01395        *  This function sets the value of this string to the first @a __n
01396        *  characters of @a __s.  If @a __n is is larger than the number of
01397        *  available characters in @a __s, the remainder of @a __s is used.
01398        */
01399       basic_string&
01400       assign(const _CharT* __s, size_type __n)
01401       {
01402         __glibcxx_requires_string_len(__s, __n);
01403         return _M_replace(size_type(0), this->size(), __s, __n);
01404       }
01405 
01406       /**
01407        *  @brief  Set value to contents of a C string.
01408        *  @param __s  The C string to use.
01409        *  @return  Reference to this string.
01410        *
01411        *  This function sets the value of this string to the value of @a __s.
01412        *  The data is copied, so there is no dependence on @a __s once the
01413        *  function returns.
01414        */
01415       basic_string&
01416       assign(const _CharT* __s)
01417       {
01418         __glibcxx_requires_string(__s);
01419         return _M_replace(size_type(0), this->size(), __s,
01420                           traits_type::length(__s));
01421       }
01422 
01423       /**
01424        *  @brief  Set value to multiple characters.
01425        *  @param __n  Length of the resulting string.
01426        *  @param __c  The character to use.
01427        *  @return  Reference to this string.
01428        *
01429        *  This function sets the value of this string to @a __n copies of
01430        *  character @a __c.
01431        */
01432       basic_string&
01433       assign(size_type __n, _CharT __c)
01434       { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
01435 
01436       /**
01437        *  @brief  Set value to a range of characters.
01438        *  @param __first  Iterator referencing the first character to append.
01439        *  @param __last  Iterator marking the end of the range.
01440        *  @return  Reference to this string.
01441        *
01442        *  Sets value of string to characters in the range [__first,__last).
01443       */
01444 #if __cplusplus >= 201103L
01445       template<class _InputIterator,
01446                typename = std::_RequireInputIter<_InputIterator>>
01447 #else
01448       template<class _InputIterator>
01449 #endif
01450         basic_string&
01451         assign(_InputIterator __first, _InputIterator __last)
01452         { return this->replace(begin(), end(), __first, __last); }
01453 
01454 #if __cplusplus >= 201103L
01455       /**
01456        *  @brief  Set value to an initializer_list of characters.
01457        *  @param __l  The initializer_list of characters to assign.
01458        *  @return  Reference to this string.
01459        */
01460       basic_string&
01461       assign(initializer_list<_CharT> __l)
01462       { return this->assign(__l.begin(), __l.size()); }
01463 #endif // C++11
01464 
01465 #if __cplusplus > 201402L
01466       /**
01467        *  @brief  Set value from a string_view.
01468        *  @param __svt  The source object convertible to string_view.
01469        *  @return  Reference to this string.
01470        */
01471       template<typename _Tp>
01472         _If_sv<_Tp, basic_string&>
01473         assign(const _Tp& __svt)
01474         {
01475           __sv_type __sv = __svt;
01476           return this->assign(__sv.data(), __sv.size());
01477         }
01478 
01479       /**
01480        *  @brief  Set value from a range of characters in a string_view.
01481        *  @param __svt  The source object convertible to string_view.
01482        *  @param __pos  The position in the string_view to assign from.
01483        *  @param __n  The number of characters to assign.
01484        *  @return  Reference to this string.
01485        */
01486       template<typename _Tp>
01487         _If_sv<_Tp, basic_string&>
01488         assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
01489         {
01490           __sv_type __sv = __svt;
01491           return _M_replace(size_type(0), this->size(), __sv.data()
01492                             + __sv._M_check(__pos, "basic_string::assign"),
01493                             __sv._M_limit(__pos, __n));
01494         }
01495 #endif // C++17
01496 
01497 #if __cplusplus >= 201103L
01498       /**
01499        *  @brief  Insert multiple characters.
01500        *  @param __p  Const_iterator referencing location in string to
01501        *              insert at.
01502        *  @param __n  Number of characters to insert
01503        *  @param __c  The character to insert.
01504        *  @return  Iterator referencing the first inserted char.
01505        *  @throw  std::length_error  If new length exceeds @c max_size().
01506        *
01507        *  Inserts @a __n copies of character @a __c starting at the
01508        *  position referenced by iterator @a __p.  If adding
01509        *  characters causes the length to exceed max_size(),
01510        *  length_error is thrown.  The value of the string doesn't
01511        *  change if an error is thrown.
01512       */
01513       iterator
01514       insert(const_iterator __p, size_type __n, _CharT __c)
01515       {
01516         _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
01517         const size_type __pos = __p - begin();
01518         this->replace(__p, __p, __n, __c);
01519         return iterator(this->_M_data() + __pos);
01520       }
01521 #else
01522       /**
01523        *  @brief  Insert multiple characters.
01524        *  @param __p  Iterator referencing location in string to insert at.
01525        *  @param __n  Number of characters to insert
01526        *  @param __c  The character to insert.
01527        *  @throw  std::length_error  If new length exceeds @c max_size().
01528        *
01529        *  Inserts @a __n copies of character @a __c starting at the
01530        *  position referenced by iterator @a __p.  If adding
01531        *  characters causes the length to exceed max_size(),
01532        *  length_error is thrown.  The value of the string doesn't
01533        *  change if an error is thrown.
01534       */
01535       void
01536       insert(iterator __p, size_type __n, _CharT __c)
01537       { this->replace(__p, __p, __n, __c);  }
01538 #endif
01539 
01540 #if __cplusplus >= 201103L
01541       /**
01542        *  @brief  Insert a range of characters.
01543        *  @param __p  Const_iterator referencing location in string to
01544        *              insert at.
01545        *  @param __beg  Start of range.
01546        *  @param __end  End of range.
01547        *  @return  Iterator referencing the first inserted char.
01548        *  @throw  std::length_error  If new length exceeds @c max_size().
01549        *
01550        *  Inserts characters in range [beg,end).  If adding characters
01551        *  causes the length to exceed max_size(), length_error is
01552        *  thrown.  The value of the string doesn't change if an error
01553        *  is thrown.
01554       */
01555       template<class _InputIterator,
01556                typename = std::_RequireInputIter<_InputIterator>>
01557         iterator
01558         insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
01559         {
01560           _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
01561           const size_type __pos = __p - begin();
01562           this->replace(__p, __p, __beg, __end);
01563           return iterator(this->_M_data() + __pos);
01564         }
01565 #else
01566       /**
01567        *  @brief  Insert a range of characters.
01568        *  @param __p  Iterator referencing location in string to insert at.
01569        *  @param __beg  Start of range.
01570        *  @param __end  End of range.
01571        *  @throw  std::length_error  If new length exceeds @c max_size().
01572        *
01573        *  Inserts characters in range [__beg,__end).  If adding
01574        *  characters causes the length to exceed max_size(),
01575        *  length_error is thrown.  The value of the string doesn't
01576        *  change if an error is thrown.
01577       */
01578       template<class _InputIterator>
01579         void
01580         insert(iterator __p, _InputIterator __beg, _InputIterator __end)
01581         { this->replace(__p, __p, __beg, __end); }
01582 #endif
01583 
01584 #if __cplusplus >= 201103L
01585       /**
01586        *  @brief  Insert an initializer_list of characters.
01587        *  @param __p  Iterator referencing location in string to insert at.
01588        *  @param __l  The initializer_list of characters to insert.
01589        *  @throw  std::length_error  If new length exceeds @c max_size().
01590        */
01591       void
01592       insert(iterator __p, initializer_list<_CharT> __l)
01593       {
01594         _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
01595         this->insert(__p - begin(), __l.begin(), __l.size());
01596       }
01597 #endif // C++11
01598 
01599       /**
01600        *  @brief  Insert value of a string.
01601        *  @param __pos1  Iterator referencing location in string to insert at.
01602        *  @param __str  The string to insert.
01603        *  @return  Reference to this string.
01604        *  @throw  std::length_error  If new length exceeds @c max_size().
01605        *
01606        *  Inserts value of @a __str starting at @a __pos1.  If adding
01607        *  characters causes the length to exceed max_size(),
01608        *  length_error is thrown.  The value of the string doesn't
01609        *  change if an error is thrown.
01610       */
01611       basic_string&
01612       insert(size_type __pos1, const basic_string& __str)
01613       { return this->replace(__pos1, size_type(0),
01614                              __str._M_data(), __str.size()); }
01615 
01616       /**
01617        *  @brief  Insert a substring.
01618        *  @param __pos1  Iterator referencing location in string to insert at.
01619        *  @param __str  The string to insert.
01620        *  @param __pos2  Start of characters in str to insert.
01621        *  @param __n  Number of characters to insert.
01622        *  @return  Reference to this string.
01623        *  @throw  std::length_error  If new length exceeds @c max_size().
01624        *  @throw  std::out_of_range  If @a pos1 > size() or
01625        *  @a __pos2 > @a str.size().
01626        *
01627        *  Starting at @a pos1, insert @a __n character of @a __str
01628        *  beginning with @a __pos2.  If adding characters causes the
01629        *  length to exceed max_size(), length_error is thrown.  If @a
01630        *  __pos1 is beyond the end of this string or @a __pos2 is
01631        *  beyond the end of @a __str, out_of_range is thrown.  The
01632        *  value of the string doesn't change if an error is thrown.
01633       */
01634       basic_string&
01635       insert(size_type __pos1, const basic_string& __str,
01636              size_type __pos2, size_type __n)
01637       { return this->replace(__pos1, size_type(0), __str._M_data()
01638                              + __str._M_check(__pos2, "basic_string::insert"),
01639                              __str._M_limit(__pos2, __n)); }
01640 
01641       /**
01642        *  @brief  Insert a C substring.
01643        *  @param __pos  Iterator referencing location in string to insert at.
01644        *  @param __s  The C string to insert.
01645        *  @param __n  The number of characters to insert.
01646        *  @return  Reference to this string.
01647        *  @throw  std::length_error  If new length exceeds @c max_size().
01648        *  @throw  std::out_of_range  If @a __pos is beyond the end of this
01649        *  string.
01650        *
01651        *  Inserts the first @a __n characters of @a __s starting at @a
01652        *  __pos.  If adding characters causes the length to exceed
01653        *  max_size(), length_error is thrown.  If @a __pos is beyond
01654        *  end(), out_of_range is thrown.  The value of the string
01655        *  doesn't change if an error is thrown.
01656       */
01657       basic_string&
01658       insert(size_type __pos, const _CharT* __s, size_type __n)
01659       { return this->replace(__pos, size_type(0), __s, __n); }
01660 
01661       /**
01662        *  @brief  Insert a C string.
01663        *  @param __pos  Iterator referencing location in string to insert at.
01664        *  @param __s  The C string to insert.
01665        *  @return  Reference to this string.
01666        *  @throw  std::length_error  If new length exceeds @c max_size().
01667        *  @throw  std::out_of_range  If @a pos is beyond the end of this
01668        *  string.
01669        *
01670        *  Inserts the first @a n characters of @a __s starting at @a __pos.  If
01671        *  adding characters causes the length to exceed max_size(),
01672        *  length_error is thrown.  If @a __pos is beyond end(), out_of_range is
01673        *  thrown.  The value of the string doesn't change if an error is
01674        *  thrown.
01675       */
01676       basic_string&
01677       insert(size_type __pos, const _CharT* __s)
01678       {
01679         __glibcxx_requires_string(__s);
01680         return this->replace(__pos, size_type(0), __s,
01681                              traits_type::length(__s));
01682       }
01683 
01684       /**
01685        *  @brief  Insert multiple characters.
01686        *  @param __pos  Index in string to insert at.
01687        *  @param __n  Number of characters to insert
01688        *  @param __c  The character to insert.
01689        *  @return  Reference to this string.
01690        *  @throw  std::length_error  If new length exceeds @c max_size().
01691        *  @throw  std::out_of_range  If @a __pos is beyond the end of this
01692        *  string.
01693        *
01694        *  Inserts @a __n copies of character @a __c starting at index
01695        *  @a __pos.  If adding characters causes the length to exceed
01696        *  max_size(), length_error is thrown.  If @a __pos > length(),
01697        *  out_of_range is thrown.  The value of the string doesn't
01698        *  change if an error is thrown.
01699       */
01700       basic_string&
01701       insert(size_type __pos, size_type __n, _CharT __c)
01702       { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
01703                               size_type(0), __n, __c); }
01704 
01705       /**
01706        *  @brief  Insert one character.
01707        *  @param __p  Iterator referencing position in string to insert at.
01708        *  @param __c  The character to insert.
01709        *  @return  Iterator referencing newly inserted char.
01710        *  @throw  std::length_error  If new length exceeds @c max_size().
01711        *
01712        *  Inserts character @a __c at position referenced by @a __p.
01713        *  If adding character causes the length to exceed max_size(),
01714        *  length_error is thrown.  If @a __p is beyond end of string,
01715        *  out_of_range is thrown.  The value of the string doesn't
01716        *  change if an error is thrown.
01717       */
01718       iterator
01719       insert(__const_iterator __p, _CharT __c)
01720       {
01721         _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
01722         const size_type __pos = __p - begin();
01723         _M_replace_aux(__pos, size_type(0), size_type(1), __c);
01724         return iterator(_M_data() + __pos);
01725       }
01726 
01727 #if __cplusplus > 201402L
01728       /**
01729        *  @brief  Insert a string_view.
01730        *  @param __pos  Iterator referencing position in string to insert at.
01731        *  @param __svt  The object convertible to string_view to insert.
01732        *  @return  Reference to this string.
01733       */
01734       template<typename _Tp>
01735         _If_sv<_Tp, basic_string&>
01736         insert(size_type __pos, const _Tp& __svt)
01737         {
01738           __sv_type __sv = __svt;
01739           return this->insert(__pos, __sv.data(), __sv.size());
01740         }
01741 
01742       /**
01743        *  @brief  Insert a string_view.
01744        *  @param __pos  Iterator referencing position in string to insert at.
01745        *  @param __svt  The object convertible to string_view to insert from.
01746        *  @param __pos  Iterator referencing position in string_view to insert
01747        *  from.
01748        *  @param __n    The number of characters to insert.
01749        *  @return  Reference to this string.
01750       */
01751       template<typename _Tp>
01752         _If_sv<_Tp, basic_string&>
01753         insert(size_type __pos1, const _Tp& __svt,
01754                size_type __pos2, size_type __n = npos)
01755         {
01756           __sv_type __sv = __svt;
01757           return this->replace(__pos1, size_type(0), __sv.data()
01758                                + __sv._M_check(__pos2, "basic_string::insert"),
01759                                __sv._M_limit(__pos2, __n));
01760         }
01761 #endif // C++17
01762 
01763       /**
01764        *  @brief  Remove characters.
01765        *  @param __pos  Index of first character to remove (default 0).
01766        *  @param __n  Number of characters to remove (default remainder).
01767        *  @return  Reference to this string.
01768        *  @throw  std::out_of_range  If @a pos is beyond the end of this
01769        *  string.
01770        *
01771        *  Removes @a __n characters from this string starting at @a
01772        *  __pos.  The length of the string is reduced by @a __n.  If
01773        *  there are < @a __n characters to remove, the remainder of
01774        *  the string is truncated.  If @a __p is beyond end of string,
01775        *  out_of_range is thrown.  The value of the string doesn't
01776        *  change if an error is thrown.
01777       */
01778       basic_string&
01779       erase(size_type __pos = 0, size_type __n = npos)
01780       {
01781         _M_check(__pos, "basic_string::erase");
01782         if (__n == npos)
01783           this->_M_set_length(__pos);
01784         else if (__n != 0)
01785           this->_M_erase(__pos, _M_limit(__pos, __n));
01786         return *this;
01787       }
01788 
01789       /**
01790        *  @brief  Remove one character.
01791        *  @param __position  Iterator referencing the character to remove.
01792        *  @return  iterator referencing same location after removal.
01793        *
01794        *  Removes the character at @a __position from this string. The value
01795        *  of the string doesn't change if an error is thrown.
01796       */
01797       iterator
01798       erase(__const_iterator __position)
01799       {
01800         _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
01801                                  && __position < end());
01802         const size_type __pos = __position - begin();
01803         this->_M_erase(__pos, size_type(1));
01804         return iterator(_M_data() + __pos);
01805       }
01806 
01807       /**
01808        *  @brief  Remove a range of characters.
01809        *  @param __first  Iterator referencing the first character to remove.
01810        *  @param __last  Iterator referencing the end of the range.
01811        *  @return  Iterator referencing location of first after removal.
01812        *
01813        *  Removes the characters in the range [first,last) from this string.
01814        *  The value of the string doesn't change if an error is thrown.
01815       */
01816       iterator
01817       erase(__const_iterator __first, __const_iterator __last)
01818       {
01819         _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
01820                                  && __last <= end());
01821         const size_type __pos = __first - begin();
01822         if (__last == end())
01823           this->_M_set_length(__pos);
01824         else
01825           this->_M_erase(__pos, __last - __first);
01826         return iterator(this->_M_data() + __pos);
01827       }
01828 
01829 #if __cplusplus >= 201103L
01830       /**
01831        *  @brief  Remove the last character.
01832        *
01833        *  The string must be non-empty.
01834        */
01835       void
01836       pop_back() noexcept
01837       {
01838         __glibcxx_assert(!empty());
01839         _M_erase(size() - 1, 1);
01840       }
01841 #endif // C++11
01842 
01843       /**
01844        *  @brief  Replace characters with value from another string.
01845        *  @param __pos  Index of first character to replace.
01846        *  @param __n  Number of characters to be replaced.
01847        *  @param __str  String to insert.
01848        *  @return  Reference to this string.
01849        *  @throw  std::out_of_range  If @a pos is beyond the end of this
01850        *  string.
01851        *  @throw  std::length_error  If new length exceeds @c max_size().
01852        *
01853        *  Removes the characters in the range [__pos,__pos+__n) from
01854        *  this string.  In place, the value of @a __str is inserted.
01855        *  If @a __pos is beyond end of string, out_of_range is thrown.
01856        *  If the length of the result exceeds max_size(), length_error
01857        *  is thrown.  The value of the string doesn't change if an
01858        *  error is thrown.
01859       */
01860       basic_string&
01861       replace(size_type __pos, size_type __n, const basic_string& __str)
01862       { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
01863 
01864       /**
01865        *  @brief  Replace characters with value from another string.
01866        *  @param __pos1  Index of first character to replace.
01867        *  @param __n1  Number of characters to be replaced.
01868        *  @param __str  String to insert.
01869        *  @param __pos2  Index of first character of str to use.
01870        *  @param __n2  Number of characters from str to use.
01871        *  @return  Reference to this string.
01872        *  @throw  std::out_of_range  If @a __pos1 > size() or @a __pos2 >
01873        *  __str.size().
01874        *  @throw  std::length_error  If new length exceeds @c max_size().
01875        *
01876        *  Removes the characters in the range [__pos1,__pos1 + n) from this
01877        *  string.  In place, the value of @a __str is inserted.  If @a __pos is
01878        *  beyond end of string, out_of_range is thrown.  If the length of the
01879        *  result exceeds max_size(), length_error is thrown.  The value of the
01880        *  string doesn't change if an error is thrown.
01881       */
01882       basic_string&
01883       replace(size_type __pos1, size_type __n1, const basic_string& __str,
01884               size_type __pos2, size_type __n2)
01885       { return this->replace(__pos1, __n1, __str._M_data()
01886                              + __str._M_check(__pos2, "basic_string::replace"),
01887                              __str._M_limit(__pos2, __n2)); }
01888 
01889       /**
01890        *  @brief  Replace characters with value of a C substring.
01891        *  @param __pos  Index of first character to replace.
01892        *  @param __n1  Number of characters to be replaced.
01893        *  @param __s  C string to insert.
01894        *  @param __n2  Number of characters from @a s to use.
01895        *  @return  Reference to this string.
01896        *  @throw  std::out_of_range  If @a pos1 > size().
01897        *  @throw  std::length_error  If new length exceeds @c max_size().
01898        *
01899        *  Removes the characters in the range [__pos,__pos + __n1)
01900        *  from this string.  In place, the first @a __n2 characters of
01901        *  @a __s are inserted, or all of @a __s if @a __n2 is too large.  If
01902        *  @a __pos is beyond end of string, out_of_range is thrown.  If
01903        *  the length of result exceeds max_size(), length_error is
01904        *  thrown.  The value of the string doesn't change if an error
01905        *  is thrown.
01906       */
01907       basic_string&
01908       replace(size_type __pos, size_type __n1, const _CharT* __s,
01909               size_type __n2)
01910       {
01911         __glibcxx_requires_string_len(__s, __n2);
01912         return _M_replace(_M_check(__pos, "basic_string::replace"),
01913                           _M_limit(__pos, __n1), __s, __n2);
01914       }
01915 
01916       /**
01917        *  @brief  Replace characters with value of a C string.
01918        *  @param __pos  Index of first character to replace.
01919        *  @param __n1  Number of characters to be replaced.
01920        *  @param __s  C string to insert.
01921        *  @return  Reference to this string.
01922        *  @throw  std::out_of_range  If @a pos > size().
01923        *  @throw  std::length_error  If new length exceeds @c max_size().
01924        *
01925        *  Removes the characters in the range [__pos,__pos + __n1)
01926        *  from this string.  In place, the characters of @a __s are
01927        *  inserted.  If @a __pos is beyond end of string, out_of_range
01928        *  is thrown.  If the length of result exceeds max_size(),
01929        *  length_error is thrown.  The value of the string doesn't
01930        *  change if an error is thrown.
01931       */
01932       basic_string&
01933       replace(size_type __pos, size_type __n1, const _CharT* __s)
01934       {
01935         __glibcxx_requires_string(__s);
01936         return this->replace(__pos, __n1, __s, traits_type::length(__s));
01937       }
01938 
01939       /**
01940        *  @brief  Replace characters with multiple characters.
01941        *  @param __pos  Index of first character to replace.
01942        *  @param __n1  Number of characters to be replaced.
01943        *  @param __n2  Number of characters to insert.
01944        *  @param __c  Character to insert.
01945        *  @return  Reference to this string.
01946        *  @throw  std::out_of_range  If @a __pos > size().
01947        *  @throw  std::length_error  If new length exceeds @c max_size().
01948        *
01949        *  Removes the characters in the range [pos,pos + n1) from this
01950        *  string.  In place, @a __n2 copies of @a __c are inserted.
01951        *  If @a __pos is beyond end of string, out_of_range is thrown.
01952        *  If the length of result exceeds max_size(), length_error is
01953        *  thrown.  The value of the string doesn't change if an error
01954        *  is thrown.
01955       */
01956       basic_string&
01957       replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
01958       { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
01959                               _M_limit(__pos, __n1), __n2, __c); }
01960 
01961       /**
01962        *  @brief  Replace range of characters with string.
01963        *  @param __i1  Iterator referencing start of range to replace.
01964        *  @param __i2  Iterator referencing end of range to replace.
01965        *  @param __str  String value to insert.
01966        *  @return  Reference to this string.
01967        *  @throw  std::length_error  If new length exceeds @c max_size().
01968        *
01969        *  Removes the characters in the range [__i1,__i2).  In place,
01970        *  the value of @a __str is inserted.  If the length of result
01971        *  exceeds max_size(), length_error is thrown.  The value of
01972        *  the string doesn't change if an error is thrown.
01973       */
01974       basic_string&
01975       replace(__const_iterator __i1, __const_iterator __i2,
01976               const basic_string& __str)
01977       { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
01978 
01979       /**
01980        *  @brief  Replace range of characters with C substring.
01981        *  @param __i1  Iterator referencing start of range to replace.
01982        *  @param __i2  Iterator referencing end of range to replace.
01983        *  @param __s  C string value to insert.
01984        *  @param __n  Number of characters from s to insert.
01985        *  @return  Reference to this string.
01986        *  @throw  std::length_error  If new length exceeds @c max_size().
01987        *
01988        *  Removes the characters in the range [__i1,__i2).  In place,
01989        *  the first @a __n characters of @a __s are inserted.  If the
01990        *  length of result exceeds max_size(), length_error is thrown.
01991        *  The value of the string doesn't change if an error is
01992        *  thrown.
01993       */
01994       basic_string&
01995       replace(__const_iterator __i1, __const_iterator __i2,
01996               const _CharT* __s, size_type __n)
01997       {
01998         _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
01999                                  && __i2 <= end());
02000         return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
02001       }
02002 
02003       /**
02004        *  @brief  Replace range of characters with C string.
02005        *  @param __i1  Iterator referencing start of range to replace.
02006        *  @param __i2  Iterator referencing end of range to replace.
02007        *  @param __s  C string value to insert.
02008        *  @return  Reference to this string.
02009        *  @throw  std::length_error  If new length exceeds @c max_size().
02010        *
02011        *  Removes the characters in the range [__i1,__i2).  In place,
02012        *  the characters of @a __s are inserted.  If the length of
02013        *  result exceeds max_size(), length_error is thrown.  The
02014        *  value of the string doesn't change if an error is thrown.
02015       */
02016       basic_string&
02017       replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
02018       {
02019         __glibcxx_requires_string(__s);
02020         return this->replace(__i1, __i2, __s, traits_type::length(__s));
02021       }
02022 
02023       /**
02024        *  @brief  Replace range of characters with multiple characters
02025        *  @param __i1  Iterator referencing start of range to replace.
02026        *  @param __i2  Iterator referencing end of range to replace.
02027        *  @param __n  Number of characters to insert.
02028        *  @param __c  Character to insert.
02029        *  @return  Reference to this string.
02030        *  @throw  std::length_error  If new length exceeds @c max_size().
02031        *
02032        *  Removes the characters in the range [__i1,__i2).  In place,
02033        *  @a __n copies of @a __c are inserted.  If the length of
02034        *  result exceeds max_size(), length_error is thrown.  The
02035        *  value of the string doesn't change if an error is thrown.
02036       */
02037       basic_string&
02038       replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
02039               _CharT __c)
02040       {
02041         _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
02042                                  && __i2 <= end());
02043         return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
02044       }
02045 
02046       /**
02047        *  @brief  Replace range of characters with range.
02048        *  @param __i1  Iterator referencing start of range to replace.
02049        *  @param __i2  Iterator referencing end of range to replace.
02050        *  @param __k1  Iterator referencing start of range to insert.
02051        *  @param __k2  Iterator referencing end of range to insert.
02052        *  @return  Reference to this string.
02053        *  @throw  std::length_error  If new length exceeds @c max_size().
02054        *
02055        *  Removes the characters in the range [__i1,__i2).  In place,
02056        *  characters in the range [__k1,__k2) are inserted.  If the
02057        *  length of result exceeds max_size(), length_error is thrown.
02058        *  The value of the string doesn't change if an error is
02059        *  thrown.
02060       */
02061 #if __cplusplus >= 201103L
02062       template<class _InputIterator,
02063                typename = std::_RequireInputIter<_InputIterator>>
02064         basic_string&
02065         replace(const_iterator __i1, const_iterator __i2,
02066                 _InputIterator __k1, _InputIterator __k2)
02067         {
02068           _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
02069                                    && __i2 <= end());
02070           __glibcxx_requires_valid_range(__k1, __k2);
02071           return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
02072                                            std::__false_type());
02073         }
02074 #else
02075       template<class _InputIterator>
02076 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
02077         typename __enable_if_not_native_iterator<_InputIterator>::__type
02078 #else
02079         basic_string&
02080 #endif
02081         replace(iterator __i1, iterator __i2,
02082                 _InputIterator __k1, _InputIterator __k2)
02083         {
02084           _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
02085                                    && __i2 <= end());
02086           __glibcxx_requires_valid_range(__k1, __k2);
02087           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
02088           return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
02089         }
02090 #endif
02091 
02092       // Specializations for the common case of pointer and iterator:
02093       // useful to avoid the overhead of temporary buffering in _M_replace.
02094       basic_string&
02095       replace(__const_iterator __i1, __const_iterator __i2,
02096               _CharT* __k1, _CharT* __k2)
02097       {
02098         _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
02099                                  && __i2 <= end());
02100         __glibcxx_requires_valid_range(__k1, __k2);
02101         return this->replace(__i1 - begin(), __i2 - __i1,
02102                              __k1, __k2 - __k1);
02103       }
02104 
02105       basic_string&
02106       replace(__const_iterator __i1, __const_iterator __i2,
02107               const _CharT* __k1, const _CharT* __k2)
02108       {
02109         _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
02110                                  && __i2 <= end());
02111         __glibcxx_requires_valid_range(__k1, __k2);
02112         return this->replace(__i1 - begin(), __i2 - __i1,
02113                              __k1, __k2 - __k1);
02114       }
02115 
02116       basic_string&
02117       replace(__const_iterator __i1, __const_iterator __i2,
02118               iterator __k1, iterator __k2)
02119       {
02120         _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
02121                                  && __i2 <= end());
02122         __glibcxx_requires_valid_range(__k1, __k2);
02123         return this->replace(__i1 - begin(), __i2 - __i1,
02124                              __k1.base(), __k2 - __k1);
02125       }
02126 
02127       basic_string&
02128       replace(__const_iterator __i1, __const_iterator __i2,
02129               const_iterator __k1, const_iterator __k2)
02130       {
02131         _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
02132                                  && __i2 <= end());
02133         __glibcxx_requires_valid_range(__k1, __k2);
02134         return this->replace(__i1 - begin(), __i2 - __i1,
02135                              __k1.base(), __k2 - __k1);
02136       }
02137 
02138 #if __cplusplus >= 201103L
02139       /**
02140        *  @brief  Replace range of characters with initializer_list.
02141        *  @param __i1  Iterator referencing start of range to replace.
02142        *  @param __i2  Iterator referencing end of range to replace.
02143        *  @param __l  The initializer_list of characters to insert.
02144        *  @return  Reference to this string.
02145        *  @throw  std::length_error  If new length exceeds @c max_size().
02146        *
02147        *  Removes the characters in the range [__i1,__i2).  In place,
02148        *  characters in the range [__k1,__k2) are inserted.  If the
02149        *  length of result exceeds max_size(), length_error is thrown.
02150        *  The value of the string doesn't change if an error is
02151        *  thrown.
02152       */
02153       basic_string& replace(const_iterator __i1, const_iterator __i2,
02154                             initializer_list<_CharT> __l)
02155       { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
02156 #endif // C++11
02157 
02158 #if __cplusplus > 201402L
02159       /**
02160        *  @brief  Replace range of characters with string_view.
02161        *  @param __pos  The position to replace at.
02162        *  @param __n    The number of characters to replace.
02163        *  @param __svt  The object convertible to string_view to insert.
02164        *  @return  Reference to this string.
02165       */
02166       template<typename _Tp>
02167         _If_sv<_Tp, basic_string&>
02168         replace(size_type __pos, size_type __n, const _Tp& __svt)
02169         {
02170           __sv_type __sv = __svt;
02171           return this->replace(__pos, __n, __sv.data(), __sv.size());
02172         }
02173 
02174       /**
02175        *  @brief  Replace range of characters with string_view.
02176        *  @param __pos1  The position to replace at.
02177        *  @param __n1    The number of characters to replace.
02178        *  @param __svt   The object convertible to string_view to insert from.
02179        *  @param __pos2  The position in the string_view to insert from.
02180        *  @param __n2    The number of characters to insert.
02181        *  @return  Reference to this string.
02182       */
02183       template<typename _Tp>
02184         _If_sv<_Tp, basic_string&>
02185         replace(size_type __pos1, size_type __n1, const _Tp& __svt,
02186                 size_type __pos2, size_type __n2 = npos)
02187         {
02188           __sv_type __sv = __svt;
02189           return this->replace(__pos1, __n1, __sv.data()
02190                                + __sv._M_check(__pos2, "basic_string::replace"),
02191                                __sv._M_limit(__pos2, __n2));
02192         }
02193 
02194       /**
02195        *  @brief  Replace range of characters with string_view.
02196        *  @param __i1    An iterator referencing the start position
02197           to replace at.
02198        *  @param __i2    An iterator referencing the end position
02199           for the replace.
02200        *  @param __svt   The object convertible to string_view to insert from.
02201        *  @return  Reference to this string.
02202       */
02203       template<typename _Tp>
02204         _If_sv<_Tp, basic_string&>
02205         replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
02206         {
02207           __sv_type __sv = __svt;
02208           return this->replace(__i1 - begin(), __i2 - __i1, __sv);
02209         }
02210 #endif // C++17
02211 
02212     private:
02213       template<class _Integer>
02214         basic_string&
02215         _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
02216                             _Integer __n, _Integer __val, __true_type)
02217         { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
02218 
02219       template<class _InputIterator>
02220         basic_string&
02221         _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
02222                             _InputIterator __k1, _InputIterator __k2,
02223                             __false_type);
02224 
02225       basic_string&
02226       _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
02227                      _CharT __c);
02228 
02229       basic_string&
02230       _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
02231                  const size_type __len2);
02232 
02233       basic_string&
02234       _M_append(const _CharT* __s, size_type __n);
02235 
02236     public:
02237 
02238       /**
02239        *  @brief  Copy substring into C string.
02240        *  @param __s  C string to copy value into.
02241        *  @param __n  Number of characters to copy.
02242        *  @param __pos  Index of first character to copy.
02243        *  @return  Number of characters actually copied
02244        *  @throw  std::out_of_range  If __pos > size().
02245        *
02246        *  Copies up to @a __n characters starting at @a __pos into the
02247        *  C string @a __s.  If @a __pos is %greater than size(),
02248        *  out_of_range is thrown.
02249       */
02250       size_type
02251       copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
02252 
02253       /**
02254        *  @brief  Swap contents with another string.
02255        *  @param __s  String to swap with.
02256        *
02257        *  Exchanges the contents of this string with that of @a __s in constant
02258        *  time.
02259       */
02260       void
02261       swap(basic_string& __s) _GLIBCXX_NOEXCEPT;
02262 
02263       // String operations:
02264       /**
02265        *  @brief  Return const pointer to null-terminated contents.
02266        *
02267        *  This is a handle to internal data.  Do not modify or dire things may
02268        *  happen.
02269       */
02270       const _CharT*
02271       c_str() const _GLIBCXX_NOEXCEPT
02272       { return _M_data(); }
02273 
02274       /**
02275        *  @brief  Return const pointer to contents.
02276        *
02277        *  This is a pointer to internal data.  It is undefined to modify
02278        *  the contents through the returned pointer. To get a pointer that
02279        *  allows modifying the contents use @c &str[0] instead,
02280        *  (or in C++17 the non-const @c str.data() overload).
02281       */
02282       const _CharT*
02283       data() const _GLIBCXX_NOEXCEPT
02284       { return _M_data(); }
02285 
02286 #if __cplusplus > 201402L
02287       /**
02288        *  @brief  Return non-const pointer to contents.
02289        *
02290        *  This is a pointer to the character sequence held by the string.
02291        *  Modifying the characters in the sequence is allowed.
02292       */
02293       _CharT*
02294       data() noexcept
02295       { return _M_data(); }
02296 #endif
02297 
02298       /**
02299        *  @brief  Return copy of allocator used to construct this string.
02300       */
02301       allocator_type
02302       get_allocator() const _GLIBCXX_NOEXCEPT
02303       { return _M_get_allocator(); }
02304 
02305       /**
02306        *  @brief  Find position of a C substring.
02307        *  @param __s  C string to locate.
02308        *  @param __pos  Index of character to search from.
02309        *  @param __n  Number of characters from @a s to search for.
02310        *  @return  Index of start of first occurrence.
02311        *
02312        *  Starting from @a __pos, searches forward for the first @a
02313        *  __n characters in @a __s within this string.  If found,
02314        *  returns the index where it begins.  If not found, returns
02315        *  npos.
02316       */
02317       size_type
02318       find(const _CharT* __s, size_type __pos, size_type __n) const
02319       _GLIBCXX_NOEXCEPT;
02320 
02321       /**
02322        *  @brief  Find position of a string.
02323        *  @param __str  String to locate.
02324        *  @param __pos  Index of character to search from (default 0).
02325        *  @return  Index of start of first occurrence.
02326        *
02327        *  Starting from @a __pos, searches forward for value of @a __str within
02328        *  this string.  If found, returns the index where it begins.  If not
02329        *  found, returns npos.
02330       */
02331       size_type
02332       find(const basic_string& __str, size_type __pos = 0) const
02333       _GLIBCXX_NOEXCEPT
02334       { return this->find(__str.data(), __pos, __str.size()); }
02335 
02336 #if __cplusplus > 201402L
02337       /**
02338        *  @brief  Find position of a string_view.
02339        *  @param __svt  The object convertible to string_view to locate.
02340        *  @param __pos  Index of character to search from (default 0).
02341        *  @return  Index of start of first occurrence.
02342       */
02343       template<typename _Tp>
02344         _If_sv<_Tp, size_type>
02345         find(const _Tp& __svt, size_type __pos = 0) const
02346         noexcept(is_same<_Tp, __sv_type>::value)
02347         {
02348           __sv_type __sv = __svt;
02349           return this->find(__sv.data(), __pos, __sv.size());
02350         }
02351 #endif // C++17
02352 
02353       /**
02354        *  @brief  Find position of a C string.
02355        *  @param __s  C string to locate.
02356        *  @param __pos  Index of character to search from (default 0).
02357        *  @return  Index of start of first occurrence.
02358        *
02359        *  Starting from @a __pos, searches forward for the value of @a
02360        *  __s within this string.  If found, returns the index where
02361        *  it begins.  If not found, returns npos.
02362       */
02363       size_type
02364       find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
02365       {
02366         __glibcxx_requires_string(__s);
02367         return this->find(__s, __pos, traits_type::length(__s));
02368       }
02369 
02370       /**
02371        *  @brief  Find position of a character.
02372        *  @param __c  Character to locate.
02373        *  @param __pos  Index of character to search from (default 0).
02374        *  @return  Index of first occurrence.
02375        *
02376        *  Starting from @a __pos, searches forward for @a __c within
02377        *  this string.  If found, returns the index where it was
02378        *  found.  If not found, returns npos.
02379       */
02380       size_type
02381       find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
02382 
02383       /**
02384        *  @brief  Find last position of a string.
02385        *  @param __str  String to locate.
02386        *  @param __pos  Index of character to search back from (default end).
02387        *  @return  Index of start of last occurrence.
02388        *
02389        *  Starting from @a __pos, searches backward for value of @a
02390        *  __str within this string.  If found, returns the index where
02391        *  it begins.  If not found, returns npos.
02392       */
02393       size_type
02394       rfind(const basic_string& __str, size_type __pos = npos) const
02395       _GLIBCXX_NOEXCEPT
02396       { return this->rfind(__str.data(), __pos, __str.size()); }
02397 
02398 #if __cplusplus > 201402L
02399       /**
02400        *  @brief  Find last position of a string_view.
02401        *  @param __svt  The object convertible to string_view to locate.
02402        *  @param __pos  Index of character to search back from (default end).
02403        *  @return  Index of start of last occurrence.
02404       */
02405       template<typename _Tp>
02406         _If_sv<_Tp, size_type>
02407         rfind(const _Tp& __svt, size_type __pos = npos) const
02408         noexcept(is_same<_Tp, __sv_type>::value)
02409         {
02410           __sv_type __sv = __svt;
02411           return this->rfind(__sv.data(), __pos, __sv.size());
02412         }
02413 #endif // C++17
02414 
02415       /**
02416        *  @brief  Find last position of a C substring.
02417        *  @param __s  C string to locate.
02418        *  @param __pos  Index of character to search back from.
02419        *  @param __n  Number of characters from s to search for.
02420        *  @return  Index of start of last occurrence.
02421        *
02422        *  Starting from @a __pos, searches backward for the first @a
02423        *  __n characters in @a __s within this string.  If found,
02424        *  returns the index where it begins.  If not found, returns
02425        *  npos.
02426       */
02427       size_type
02428       rfind(const _CharT* __s, size_type __pos, size_type __n) const
02429       _GLIBCXX_NOEXCEPT;
02430 
02431       /**
02432        *  @brief  Find last position of a C string.
02433        *  @param __s  C string to locate.
02434        *  @param __pos  Index of character to start search at (default end).
02435        *  @return  Index of start of  last occurrence.
02436        *
02437        *  Starting from @a __pos, searches backward for the value of
02438        *  @a __s within this string.  If found, returns the index
02439        *  where it begins.  If not found, returns npos.
02440       */
02441       size_type
02442       rfind(const _CharT* __s, size_type __pos = npos) const
02443       {
02444         __glibcxx_requires_string(__s);
02445         return this->rfind(__s, __pos, traits_type::length(__s));
02446       }
02447 
02448       /**
02449        *  @brief  Find last position of a character.
02450        *  @param __c  Character to locate.
02451        *  @param __pos  Index of character to search back from (default end).
02452        *  @return  Index of last occurrence.
02453        *
02454        *  Starting from @a __pos, searches backward for @a __c within
02455        *  this string.  If found, returns the index where it was
02456        *  found.  If not found, returns npos.
02457       */
02458       size_type
02459       rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
02460 
02461       /**
02462        *  @brief  Find position of a character of string.
02463        *  @param __str  String containing characters to locate.
02464        *  @param __pos  Index of character to search from (default 0).
02465        *  @return  Index of first occurrence.
02466        *
02467        *  Starting from @a __pos, searches forward for one of the
02468        *  characters of @a __str within this string.  If found,
02469        *  returns the index where it was found.  If not found, returns
02470        *  npos.
02471       */
02472       size_type
02473       find_first_of(const basic_string& __str, size_type __pos = 0) const
02474       _GLIBCXX_NOEXCEPT
02475       { return this->find_first_of(__str.data(), __pos, __str.size()); }
02476 
02477 #if __cplusplus > 201402L
02478       /**
02479        *  @brief  Find position of a character of a string_view.
02480        *  @param __svt  An object convertible to string_view containing
02481        *                characters to locate.
02482        *  @param __pos  Index of character to search from (default 0).
02483        *  @return  Index of first occurrence.
02484       */
02485       template<typename _Tp>
02486         _If_sv<_Tp, size_type>
02487         find_first_of(const _Tp& __svt, size_type __pos = 0) const
02488         noexcept(is_same<_Tp, __sv_type>::value)
02489         {
02490           __sv_type __sv = __svt;
02491           return this->find_first_of(__sv.data(), __pos, __sv.size());
02492         }
02493 #endif // C++17
02494 
02495       /**
02496        *  @brief  Find position of a character of C substring.
02497        *  @param __s  String containing characters to locate.
02498        *  @param __pos  Index of character to search from.
02499        *  @param __n  Number of characters from s to search for.
02500        *  @return  Index of first occurrence.
02501        *
02502        *  Starting from @a __pos, searches forward for one of the
02503        *  first @a __n characters of @a __s within this string.  If
02504        *  found, returns the index where it was found.  If not found,
02505        *  returns npos.
02506       */
02507       size_type
02508       find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
02509       _GLIBCXX_NOEXCEPT;
02510 
02511       /**
02512        *  @brief  Find position of a character of C string.
02513        *  @param __s  String containing characters to locate.
02514        *  @param __pos  Index of character to search from (default 0).
02515        *  @return  Index of first occurrence.
02516        *
02517        *  Starting from @a __pos, searches forward for one of the
02518        *  characters of @a __s within this string.  If found, returns
02519        *  the index where it was found.  If not found, returns npos.
02520       */
02521       size_type
02522       find_first_of(const _CharT* __s, size_type __pos = 0) const
02523       _GLIBCXX_NOEXCEPT
02524       {
02525         __glibcxx_requires_string(__s);
02526         return this->find_first_of(__s, __pos, traits_type::length(__s));
02527       }
02528 
02529       /**
02530        *  @brief  Find position of a character.
02531        *  @param __c  Character to locate.
02532        *  @param __pos  Index of character to search from (default 0).
02533        *  @return  Index of first occurrence.
02534        *
02535        *  Starting from @a __pos, searches forward for the character
02536        *  @a __c within this string.  If found, returns the index
02537        *  where it was found.  If not found, returns npos.
02538        *
02539        *  Note: equivalent to find(__c, __pos).
02540       */
02541       size_type
02542       find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
02543       { return this->find(__c, __pos); }
02544 
02545       /**
02546        *  @brief  Find last position of a character of string.
02547        *  @param __str  String containing characters to locate.
02548        *  @param __pos  Index of character to search back from (default end).
02549        *  @return  Index of last occurrence.
02550        *
02551        *  Starting from @a __pos, searches backward for one of the
02552        *  characters of @a __str within this string.  If found,
02553        *  returns the index where it was found.  If not found, returns
02554        *  npos.
02555       */
02556       size_type
02557       find_last_of(const basic_string& __str, size_type __pos = npos) const
02558       _GLIBCXX_NOEXCEPT
02559       { return this->find_last_of(__str.data(), __pos, __str.size()); }
02560 
02561 #if __cplusplus > 201402L
02562       /**
02563        *  @brief  Find last position of a character of string.
02564        *  @param __svt  An object convertible to string_view containing
02565        *                characters to locate.
02566        *  @param __pos  Index of character to search back from (default end).
02567        *  @return  Index of last occurrence.
02568       */
02569       template<typename _Tp>
02570         _If_sv<_Tp, size_type>
02571         find_last_of(const _Tp& __svt, size_type __pos = npos) const
02572         noexcept(is_same<_Tp, __sv_type>::value)
02573         {
02574           __sv_type __sv = __svt;
02575           return this->find_last_of(__sv.data(), __pos, __sv.size());
02576         }
02577 #endif // C++17
02578 
02579       /**
02580        *  @brief  Find last position of a character of C substring.
02581        *  @param __s  C string containing characters to locate.
02582        *  @param __pos  Index of character to search back from.
02583        *  @param __n  Number of characters from s to search for.
02584        *  @return  Index of last occurrence.
02585        *
02586        *  Starting from @a __pos, searches backward for one of the
02587        *  first @a __n characters of @a __s within this string.  If
02588        *  found, returns the index where it was found.  If not found,
02589        *  returns npos.
02590       */
02591       size_type
02592       find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
02593       _GLIBCXX_NOEXCEPT;
02594 
02595       /**
02596        *  @brief  Find last position of a character of C string.
02597        *  @param __s  C string containing characters to locate.
02598        *  @param __pos  Index of character to search back from (default end).
02599        *  @return  Index of last occurrence.
02600        *
02601        *  Starting from @a __pos, searches backward for one of the
02602        *  characters of @a __s within this string.  If found, returns
02603        *  the index where it was found.  If not found, returns npos.
02604       */
02605       size_type
02606       find_last_of(const _CharT* __s, size_type __pos = npos) const
02607       _GLIBCXX_NOEXCEPT
02608       {
02609         __glibcxx_requires_string(__s);
02610         return this->find_last_of(__s, __pos, traits_type::length(__s));
02611       }
02612 
02613       /**
02614        *  @brief  Find last position of a character.
02615        *  @param __c  Character to locate.
02616        *  @param __pos  Index of character to search back from (default end).
02617        *  @return  Index of last occurrence.
02618        *
02619        *  Starting from @a __pos, searches backward for @a __c within
02620        *  this string.  If found, returns the index where it was
02621        *  found.  If not found, returns npos.
02622        *
02623        *  Note: equivalent to rfind(__c, __pos).
02624       */
02625       size_type
02626       find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
02627       { return this->rfind(__c, __pos); }
02628 
02629       /**
02630        *  @brief  Find position of a character not in string.
02631        *  @param __str  String containing characters to avoid.
02632        *  @param __pos  Index of character to search from (default 0).
02633        *  @return  Index of first occurrence.
02634        *
02635        *  Starting from @a __pos, searches forward for a character not contained
02636        *  in @a __str within this string.  If found, returns the index where it
02637        *  was found.  If not found, returns npos.
02638       */
02639       size_type
02640       find_first_not_of(const basic_string& __str, size_type __pos = 0) const
02641       _GLIBCXX_NOEXCEPT
02642       { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
02643 
02644 #if __cplusplus > 201402L
02645       /**
02646        *  @brief  Find position of a character not in a string_view.
02647        *  @param __svt  A object convertible to string_view containing
02648        *                characters to avoid.
02649        *  @param __pos  Index of character to search from (default 0).
02650        *  @return  Index of first occurrence.
02651        */
02652       template<typename _Tp>
02653         _If_sv<_Tp, size_type>
02654         find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
02655         noexcept(is_same<_Tp, __sv_type>::value)
02656         {
02657           __sv_type __sv = __svt;
02658           return this->find_first_not_of(__sv.data(), __pos, __sv.size());
02659         }
02660 #endif // C++17
02661 
02662       /**
02663        *  @brief  Find position of a character not in C substring.
02664        *  @param __s  C string containing characters to avoid.
02665        *  @param __pos  Index of character to search from.
02666        *  @param __n  Number of characters from __s to consider.
02667        *  @return  Index of first occurrence.
02668        *
02669        *  Starting from @a __pos, searches forward for a character not
02670        *  contained in the first @a __n characters of @a __s within
02671        *  this string.  If found, returns the index where it was
02672        *  found.  If not found, returns npos.
02673       */
02674       size_type
02675       find_first_not_of(const _CharT* __s, size_type __pos,
02676                         size_type __n) const _GLIBCXX_NOEXCEPT;
02677 
02678       /**
02679        *  @brief  Find position of a character not in C string.
02680        *  @param __s  C string containing characters to avoid.
02681        *  @param __pos  Index of character to search from (default 0).
02682        *  @return  Index of first occurrence.
02683        *
02684        *  Starting from @a __pos, searches forward for a character not
02685        *  contained in @a __s within this string.  If found, returns
02686        *  the index where it was found.  If not found, returns npos.
02687       */
02688       size_type
02689       find_first_not_of(const _CharT* __s, size_type __pos = 0) const
02690       _GLIBCXX_NOEXCEPT
02691       {
02692         __glibcxx_requires_string(__s);
02693         return this->find_first_not_of(__s, __pos, traits_type::length(__s));
02694       }
02695 
02696       /**
02697        *  @brief  Find position of a different character.
02698        *  @param __c  Character to avoid.
02699        *  @param __pos  Index of character to search from (default 0).
02700        *  @return  Index of first occurrence.
02701        *
02702        *  Starting from @a __pos, searches forward for a character
02703        *  other than @a __c within this string.  If found, returns the
02704        *  index where it was found.  If not found, returns npos.
02705       */
02706       size_type
02707       find_first_not_of(_CharT __c, size_type __pos = 0) const
02708       _GLIBCXX_NOEXCEPT;
02709 
02710       /**
02711        *  @brief  Find last position of a character not in string.
02712        *  @param __str  String containing characters to avoid.
02713        *  @param __pos  Index of character to search back from (default end).
02714        *  @return  Index of last occurrence.
02715        *
02716        *  Starting from @a __pos, searches backward for a character
02717        *  not contained in @a __str within this string.  If found,
02718        *  returns the index where it was found.  If not found, returns
02719        *  npos.
02720       */
02721       size_type
02722       find_last_not_of(const basic_string& __str, size_type __pos = npos) const
02723       _GLIBCXX_NOEXCEPT
02724       { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
02725 
02726 #if __cplusplus > 201402L
02727       /**
02728        *  @brief  Find last position of a character not in a string_view.
02729        *  @param __svt  An object convertible to string_view containing
02730        *                characters to avoid.
02731        *  @param __pos  Index of character to search back from (default end).
02732        *  @return  Index of last occurrence.
02733        */
02734       template<typename _Tp>
02735         _If_sv<_Tp, size_type>
02736         find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
02737         noexcept(is_same<_Tp, __sv_type>::value)
02738         {
02739           __sv_type __sv = __svt;
02740           return this->find_last_not_of(__sv.data(), __pos, __sv.size());
02741         }
02742 #endif // C++17
02743 
02744       /**
02745        *  @brief  Find last position of a character not in C substring.
02746        *  @param __s  C string containing characters to avoid.
02747        *  @param __pos  Index of character to search back from.
02748        *  @param __n  Number of characters from s to consider.
02749        *  @return  Index of last occurrence.
02750        *
02751        *  Starting from @a __pos, searches backward for a character not
02752        *  contained in the first @a __n characters of @a __s within this string.
02753        *  If found, returns the index where it was found.  If not found,
02754        *  returns npos.
02755       */
02756       size_type
02757       find_last_not_of(const _CharT* __s, size_type __pos,
02758                        size_type __n) const _GLIBCXX_NOEXCEPT;
02759       /**
02760        *  @brief  Find last position of a character not in C string.
02761        *  @param __s  C string containing characters to avoid.
02762        *  @param __pos  Index of character to search back from (default end).
02763        *  @return  Index of last occurrence.
02764        *
02765        *  Starting from @a __pos, searches backward for a character
02766        *  not contained in @a __s within this string.  If found,
02767        *  returns the index where it was found.  If not found, returns
02768        *  npos.
02769       */
02770       size_type
02771       find_last_not_of(const _CharT* __s, size_type __pos = npos) const
02772       _GLIBCXX_NOEXCEPT
02773       {
02774         __glibcxx_requires_string(__s);
02775         return this->find_last_not_of(__s, __pos, traits_type::length(__s));
02776       }
02777 
02778       /**
02779        *  @brief  Find last position of a different character.
02780        *  @param __c  Character to avoid.
02781        *  @param __pos  Index of character to search back from (default end).
02782        *  @return  Index of last occurrence.
02783        *
02784        *  Starting from @a __pos, searches backward for a character other than
02785        *  @a __c within this string.  If found, returns the index where it was
02786        *  found.  If not found, returns npos.
02787       */
02788       size_type
02789       find_last_not_of(_CharT __c, size_type __pos = npos) const
02790       _GLIBCXX_NOEXCEPT;
02791 
02792       /**
02793        *  @brief  Get a substring.
02794        *  @param __pos  Index of first character (default 0).
02795        *  @param __n  Number of characters in substring (default remainder).
02796        *  @return  The new string.
02797        *  @throw  std::out_of_range  If __pos > size().
02798        *
02799        *  Construct and return a new string using the @a __n
02800        *  characters starting at @a __pos.  If the string is too
02801        *  short, use the remainder of the characters.  If @a __pos is
02802        *  beyond the end of the string, out_of_range is thrown.
02803       */
02804       basic_string
02805       substr(size_type __pos = 0, size_type __n = npos) const
02806       { return basic_string(*this,
02807                             _M_check(__pos, "basic_string::substr"), __n); }
02808 
02809       /**
02810        *  @brief  Compare to a string.
02811        *  @param __str  String to compare against.
02812        *  @return  Integer < 0, 0, or > 0.
02813        *
02814        *  Returns an integer < 0 if this string is ordered before @a
02815        *  __str, 0 if their values are equivalent, or > 0 if this
02816        *  string is ordered after @a __str.  Determines the effective
02817        *  length rlen of the strings to compare as the smallest of
02818        *  size() and str.size().  The function then compares the two
02819        *  strings by calling traits::compare(data(), str.data(),rlen).
02820        *  If the result of the comparison is nonzero returns it,
02821        *  otherwise the shorter one is ordered first.
02822       */
02823       int
02824       compare(const basic_string& __str) const
02825       {
02826         const size_type __size = this->size();
02827         const size_type __osize = __str.size();
02828         const size_type __len = std::min(__size, __osize);
02829 
02830         int __r = traits_type::compare(_M_data(), __str.data(), __len);
02831         if (!__r)
02832           __r = _S_compare(__size, __osize);
02833         return __r;
02834       }
02835 
02836 #if __cplusplus > 201402L
02837       /**
02838        *  @brief  Compare to a string_view.
02839        *  @param __svt An object convertible to string_view to compare against.
02840        *  @return  Integer < 0, 0, or > 0.
02841        */
02842       template<typename _Tp>
02843         _If_sv<_Tp, int>
02844         compare(const _Tp& __svt) const
02845         noexcept(is_same<_Tp, __sv_type>::value)
02846         {
02847           __sv_type __sv = __svt;
02848           const size_type __size = this->size();
02849           const size_type __osize = __sv.size();
02850           const size_type __len = std::min(__size, __osize);
02851 
02852           int __r = traits_type::compare(_M_data(), __sv.data(), __len);
02853           if (!__r)
02854             __r = _S_compare(__size, __osize);
02855           return __r;
02856         }
02857 
02858       /**
02859        *  @brief  Compare to a string_view.
02860        *  @param __pos  A position in the string to start comparing from.
02861        *  @param __n  The number of characters to compare.
02862        *  @param __svt  An object convertible to string_view to compare
02863        *                against.
02864        *  @return  Integer < 0, 0, or > 0.
02865        */
02866       template<typename _Tp>
02867         _If_sv<_Tp, int>
02868         compare(size_type __pos, size_type __n, const _Tp& __svt) const
02869         noexcept(is_same<_Tp, __sv_type>::value)
02870         {
02871           __sv_type __sv = __svt;
02872           return __sv_type(*this).substr(__pos, __n).compare(__sv);
02873         }
02874 
02875       /**
02876        *  @brief  Compare to a string_view.
02877        *  @param __pos1  A position in the string to start comparing from.
02878        *  @param __n1  The number of characters to compare.
02879        *  @param __svt  An object convertible to string_view to compare
02880        *                against.
02881        *  @param __pos2  A position in the string_view to start comparing from.
02882        *  @param __n2  The number of characters to compare.
02883        *  @return  Integer < 0, 0, or > 0.
02884        */
02885       template<typename _Tp>
02886         _If_sv<_Tp, int>
02887         compare(size_type __pos1, size_type __n1, const _Tp& __svt,
02888                 size_type __pos2, size_type __n2 = npos) const
02889         noexcept(is_same<_Tp, __sv_type>::value)
02890         {
02891           __sv_type __sv = __svt;
02892           return __sv_type(*this)
02893             .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
02894         }
02895 #endif // C++17
02896 
02897       /**
02898        *  @brief  Compare substring to a string.
02899        *  @param __pos  Index of first character of substring.
02900        *  @param __n  Number of characters in substring.
02901        *  @param __str  String to compare against.
02902        *  @return  Integer < 0, 0, or > 0.
02903        *
02904        *  Form the substring of this string from the @a __n characters
02905        *  starting at @a __pos.  Returns an integer < 0 if the
02906        *  substring is ordered before @a __str, 0 if their values are
02907        *  equivalent, or > 0 if the substring is ordered after @a
02908        *  __str.  Determines the effective length rlen of the strings
02909        *  to compare as the smallest of the length of the substring
02910        *  and @a __str.size().  The function then compares the two
02911        *  strings by calling
02912        *  traits::compare(substring.data(),str.data(),rlen).  If the
02913        *  result of the comparison is nonzero returns it, otherwise
02914        *  the shorter one is ordered first.
02915       */
02916       int
02917       compare(size_type __pos, size_type __n, const basic_string& __str) const;
02918 
02919       /**
02920        *  @brief  Compare substring to a substring.
02921        *  @param __pos1  Index of first character of substring.
02922        *  @param __n1  Number of characters in substring.
02923        *  @param __str  String to compare against.
02924        *  @param __pos2  Index of first character of substring of str.
02925        *  @param __n2  Number of characters in substring of str.
02926        *  @return  Integer < 0, 0, or > 0.
02927        *
02928        *  Form the substring of this string from the @a __n1
02929        *  characters starting at @a __pos1.  Form the substring of @a
02930        *  __str from the @a __n2 characters starting at @a __pos2.
02931        *  Returns an integer < 0 if this substring is ordered before
02932        *  the substring of @a __str, 0 if their values are equivalent,
02933        *  or > 0 if this substring is ordered after the substring of
02934        *  @a __str.  Determines the effective length rlen of the
02935        *  strings to compare as the smallest of the lengths of the
02936        *  substrings.  The function then compares the two strings by
02937        *  calling
02938        *  traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
02939        *  If the result of the comparison is nonzero returns it,
02940        *  otherwise the shorter one is ordered first.
02941       */
02942       int
02943       compare(size_type __pos1, size_type __n1, const basic_string& __str,
02944               size_type __pos2, size_type __n2) const;
02945 
02946       /**
02947        *  @brief  Compare to a C string.
02948        *  @param __s  C string to compare against.
02949        *  @return  Integer < 0, 0, or > 0.
02950        *
02951        *  Returns an integer < 0 if this string is ordered before @a __s, 0 if
02952        *  their values are equivalent, or > 0 if this string is ordered after
02953        *  @a __s.  Determines the effective length rlen of the strings to
02954        *  compare as the smallest of size() and the length of a string
02955        *  constructed from @a __s.  The function then compares the two strings
02956        *  by calling traits::compare(data(),s,rlen).  If the result of the
02957        *  comparison is nonzero returns it, otherwise the shorter one is
02958        *  ordered first.
02959       */
02960       int
02961       compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
02962 
02963       // _GLIBCXX_RESOLVE_LIB_DEFECTS
02964       // 5 String::compare specification questionable
02965       /**
02966        *  @brief  Compare substring to a C string.
02967        *  @param __pos  Index of first character of substring.
02968        *  @param __n1  Number of characters in substring.
02969        *  @param __s  C string to compare against.
02970        *  @return  Integer < 0, 0, or > 0.
02971        *
02972        *  Form the substring of this string from the @a __n1
02973        *  characters starting at @a pos.  Returns an integer < 0 if
02974        *  the substring is ordered before @a __s, 0 if their values
02975        *  are equivalent, or > 0 if the substring is ordered after @a
02976        *  __s.  Determines the effective length rlen of the strings to
02977        *  compare as the smallest of the length of the substring and
02978        *  the length of a string constructed from @a __s.  The
02979        *  function then compares the two string by calling
02980        *  traits::compare(substring.data(),__s,rlen).  If the result of
02981        *  the comparison is nonzero returns it, otherwise the shorter
02982        *  one is ordered first.
02983       */
02984       int
02985       compare(size_type __pos, size_type __n1, const _CharT* __s) const;
02986 
02987       /**
02988        *  @brief  Compare substring against a character %array.
02989        *  @param __pos  Index of first character of substring.
02990        *  @param __n1  Number of characters in substring.
02991        *  @param __s  character %array to compare against.
02992        *  @param __n2  Number of characters of s.
02993        *  @return  Integer < 0, 0, or > 0.
02994        *
02995        *  Form the substring of this string from the @a __n1
02996        *  characters starting at @a __pos.  Form a string from the
02997        *  first @a __n2 characters of @a __s.  Returns an integer < 0
02998        *  if this substring is ordered before the string from @a __s,
02999        *  0 if their values are equivalent, or > 0 if this substring
03000        *  is ordered after the string from @a __s.  Determines the
03001        *  effective length rlen of the strings to compare as the
03002        *  smallest of the length of the substring and @a __n2.  The
03003        *  function then compares the two strings by calling
03004        *  traits::compare(substring.data(),s,rlen).  If the result of
03005        *  the comparison is nonzero returns it, otherwise the shorter
03006        *  one is ordered first.
03007        *
03008        *  NB: s must have at least n2 characters, &apos;\\0&apos; has
03009        *  no special meaning.
03010       */
03011       int
03012       compare(size_type __pos, size_type __n1, const _CharT* __s,
03013               size_type __n2) const;
03014 
03015       // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
03016       template<typename, typename, typename> friend class basic_stringbuf;
03017     };
03018 _GLIBCXX_END_NAMESPACE_CXX11
03019 #else  // !_GLIBCXX_USE_CXX11_ABI
03020   // Reference-counted COW string implentation
03021 
03022   /**
03023    *  @class basic_string basic_string.h <string>
03024    *  @brief  Managing sequences of characters and character-like objects.
03025    *
03026    *  @ingroup strings
03027    *  @ingroup sequences
03028    *
03029    *  @tparam _CharT  Type of character
03030    *  @tparam _Traits  Traits for character type, defaults to
03031    *                   char_traits<_CharT>.
03032    *  @tparam _Alloc  Allocator type, defaults to allocator<_CharT>.
03033    *
03034    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
03035    *  <a href="tables.html#66">reversible container</a>, and a
03036    *  <a href="tables.html#67">sequence</a>.  Of the
03037    *  <a href="tables.html#68">optional sequence requirements</a>, only
03038    *  @c push_back, @c at, and @c %array access are supported.
03039    *
03040    *  @doctodo
03041    *
03042    *
03043    *  Documentation?  What's that?
03044    *  Nathan Myers <ncm@cantrip.org>.
03045    *
03046    *  A string looks like this:
03047    *
03048    *  @code
03049    *                                        [_Rep]
03050    *                                        _M_length
03051    *   [basic_string<char_type>]            _M_capacity
03052    *   _M_dataplus                          _M_refcount
03053    *   _M_p ---------------->               unnamed array of char_type
03054    *  @endcode
03055    *
03056    *  Where the _M_p points to the first character in the string, and
03057    *  you cast it to a pointer-to-_Rep and subtract 1 to get a
03058    *  pointer to the header.
03059    *
03060    *  This approach has the enormous advantage that a string object
03061    *  requires only one allocation.  All the ugliness is confined
03062    *  within a single %pair of inline functions, which each compile to
03063    *  a single @a add instruction: _Rep::_M_data(), and
03064    *  string::_M_rep(); and the allocation function which gets a
03065    *  block of raw bytes and with room enough and constructs a _Rep
03066    *  object at the front.
03067    *
03068    *  The reason you want _M_data pointing to the character %array and
03069    *  not the _Rep is so that the debugger can see the string
03070    *  contents. (Probably we should add a non-inline member to get
03071    *  the _Rep for the debugger to use, so users can check the actual
03072    *  string length.)
03073    *
03074    *  Note that the _Rep object is a POD so that you can have a
03075    *  static <em>empty string</em> _Rep object already @a constructed before
03076    *  static constructors have run.  The reference-count encoding is
03077    *  chosen so that a 0 indicates one reference, so you never try to
03078    *  destroy the empty-string _Rep object.
03079    *
03080    *  All but the last paragraph is considered pretty conventional
03081    *  for a C++ string implementation.
03082   */
03083   // 21.3  Template class basic_string
03084   template<typename _CharT, typename _Traits, typename _Alloc>
03085     class basic_string
03086     {
03087       typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
03088 
03089       // Types:
03090     public:
03091       typedef _Traits                                       traits_type;
03092       typedef typename _Traits::char_type                   value_type;
03093       typedef _Alloc                                        allocator_type;
03094       typedef typename _CharT_alloc_type::size_type         size_type;
03095       typedef typename _CharT_alloc_type::difference_type   difference_type;
03096       typedef typename _CharT_alloc_type::reference         reference;
03097       typedef typename _CharT_alloc_type::const_reference   const_reference;
03098       typedef typename _CharT_alloc_type::pointer           pointer;
03099       typedef typename _CharT_alloc_type::const_pointer     const_pointer;
03100       typedef __gnu_cxx::__normal_iterator<pointer, basic_string>  iterator;
03101       typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
03102                                                             const_iterator;
03103       typedef std::reverse_iterator<const_iterator>     const_reverse_iterator;
03104       typedef std::reverse_iterator<iterator>               reverse_iterator;
03105 
03106     private:
03107       // _Rep: string representation
03108       //   Invariants:
03109       //   1. String really contains _M_length + 1 characters: due to 21.3.4
03110       //      must be kept null-terminated.
03111       //   2. _M_capacity >= _M_length
03112       //      Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
03113       //   3. _M_refcount has three states:
03114       //      -1: leaked, one reference, no ref-copies allowed, non-const.
03115       //       0: one reference, non-const.
03116       //     n>0: n + 1 references, operations require a lock, const.
03117       //   4. All fields==0 is an empty string, given the extra storage
03118       //      beyond-the-end for a null terminator; thus, the shared
03119       //      empty string representation needs no constructor.
03120 
03121       struct _Rep_base
03122       {
03123         size_type               _M_length;
03124         size_type               _M_capacity;
03125         _Atomic_word            _M_refcount;
03126       };
03127 
03128       struct _Rep : _Rep_base
03129       {
03130         // Types:
03131         typedef typename _Alloc::template rebind<char>::other _Raw_bytes_alloc;
03132 
03133         // (Public) Data members:
03134 
03135         // The maximum number of individual char_type elements of an
03136         // individual string is determined by _S_max_size. This is the
03137         // value that will be returned by max_size().  (Whereas npos
03138         // is the maximum number of bytes the allocator can allocate.)
03139         // If one was to divvy up the theoretical largest size string,
03140         // with a terminating character and m _CharT elements, it'd
03141         // look like this:
03142         // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
03143         // Solving for m:
03144         // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
03145         // In addition, this implementation quarters this amount.
03146         static const size_type  _S_max_size;
03147         static const _CharT     _S_terminal;
03148 
03149         // The following storage is init'd to 0 by the linker, resulting
03150         // (carefully) in an empty string with one reference.
03151         static size_type _S_empty_rep_storage[];
03152 
03153         static _Rep&
03154         _S_empty_rep() _GLIBCXX_NOEXCEPT
03155         { 
03156           // NB: Mild hack to avoid strict-aliasing warnings.  Note that
03157           // _S_empty_rep_storage is never modified and the punning should
03158           // be reasonably safe in this case.
03159           void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
03160           return *reinterpret_cast<_Rep*>(__p);
03161         }
03162 
03163         bool
03164         _M_is_leaked() const _GLIBCXX_NOEXCEPT
03165         {
03166 #if defined(__GTHREADS)
03167           // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
03168           // so we need to use an atomic load. However, _M_is_leaked
03169           // predicate does not change concurrently (i.e. the string is either
03170           // leaked or not), so a relaxed load is enough.
03171           return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0;
03172 #else
03173           return this->_M_refcount < 0;
03174 #endif
03175         }
03176 
03177         bool
03178         _M_is_shared() const _GLIBCXX_NOEXCEPT
03179         {
03180 #if defined(__GTHREADS)
03181           // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
03182           // so we need to use an atomic load. Another thread can drop last
03183           // but one reference concurrently with this check, so we need this
03184           // load to be acquire to synchronize with release fetch_and_add in
03185           // _M_dispose.
03186           return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0;
03187 #else
03188           return this->_M_refcount > 0;
03189 #endif
03190         }
03191 
03192         void
03193         _M_set_leaked() _GLIBCXX_NOEXCEPT
03194         { this->_M_refcount = -1; }
03195 
03196         void
03197         _M_set_sharable() _GLIBCXX_NOEXCEPT
03198         { this->_M_refcount = 0; }
03199 
03200         void
03201         _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
03202         {
03203 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
03204           if (__builtin_expect(this != &_S_empty_rep(), false))
03205 #endif
03206             {
03207               this->_M_set_sharable();  // One reference.
03208               this->_M_length = __n;
03209               traits_type::assign(this->_M_refdata()[__n], _S_terminal);
03210               // grrr. (per 21.3.4)
03211               // You cannot leave those LWG people alone for a second.
03212             }
03213         }
03214 
03215         _CharT*
03216         _M_refdata() throw()
03217         { return reinterpret_cast<_CharT*>(this + 1); }
03218 
03219         _CharT*
03220         _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
03221         {
03222           return (!_M_is_leaked() && __alloc1 == __alloc2)
03223                   ? _M_refcopy() : _M_clone(__alloc1);
03224         }
03225 
03226         // Create & Destroy
03227         static _Rep*
03228         _S_create(size_type, size_type, const _Alloc&);
03229 
03230         void
03231         _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
03232         {
03233 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
03234           if (__builtin_expect(this != &_S_empty_rep(), false))
03235 #endif
03236             {
03237               // Be race-detector-friendly.  For more info see bits/c++config.
03238               _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
03239               // Decrement of _M_refcount is acq_rel, because:
03240               // - all but last decrements need to release to synchronize with
03241               //   the last decrement that will delete the object.
03242               // - the last decrement needs to acquire to synchronize with
03243               //   all the previous decrements.
03244               // - last but one decrement needs to release to synchronize with
03245               //   the acquire load in _M_is_shared that will conclude that
03246               //   the object is not shared anymore.
03247               if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
03248                                                          -1) <= 0)
03249                 {
03250                   _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
03251                   _M_destroy(__a);
03252                 }
03253             }
03254         }  // XXX MT
03255 
03256         void
03257         _M_destroy(const _Alloc&) throw();
03258 
03259         _CharT*
03260         _M_refcopy() throw()
03261         {
03262 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
03263           if (__builtin_expect(this != &_S_empty_rep(), false))
03264 #endif
03265             __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
03266           return _M_refdata();
03267         }  // XXX MT
03268 
03269         _CharT*
03270         _M_clone(const _Alloc&, size_type __res = 0);
03271       };
03272 
03273       // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
03274       struct _Alloc_hider : _Alloc
03275       {
03276         _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
03277         : _Alloc(__a), _M_p(__dat) { }
03278 
03279         _CharT* _M_p; // The actual data.
03280       };
03281 
03282     public:
03283       // Data Members (public):
03284       // NB: This is an unsigned type, and thus represents the maximum
03285       // size that the allocator can hold.
03286       ///  Value returned by various member functions when they fail.
03287       static const size_type    npos = static_cast<size_type>(-1);
03288 
03289     private:
03290       // Data Members (private):
03291       mutable _Alloc_hider      _M_dataplus;
03292 
03293       _CharT*
03294       _M_data() const _GLIBCXX_NOEXCEPT
03295       { return  _M_dataplus._M_p; }
03296 
03297       _CharT*
03298       _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
03299       { return (_M_dataplus._M_p = __p); }
03300 
03301       _Rep*
03302       _M_rep() const _GLIBCXX_NOEXCEPT
03303       { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
03304 
03305       // For the internal use we have functions similar to `begin'/`end'
03306       // but they do not call _M_leak.
03307       iterator
03308       _M_ibegin() const _GLIBCXX_NOEXCEPT
03309       { return iterator(_M_data()); }
03310 
03311       iterator
03312       _M_iend() const _GLIBCXX_NOEXCEPT
03313       { return iterator(_M_data() + this->size()); }
03314 
03315       void
03316       _M_leak()    // for use in begin() & non-const op[]
03317       {
03318         if (!_M_rep()->_M_is_leaked())
03319           _M_leak_hard();
03320       }
03321 
03322       size_type
03323       _M_check(size_type __pos, const char* __s) const
03324       {
03325         if (__pos > this->size())
03326           __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
03327                                        "this->size() (which is %zu)"),
03328                                    __s, __pos, this->size());
03329         return __pos;
03330       }
03331 
03332       void
03333       _M_check_length(size_type __n1, size_type __n2, const char* __s) const
03334       {
03335         if (this->max_size() - (this->size() - __n1) < __n2)
03336           __throw_length_error(__N(__s));
03337       }
03338 
03339       // NB: _M_limit doesn't check for a bad __pos value.
03340       size_type
03341       _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
03342       {
03343         const bool __testoff =  __off < this->size() - __pos;
03344         return __testoff ? __off : this->size() - __pos;
03345       }
03346 
03347       // True if _Rep and source do not overlap.
03348       bool
03349       _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
03350       {
03351         return (less<const _CharT*>()(__s, _M_data())
03352                 || less<const _CharT*>()(_M_data() + this->size(), __s));
03353       }
03354 
03355       // When __n = 1 way faster than the general multichar
03356       // traits_type::copy/move/assign.
03357       static void
03358       _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
03359       {
03360         if (__n == 1)
03361           traits_type::assign(*__d, *__s);
03362         else
03363           traits_type::copy(__d, __s, __n);
03364       }
03365 
03366       static void
03367       _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
03368       {
03369         if (__n == 1)
03370           traits_type::assign(*__d, *__s);
03371         else
03372           traits_type::move(__d, __s, __n);       
03373       }
03374 
03375       static void
03376       _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
03377       {
03378         if (__n == 1)
03379           traits_type::assign(*__d, __c);
03380         else
03381           traits_type::assign(__d, __n, __c);     
03382       }
03383 
03384       // _S_copy_chars is a separate template to permit specialization
03385       // to optimize for the common case of pointers as iterators.
03386       template<class _Iterator>
03387         static void
03388         _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
03389         {
03390           for (; __k1 != __k2; ++__k1, (void)++__p)
03391             traits_type::assign(*__p, *__k1); // These types are off.
03392         }
03393 
03394       static void
03395       _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
03396       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
03397 
03398       static void
03399       _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
03400       _GLIBCXX_NOEXCEPT
03401       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
03402 
03403       static void
03404       _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
03405       { _M_copy(__p, __k1, __k2 - __k1); }
03406 
03407       static void
03408       _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
03409       _GLIBCXX_NOEXCEPT
03410       { _M_copy(__p, __k1, __k2 - __k1); }
03411 
03412       static int
03413       _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
03414       {
03415         const difference_type __d = difference_type(__n1 - __n2);
03416 
03417         if (__d > __gnu_cxx::__numeric_traits<int>::__max)
03418           return __gnu_cxx::__numeric_traits<int>::__max;
03419         else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
03420           return __gnu_cxx::__numeric_traits<int>::__min;
03421         else
03422           return int(__d);
03423       }
03424 
03425       void
03426       _M_mutate(size_type __pos, size_type __len1, size_type __len2);
03427 
03428       void
03429       _M_leak_hard();
03430 
03431       static _Rep&
03432       _S_empty_rep() _GLIBCXX_NOEXCEPT
03433       { return _Rep::_S_empty_rep(); }
03434 
03435 #if __cplusplus > 201402L
03436       // A helper type for avoiding boiler-plate.
03437       typedef basic_string_view<_CharT, _Traits> __sv_type;
03438 
03439       template<typename _Tp, typename _Res>
03440         using _If_sv = enable_if_t<
03441           __and_<is_convertible<const _Tp&, __sv_type>,
03442                  __not_<is_convertible<const _Tp*, const basic_string*>>,
03443                  __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
03444           _Res>;
03445 
03446       // Allows an implicit conversion to __sv_type.
03447       static __sv_type
03448       _S_to_string_view(__sv_type __svt) noexcept
03449       { return __svt; }
03450 
03451       // Wraps a string_view by explicit conversion and thus
03452       // allows to add an internal constructor that does not
03453       // participate in overload resolution when a string_view
03454       // is provided.
03455       struct __sv_wrapper
03456       {
03457         explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
03458         __sv_type _M_sv;
03459       };
03460 #endif
03461 
03462     public:
03463       // Construct/copy/destroy:
03464       // NB: We overload ctors in some cases instead of using default
03465       // arguments, per 17.4.4.4 para. 2 item 2.
03466 
03467       /**
03468        *  @brief  Default constructor creates an empty string.
03469        */
03470       basic_string()
03471 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
03472       : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
03473 #else
03474       : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
03475 #endif
03476 
03477       /**
03478        *  @brief  Construct an empty string using allocator @a a.
03479        */
03480       explicit
03481       basic_string(const _Alloc& __a);
03482 
03483       // NB: per LWG issue 42, semantics different from IS:
03484       /**
03485        *  @brief  Construct string with copy of value of @a str.
03486        *  @param  __str  Source string.
03487        */
03488       basic_string(const basic_string& __str);
03489 
03490       // _GLIBCXX_RESOLVE_LIB_DEFECTS
03491       // 2583. no way to supply an allocator for basic_string(str, pos)
03492       /**
03493        *  @brief  Construct string as copy of a substring.
03494        *  @param  __str  Source string.
03495        *  @param  __pos  Index of first character to copy from.
03496        *  @param  __a  Allocator to use.
03497        */
03498       basic_string(const basic_string& __str, size_type __pos,
03499                    const _Alloc& __a = _Alloc());
03500 
03501       /**
03502        *  @brief  Construct string as copy of a substring.
03503        *  @param  __str  Source string.
03504        *  @param  __pos  Index of first character to copy from.
03505        *  @param  __n  Number of characters to copy.
03506        */
03507       basic_string(const basic_string& __str, size_type __pos,
03508                    size_type __n);
03509       /**
03510        *  @brief  Construct string as copy of a substring.
03511        *  @param  __str  Source string.
03512        *  @param  __pos  Index of first character to copy from.
03513        *  @param  __n  Number of characters to copy.
03514        *  @param  __a  Allocator to use.
03515        */
03516       basic_string(const basic_string& __str, size_type __pos,
03517                    size_type __n, const _Alloc& __a);
03518 
03519       /**
03520        *  @brief  Construct string initialized by a character %array.
03521        *  @param  __s  Source character %array.
03522        *  @param  __n  Number of characters to copy.
03523        *  @param  __a  Allocator to use (default is default allocator).
03524        *
03525        *  NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
03526        *  has no special meaning.
03527        */
03528       basic_string(const _CharT* __s, size_type __n,
03529                    const _Alloc& __a = _Alloc());
03530       /**
03531        *  @brief  Construct string as copy of a C string.
03532        *  @param  __s  Source C string.
03533        *  @param  __a  Allocator to use (default is default allocator).
03534        */
03535       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
03536       /**
03537        *  @brief  Construct string as multiple characters.
03538        *  @param  __n  Number of characters.
03539        *  @param  __c  Character to use.
03540        *  @param  __a  Allocator to use (default is default allocator).
03541        */
03542       basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
03543 
03544 #if __cplusplus >= 201103L
03545       /**
03546        *  @brief  Move construct string.
03547        *  @param  __str  Source string.
03548        *
03549        *  The newly-created string contains the exact contents of @a __str.
03550        *  @a __str is a valid, but unspecified string.
03551        **/
03552       basic_string(basic_string&& __str)
03553 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
03554       noexcept // FIXME C++11: should always be noexcept.
03555 #endif
03556       : _M_dataplus(__str._M_dataplus)
03557       {
03558 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
03559         __str._M_data(_S_empty_rep()._M_refdata());
03560 #else
03561         __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
03562 #endif
03563       }
03564 
03565       /**
03566        *  @brief  Construct string from an initializer %list.
03567        *  @param  __l  std::initializer_list of characters.
03568        *  @param  __a  Allocator to use (default is default allocator).
03569        */
03570       basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
03571 #endif // C++11
03572 
03573       /**
03574        *  @brief  Construct string as copy of a range.
03575        *  @param  __beg  Start of range.
03576        *  @param  __end  End of range.
03577        *  @param  __a  Allocator to use (default is default allocator).
03578        */
03579       template<class _InputIterator>
03580         basic_string(_InputIterator __beg, _InputIterator __end,
03581                      const _Alloc& __a = _Alloc());
03582 
03583 #if __cplusplus > 201402L
03584       /**
03585        *  @brief  Construct string from a substring of a string_view.
03586        *  @param  __t   Source object convertible to string view.
03587        *  @param  __pos The index of the first character to copy from __t.
03588        *  @param  __n   The number of characters to copy from __t.
03589        *  @param  __a   Allocator to use.
03590        */
03591       template<typename _Tp, typename = _If_sv<_Tp, void>>
03592         basic_string(const _Tp& __t, size_type __pos, size_type __n,
03593                      const _Alloc& __a = _Alloc())
03594         : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
03595 
03596       /**
03597        *  @brief  Construct string from a string_view.
03598        *  @param  __t  Source object convertible to string view.
03599        *  @param  __a  Allocator to use (default is default allocator).
03600        */
03601       template<typename _Tp, typename = _If_sv<_Tp, void>>
03602         explicit
03603         basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
03604         : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
03605 
03606       /**
03607        *  @brief  Only internally used: Construct string from a string view
03608        *          wrapper.
03609        *  @param  __svw  string view wrapper.
03610        *  @param  __a  Allocator to use.
03611        */
03612       explicit
03613       basic_string(__sv_wrapper __svw, const _Alloc& __a)
03614       : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
03615 #endif // C++17
03616 
03617       /**
03618        *  @brief  Destroy the string instance.
03619        */
03620       ~basic_string() _GLIBCXX_NOEXCEPT
03621       { _M_rep()->_M_dispose(this->get_allocator()); }
03622 
03623       /**
03624        *  @brief  Assign the value of @a str to this string.
03625        *  @param  __str  Source string.
03626        */
03627       basic_string&
03628       operator=(const basic_string& __str) 
03629       { return this->assign(__str); }
03630 
03631       /**
03632        *  @brief  Copy contents of @a s into this string.
03633        *  @param  __s  Source null-terminated string.
03634        */
03635       basic_string&
03636       operator=(const _CharT* __s) 
03637       { return this->assign(__s); }
03638 
03639       /**
03640        *  @brief  Set value to string of length 1.
03641        *  @param  __c  Source character.
03642        *
03643        *  Assigning to a character makes this string length 1 and
03644        *  (*this)[0] == @a c.
03645        */
03646       basic_string&
03647       operator=(_CharT __c) 
03648       { 
03649         this->assign(1, __c); 
03650         return *this;
03651       }
03652 
03653 #if __cplusplus >= 201103L
03654       /**
03655        *  @brief  Move assign the value of @a str to this string.
03656        *  @param  __str  Source string.
03657        *
03658        *  The contents of @a str are moved into this string (without copying).
03659        *  @a str is a valid, but unspecified string.
03660        **/
03661       // PR 58265, this should be noexcept.
03662       basic_string&
03663       operator=(basic_string&& __str)
03664       {
03665         // NB: DR 1204.
03666         this->swap(__str);
03667         return *this;
03668       }
03669 
03670       /**
03671        *  @brief  Set value to string constructed from initializer %list.
03672        *  @param  __l  std::initializer_list.
03673        */
03674       basic_string&
03675       operator=(initializer_list<_CharT> __l)
03676       {
03677         this->assign(__l.begin(), __l.size());
03678         return *this;
03679       }
03680 #endif // C++11
03681 
03682 #if __cplusplus > 201402L
03683       /**
03684        *  @brief  Set value to string constructed from a string_view.
03685        *  @param  __svt An object convertible to  string_view.
03686        */
03687       template<typename _Tp>
03688         _If_sv<_Tp, basic_string&>
03689         operator=(const _Tp& __svt)
03690         { return this->assign(__svt); }
03691 
03692       /**
03693        *  @brief  Convert to a string_view.
03694        *  @return A string_view.
03695        */
03696       operator __sv_type() const noexcept
03697       { return __sv_type(data(), size()); }
03698 #endif // C++17
03699 
03700       // Iterators:
03701       /**
03702        *  Returns a read/write iterator that points to the first character in
03703        *  the %string.  Unshares the string.
03704        */
03705       iterator
03706       begin() // FIXME C++11: should be noexcept.
03707       {
03708         _M_leak();
03709         return iterator(_M_data());
03710       }
03711 
03712       /**
03713        *  Returns a read-only (constant) iterator that points to the first
03714        *  character in the %string.
03715        */
03716       const_iterator
03717       begin() const _GLIBCXX_NOEXCEPT
03718       { return const_iterator(_M_data()); }
03719 
03720       /**
03721        *  Returns a read/write iterator that points one past the last
03722        *  character in the %string.  Unshares the string.
03723        */
03724       iterator
03725       end() // FIXME C++11: should be noexcept.
03726       {
03727         _M_leak();
03728         return iterator(_M_data() + this->size());
03729       }
03730 
03731       /**
03732        *  Returns a read-only (constant) iterator that points one past the
03733        *  last character in the %string.
03734        */
03735       const_iterator
03736       end() const _GLIBCXX_NOEXCEPT
03737       { return const_iterator(_M_data() + this->size()); }
03738 
03739       /**
03740        *  Returns a read/write reverse iterator that points to the last
03741        *  character in the %string.  Iteration is done in reverse element
03742        *  order.  Unshares the string.
03743        */
03744       reverse_iterator
03745       rbegin() // FIXME C++11: should be noexcept.
03746       { return reverse_iterator(this->end()); }
03747 
03748       /**
03749        *  Returns a read-only (constant) reverse iterator that points
03750        *  to the last character in the %string.  Iteration is done in
03751        *  reverse element order.
03752        */
03753       const_reverse_iterator
03754       rbegin() const _GLIBCXX_NOEXCEPT
03755       { return const_reverse_iterator(this->end()); }
03756 
03757       /**
03758        *  Returns a read/write reverse iterator that points to one before the
03759        *  first character in the %string.  Iteration is done in reverse
03760        *  element order.  Unshares the string.
03761        */
03762       reverse_iterator
03763       rend() // FIXME C++11: should be noexcept.
03764       { return reverse_iterator(this->begin()); }
03765 
03766       /**
03767        *  Returns a read-only (constant) reverse iterator that points
03768        *  to one before the first character in the %string.  Iteration
03769        *  is done in reverse element order.
03770        */
03771       const_reverse_iterator
03772       rend() const _GLIBCXX_NOEXCEPT
03773       { return const_reverse_iterator(this->begin()); }
03774 
03775 #if __cplusplus >= 201103L
03776       /**
03777        *  Returns a read-only (constant) iterator that points to the first
03778        *  character in the %string.
03779        */
03780       const_iterator
03781       cbegin() const noexcept
03782       { return const_iterator(this->_M_data()); }
03783 
03784       /**
03785        *  Returns a read-only (constant) iterator that points one past the
03786        *  last character in the %string.
03787        */
03788       const_iterator
03789       cend() const noexcept
03790       { return const_iterator(this->_M_data() + this->size()); }
03791 
03792       /**
03793        *  Returns a read-only (constant) reverse iterator that points
03794        *  to the last character in the %string.  Iteration is done in
03795        *  reverse element order.
03796        */
03797       const_reverse_iterator
03798       crbegin() const noexcept
03799       { return const_reverse_iterator(this->end()); }
03800 
03801       /**
03802        *  Returns a read-only (constant) reverse iterator that points
03803        *  to one before the first character in the %string.  Iteration
03804        *  is done in reverse element order.
03805        */
03806       const_reverse_iterator
03807       crend() const noexcept
03808       { return const_reverse_iterator(this->begin()); }
03809 #endif
03810 
03811     public:
03812       // Capacity:
03813       ///  Returns the number of characters in the string, not including any
03814       ///  null-termination.
03815       size_type
03816       size() const _GLIBCXX_NOEXCEPT
03817       { return _M_rep()->_M_length; }
03818 
03819       ///  Returns the number of characters in the string, not including any
03820       ///  null-termination.
03821       size_type
03822       length() const _GLIBCXX_NOEXCEPT
03823       { return _M_rep()->_M_length; }
03824 
03825       ///  Returns the size() of the largest possible %string.
03826       size_type
03827       max_size() const _GLIBCXX_NOEXCEPT
03828       { return _Rep::_S_max_size; }
03829 
03830       /**
03831        *  @brief  Resizes the %string to the specified number of characters.
03832        *  @param  __n  Number of characters the %string should contain.
03833        *  @param  __c  Character to fill any new elements.
03834        *
03835        *  This function will %resize the %string to the specified
03836        *  number of characters.  If the number is smaller than the
03837        *  %string's current size the %string is truncated, otherwise
03838        *  the %string is extended and new elements are %set to @a __c.
03839        */
03840       void
03841       resize(size_type __n, _CharT __c);
03842 
03843       /**
03844        *  @brief  Resizes the %string to the specified number of characters.
03845        *  @param  __n  Number of characters the %string should contain.
03846        *
03847        *  This function will resize the %string to the specified length.  If
03848        *  the new size is smaller than the %string's current size the %string
03849        *  is truncated, otherwise the %string is extended and new characters
03850        *  are default-constructed.  For basic types such as char, this means
03851        *  setting them to 0.
03852        */
03853       void
03854       resize(size_type __n)
03855       { this->resize(__n, _CharT()); }
03856 
03857 #if __cplusplus >= 201103L
03858       ///  A non-binding request to reduce capacity() to size().
03859       void
03860       shrink_to_fit() _GLIBCXX_NOEXCEPT
03861       {
03862 #if __cpp_exceptions
03863         if (capacity() > size())
03864           {
03865             try
03866               { reserve(0); }
03867             catch(...)
03868               { }
03869           }
03870 #endif
03871       }
03872 #endif
03873 
03874       /**
03875        *  Returns the total number of characters that the %string can hold
03876        *  before needing to allocate more memory.
03877        */
03878       size_type
03879       capacity() const _GLIBCXX_NOEXCEPT
03880       { return _M_rep()->_M_capacity; }
03881 
03882       /**
03883        *  @brief  Attempt to preallocate enough memory for specified number of
03884        *          characters.
03885        *  @param  __res_arg  Number of characters required.
03886        *  @throw  std::length_error  If @a __res_arg exceeds @c max_size().
03887        *
03888        *  This function attempts to reserve enough memory for the
03889        *  %string to hold the specified number of characters.  If the
03890        *  number requested is more than max_size(), length_error is
03891        *  thrown.
03892        *
03893        *  The advantage of this function is that if optimal code is a
03894        *  necessity and the user can determine the string length that will be
03895        *  required, the user can reserve the memory in %advance, and thus
03896        *  prevent a possible reallocation of memory and copying of %string
03897        *  data.
03898        */
03899       void
03900       reserve(size_type __res_arg = 0);
03901 
03902       /**
03903        *  Erases the string, making it empty.
03904        */
03905 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
03906       void
03907       clear() _GLIBCXX_NOEXCEPT
03908       {
03909         if (_M_rep()->_M_is_shared())
03910           {
03911             _M_rep()->_M_dispose(this->get_allocator());
03912             _M_data(_S_empty_rep()._M_refdata());
03913           }
03914         else
03915           _M_rep()->_M_set_length_and_sharable(0);
03916       }
03917 #else
03918       // PR 56166: this should not throw.
03919       void
03920       clear()
03921       { _M_mutate(0, this->size(), 0); }
03922 #endif
03923 
03924       /**
03925        *  Returns true if the %string is empty.  Equivalent to 
03926        *  <code>*this == ""</code>.
03927        */
03928       bool
03929       empty() const _GLIBCXX_NOEXCEPT
03930       { return this->size() == 0; }
03931 
03932       // Element access:
03933       /**
03934        *  @brief  Subscript access to the data contained in the %string.
03935        *  @param  __pos  The index of the character to access.
03936        *  @return  Read-only (constant) reference to the character.
03937        *
03938        *  This operator allows for easy, array-style, data access.
03939        *  Note that data access with this operator is unchecked and
03940        *  out_of_range lookups are not defined. (For checked lookups
03941        *  see at().)
03942        */
03943       const_reference
03944       operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
03945       {
03946         __glibcxx_assert(__pos <= size());
03947         return _M_data()[__pos];
03948       }
03949 
03950       /**
03951        *  @brief  Subscript access to the data contained in the %string.
03952        *  @param  __pos  The index of the character to access.
03953        *  @return  Read/write reference to the character.
03954        *
03955        *  This operator allows for easy, array-style, data access.
03956        *  Note that data access with this operator is unchecked and
03957        *  out_of_range lookups are not defined. (For checked lookups
03958        *  see at().)  Unshares the string.
03959        */
03960       reference
03961       operator[](size_type __pos)
03962       {
03963         // Allow pos == size() both in C++98 mode, as v3 extension,
03964         // and in C++11 mode.
03965         __glibcxx_assert(__pos <= size());
03966         // In pedantic mode be strict in C++98 mode.
03967         _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
03968         _M_leak();
03969         return _M_data()[__pos];
03970       }
03971 
03972       /**
03973        *  @brief  Provides access to the data contained in the %string.
03974        *  @param __n The index of the character to access.
03975        *  @return  Read-only (const) reference to the character.
03976        *  @throw  std::out_of_range  If @a n is an invalid index.
03977        *
03978        *  This function provides for safer data access.  The parameter is
03979        *  first checked that it is in the range of the string.  The function
03980        *  throws out_of_range if the check fails.
03981        */
03982       const_reference
03983       at(size_type __n) const
03984       {
03985         if (__n >= this->size())
03986           __throw_out_of_range_fmt(__N("basic_string::at: __n "
03987                                        "(which is %zu) >= this->size() "
03988                                        "(which is %zu)"),
03989                                    __n, this->size());
03990         return _M_data()[__n];
03991       }
03992 
03993       /**
03994        *  @brief  Provides access to the data contained in the %string.
03995        *  @param __n The index of the character to access.
03996        *  @return  Read/write reference to the character.
03997        *  @throw  std::out_of_range  If @a n is an invalid index.
03998        *
03999        *  This function provides for safer data access.  The parameter is
04000        *  first checked that it is in the range of the string.  The function
04001        *  throws out_of_range if the check fails.  Success results in
04002        *  unsharing the string.
04003        */
04004       reference
04005       at(size_type __n)
04006       {
04007         if (__n >= size())
04008           __throw_out_of_range_fmt(__N("basic_string::at: __n "
04009                                        "(which is %zu) >= this->size() "
04010                                        "(which is %zu)"),
04011                                    __n, this->size());
04012         _M_leak();
04013         return _M_data()[__n];
04014       }
04015 
04016 #if __cplusplus >= 201103L
04017       /**
04018        *  Returns a read/write reference to the data at the first
04019        *  element of the %string.
04020        */
04021       reference
04022       front()
04023       {
04024         __glibcxx_assert(!empty());
04025         return operator[](0);
04026       }
04027 
04028       /**
04029        *  Returns a read-only (constant) reference to the data at the first
04030        *  element of the %string.
04031        */
04032       const_reference
04033       front() const noexcept
04034       {
04035         __glibcxx_assert(!empty());
04036         return operator[](0);
04037       }
04038 
04039       /**
04040        *  Returns a read/write reference to the data at the last
04041        *  element of the %string.
04042        */
04043       reference
04044       back()
04045       {
04046         __glibcxx_assert(!empty());
04047         return operator[](this->size() - 1);
04048       }
04049 
04050       /**
04051        *  Returns a read-only (constant) reference to the data at the
04052        *  last element of the %string.
04053        */
04054       const_reference
04055       back() const noexcept
04056       {
04057         __glibcxx_assert(!empty());
04058         return operator[](this->size() - 1);
04059       }
04060 #endif
04061 
04062       // Modifiers:
04063       /**
04064        *  @brief  Append a string to this string.
04065        *  @param __str  The string to append.
04066        *  @return  Reference to this string.
04067        */
04068       basic_string&
04069       operator+=(const basic_string& __str)
04070       { return this->append(__str); }
04071 
04072       /**
04073        *  @brief  Append a C string.
04074        *  @param __s  The C string to append.
04075        *  @return  Reference to this string.
04076        */
04077       basic_string&
04078       operator+=(const _CharT* __s)
04079       { return this->append(__s); }
04080 
04081       /**
04082        *  @brief  Append a character.
04083        *  @param __c  The character to append.
04084        *  @return  Reference to this string.
04085        */
04086       basic_string&
04087       operator+=(_CharT __c)
04088       { 
04089         this->push_back(__c);
04090         return *this;
04091       }
04092 
04093 #if __cplusplus >= 201103L
04094       /**
04095        *  @brief  Append an initializer_list of characters.
04096        *  @param __l  The initializer_list of characters to be appended.
04097        *  @return  Reference to this string.
04098        */
04099       basic_string&
04100       operator+=(initializer_list<_CharT> __l)
04101       { return this->append(__l.begin(), __l.size()); }
04102 #endif // C++11
04103 
04104 #if __cplusplus > 201402L
04105       /**
04106        *  @brief  Append a string_view.
04107        *  @param __svt The object convertible to string_view to be appended.
04108        *  @return  Reference to this string.
04109        */
04110       template<typename _Tp>
04111         _If_sv<_Tp, basic_string&>
04112         operator+=(const _Tp& __svt)
04113         { return this->append(__svt); }
04114 #endif // C++17
04115 
04116       /**
04117        *  @brief  Append a string to this string.
04118        *  @param __str  The string to append.
04119        *  @return  Reference to this string.
04120        */
04121       basic_string&
04122       append(const basic_string& __str);
04123 
04124       /**
04125        *  @brief  Append a substring.
04126        *  @param __str  The string to append.
04127        *  @param __pos  Index of the first character of str to append.
04128        *  @param __n  The number of characters to append.
04129        *  @return  Reference to this string.
04130        *  @throw  std::out_of_range if @a __pos is not a valid index.
04131        *
04132        *  This function appends @a __n characters from @a __str
04133        *  starting at @a __pos to this string.  If @a __n is is larger
04134        *  than the number of available characters in @a __str, the
04135        *  remainder of @a __str is appended.
04136        */
04137       basic_string&
04138       append(const basic_string& __str, size_type __pos, size_type __n);
04139 
04140       /**
04141        *  @brief  Append a C substring.
04142        *  @param __s  The C string to append.
04143        *  @param __n  The number of characters to append.
04144        *  @return  Reference to this string.
04145        */
04146       basic_string&
04147       append(const _CharT* __s, size_type __n);
04148 
04149       /**
04150        *  @brief  Append a C string.
04151        *  @param __s  The C string to append.
04152        *  @return  Reference to this string.
04153        */
04154       basic_string&
04155       append(const _CharT* __s)
04156       {
04157         __glibcxx_requires_string(__s);
04158         return this->append(__s, traits_type::length(__s));
04159       }
04160 
04161       /**
04162        *  @brief  Append multiple characters.
04163        *  @param __n  The number of characters to append.
04164        *  @param __c  The character to use.
04165        *  @return  Reference to this string.
04166        *
04167        *  Appends __n copies of __c to this string.
04168        */
04169       basic_string&
04170       append(size_type __n, _CharT __c);
04171 
04172 #if __cplusplus >= 201103L
04173       /**
04174        *  @brief  Append an initializer_list of characters.
04175        *  @param __l  The initializer_list of characters to append.
04176        *  @return  Reference to this string.
04177        */
04178       basic_string&
04179       append(initializer_list<_CharT> __l)
04180       { return this->append(__l.begin(), __l.size()); }
04181 #endif // C++11
04182 
04183       /**
04184        *  @brief  Append a range of characters.
04185        *  @param __first  Iterator referencing the first character to append.
04186        *  @param __last  Iterator marking the end of the range.
04187        *  @return  Reference to this string.
04188        *
04189        *  Appends characters in the range [__first,__last) to this string.
04190        */
04191       template<class _InputIterator>
04192         basic_string&
04193         append(_InputIterator __first, _InputIterator __last)
04194         { return this->replace(_M_iend(), _M_iend(), __first, __last); }
04195 
04196 #if __cplusplus > 201402L
04197       /**
04198        *  @brief  Append a string_view.
04199        *  @param __svt The object convertible to string_view to be appended.
04200        *  @return  Reference to this string.
04201        */
04202       template<typename _Tp>
04203         _If_sv<_Tp, basic_string&>
04204         append(const _Tp& __svt)
04205         {
04206           __sv_type __sv = __svt;
04207           return this->append(__sv.data(), __sv.size());
04208         }
04209 
04210       /**
04211        *  @brief  Append a range of characters from a string_view.
04212        *  @param __svt The object convertible to string_view to be appended
04213        *               from.
04214        *  @param __pos The position in the string_view to append from.
04215        *  @param __n   The number of characters to append from the string_view.
04216        *  @return  Reference to this string.
04217        */
04218       template<typename _Tp>
04219         _If_sv<_Tp, basic_string&>
04220         append(const _Tp& __svt, size_type __pos, size_type __n = npos)
04221         {
04222           __sv_type __sv = __svt;
04223           return append(__sv.data()
04224                         + __sv._M_check(__pos, "basic_string::append"),
04225                         __sv._M_limit(__pos, __n));
04226         }
04227 #endif // C++17
04228 
04229       /**
04230        *  @brief  Append a single character.
04231        *  @param __c  Character to append.
04232        */
04233       void
04234       push_back(_CharT __c)
04235       { 
04236         const size_type __len = 1 + this->size();
04237         if (__len > this->capacity() || _M_rep()->_M_is_shared())
04238           this->reserve(__len);
04239         traits_type::assign(_M_data()[this->size()], __c);
04240         _M_rep()->_M_set_length_and_sharable(__len);
04241       }
04242 
04243       /**
04244        *  @brief  Set value to contents of another string.
04245        *  @param  __str  Source string to use.
04246        *  @return  Reference to this string.
04247        */
04248       basic_string&
04249       assign(const basic_string& __str);
04250 
04251 #if __cplusplus >= 201103L
04252       /**
04253        *  @brief  Set value to contents of another string.
04254        *  @param  __str  Source string to use.
04255        *  @return  Reference to this string.
04256        *
04257        *  This function sets this string to the exact contents of @a __str.
04258        *  @a __str is a valid, but unspecified string.
04259        */
04260       // PR 58265, this should be noexcept.
04261       basic_string&
04262       assign(basic_string&& __str)
04263       {
04264         this->swap(__str);
04265         return *this;
04266       }
04267 #endif // C++11
04268 
04269       /**
04270        *  @brief  Set value to a substring of a string.
04271        *  @param __str  The string to use.
04272        *  @param __pos  Index of the first character of str.
04273        *  @param __n  Number of characters to use.
04274        *  @return  Reference to this string.
04275        *  @throw  std::out_of_range if @a pos is not a valid index.
04276        *
04277        *  This function sets this string to the substring of @a __str
04278        *  consisting of @a __n characters at @a __pos.  If @a __n is
04279        *  is larger than the number of available characters in @a
04280        *  __str, the remainder of @a __str is used.
04281        */
04282       basic_string&
04283       assign(const basic_string& __str, size_type __pos, size_type __n)
04284       { return this->assign(__str._M_data()
04285                             + __str._M_check(__pos, "basic_string::assign"),
04286                             __str._M_limit(__pos, __n)); }
04287 
04288       /**
04289        *  @brief  Set value to a C substring.
04290        *  @param __s  The C string to use.
04291        *  @param __n  Number of characters to use.
04292        *  @return  Reference to this string.
04293        *
04294        *  This function sets the value of this string to the first @a __n
04295        *  characters of @a __s.  If @a __n is is larger than the number of
04296        *  available characters in @a __s, the remainder of @a __s is used.
04297        */
04298       basic_string&
04299       assign(const _CharT* __s, size_type __n);
04300 
04301       /**
04302        *  @brief  Set value to contents of a C string.
04303        *  @param __s  The C string to use.
04304        *  @return  Reference to this string.
04305        *
04306        *  This function sets the value of this string to the value of @a __s.
04307        *  The data is copied, so there is no dependence on @a __s once the
04308        *  function returns.
04309        */
04310       basic_string&
04311       assign(const _CharT* __s)
04312       {
04313         __glibcxx_requires_string(__s);
04314         return this->assign(__s, traits_type::length(__s));
04315       }
04316 
04317       /**
04318        *  @brief  Set value to multiple characters.
04319        *  @param __n  Length of the resulting string.
04320        *  @param __c  The character to use.
04321        *  @return  Reference to this string.
04322        *
04323        *  This function sets the value of this string to @a __n copies of
04324        *  character @a __c.
04325        */
04326       basic_string&
04327       assign(size_type __n, _CharT __c)
04328       { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
04329 
04330       /**
04331        *  @brief  Set value to a range of characters.
04332        *  @param __first  Iterator referencing the first character to append.
04333        *  @param __last  Iterator marking the end of the range.
04334        *  @return  Reference to this string.
04335        *
04336        *  Sets value of string to characters in the range [__first,__last).
04337       */
04338       template<class _InputIterator>
04339         basic_string&
04340         assign(_InputIterator __first, _InputIterator __last)
04341         { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
04342 
04343 #if __cplusplus >= 201103L
04344       /**
04345        *  @brief  Set value to an initializer_list of characters.
04346        *  @param __l  The initializer_list of characters to assign.
04347        *  @return  Reference to this string.
04348        */
04349       basic_string&
04350       assign(initializer_list<_CharT> __l)
04351       { return this->assign(__l.begin(), __l.size()); }
04352 #endif // C++11
04353 
04354 #if __cplusplus > 201402L
04355       /**
04356        *  @brief  Set value from a string_view.
04357        *  @param __svt The source object convertible to string_view.
04358        *  @return  Reference to this string.
04359        */
04360       template<typename _Tp>
04361         _If_sv<_Tp, basic_string&>
04362         assign(const _Tp& __svt)
04363         {
04364           __sv_type __sv = __svt;
04365           return this->assign(__sv.data(), __sv.size());
04366         }
04367 
04368       /**
04369        *  @brief  Set value from a range of characters in a string_view.
04370        *  @param __svt  The source object convertible to string_view.
04371        *  @param __pos  The position in the string_view to assign from.
04372        *  @param __n  The number of characters to assign.
04373        *  @return  Reference to this string.
04374        */
04375       template<typename _Tp>
04376         _If_sv<_Tp, basic_string&>
04377         assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
04378         {
04379           __sv_type __sv = __svt;
04380           return assign(__sv.data()
04381                         + __sv._M_check(__pos, "basic_string::assign"),
04382                         __sv._M_limit(__pos, __n));
04383         }
04384 #endif // C++17
04385 
04386       /**
04387        *  @brief  Insert multiple characters.
04388        *  @param __p  Iterator referencing location in string to insert at.
04389        *  @param __n  Number of characters to insert
04390        *  @param __c  The character to insert.
04391        *  @throw  std::length_error  If new length exceeds @c max_size().
04392        *
04393        *  Inserts @a __n copies of character @a __c starting at the
04394        *  position referenced by iterator @a __p.  If adding
04395        *  characters causes the length to exceed max_size(),
04396        *  length_error is thrown.  The value of the string doesn't
04397        *  change if an error is thrown.
04398       */
04399       void
04400       insert(iterator __p, size_type __n, _CharT __c)
04401       { this->replace(__p, __p, __n, __c);  }
04402 
04403       /**
04404        *  @brief  Insert a range of characters.
04405        *  @param __p  Iterator referencing location in string to insert at.
04406        *  @param __beg  Start of range.
04407        *  @param __end  End of range.
04408        *  @throw  std::length_error  If new length exceeds @c max_size().
04409        *
04410        *  Inserts characters in range [__beg,__end).  If adding
04411        *  characters causes the length to exceed max_size(),
04412        *  length_error is thrown.  The value of the string doesn't
04413        *  change if an error is thrown.
04414       */
04415       template<class _InputIterator>
04416         void
04417         insert(iterator __p, _InputIterator __beg, _InputIterator __end)
04418         { this->replace(__p, __p, __beg, __end); }
04419 
04420 #if __cplusplus >= 201103L
04421       /**
04422        *  @brief  Insert an initializer_list of characters.
04423        *  @param __p  Iterator referencing location in string to insert at.
04424        *  @param __l  The initializer_list of characters to insert.
04425        *  @throw  std::length_error  If new length exceeds @c max_size().
04426        */
04427       void
04428       insert(iterator __p, initializer_list<_CharT> __l)
04429       {
04430         _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
04431         this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
04432       }
04433 #endif // C++11
04434 
04435       /**
04436        *  @brief  Insert value of a string.
04437        *  @param __pos1  Iterator referencing location in string to insert at.
04438        *  @param __str  The string to insert.
04439        *  @return  Reference to this string.
04440        *  @throw  std::length_error  If new length exceeds @c max_size().
04441        *
04442        *  Inserts value of @a __str starting at @a __pos1.  If adding
04443        *  characters causes the length to exceed max_size(),
04444        *  length_error is thrown.  The value of the string doesn't
04445        *  change if an error is thrown.
04446       */
04447       basic_string&
04448       insert(size_type __pos1, const basic_string& __str)
04449       { return this->insert(__pos1, __str, size_type(0), __str.size()); }
04450 
04451       /**
04452        *  @brief  Insert a substring.
04453        *  @param __pos1  Iterator referencing location in string to insert at.
04454        *  @param __str  The string to insert.
04455        *  @param __pos2  Start of characters in str to insert.
04456        *  @param __n  Number of characters to insert.
04457        *  @return  Reference to this string.
04458        *  @throw  std::length_error  If new length exceeds @c max_size().
04459        *  @throw  std::out_of_range  If @a pos1 > size() or
04460        *  @a __pos2 > @a str.size().
04461        *
04462        *  Starting at @a pos1, insert @a __n character of @a __str
04463        *  beginning with @a __pos2.  If adding characters causes the
04464        *  length to exceed max_size(), length_error is thrown.  If @a
04465        *  __pos1 is beyond the end of this string or @a __pos2 is
04466        *  beyond the end of @a __str, out_of_range is thrown.  The
04467        *  value of the string doesn't change if an error is thrown.
04468       */
04469       basic_string&
04470       insert(size_type __pos1, const basic_string& __str,
04471              size_type __pos2, size_type __n)
04472       { return this->insert(__pos1, __str._M_data()
04473                             + __str._M_check(__pos2, "basic_string::insert"),
04474                             __str._M_limit(__pos2, __n)); }
04475 
04476       /**
04477        *  @brief  Insert a C substring.
04478        *  @param __pos  Iterator referencing location in string to insert at.
04479        *  @param __s  The C string to insert.
04480        *  @param __n  The number of characters to insert.
04481        *  @return  Reference to this string.
04482        *  @throw  std::length_error  If new length exceeds @c max_size().
04483        *  @throw  std::out_of_range  If @a __pos is beyond the end of this
04484        *  string.
04485        *
04486        *  Inserts the first @a __n characters of @a __s starting at @a
04487        *  __pos.  If adding characters causes the length to exceed
04488        *  max_size(), length_error is thrown.  If @a __pos is beyond
04489        *  end(), out_of_range is thrown.  The value of the string
04490        *  doesn't change if an error is thrown.
04491       */
04492       basic_string&
04493       insert(size_type __pos, const _CharT* __s, size_type __n);
04494 
04495       /**
04496        *  @brief  Insert a C string.
04497        *  @param __pos  Iterator referencing location in string to insert at.
04498        *  @param __s  The C string to insert.
04499        *  @return  Reference to this string.
04500        *  @throw  std::length_error  If new length exceeds @c max_size().
04501        *  @throw  std::out_of_range  If @a pos is beyond the end of this
04502        *  string.
04503        *
04504        *  Inserts the first @a n characters of @a __s starting at @a __pos.  If
04505        *  adding characters causes the length to exceed max_size(),
04506        *  length_error is thrown.  If @a __pos is beyond end(), out_of_range is
04507        *  thrown.  The value of the string doesn't change if an error is
04508        *  thrown.
04509       */
04510       basic_string&
04511       insert(size_type __pos, const _CharT* __s)
04512       {
04513         __glibcxx_requires_string(__s);
04514         return this->insert(__pos, __s, traits_type::length(__s));
04515       }
04516 
04517       /**
04518        *  @brief  Insert multiple characters.
04519        *  @param __pos  Index in string to insert at.
04520        *  @param __n  Number of characters to insert
04521        *  @param __c  The character to insert.
04522        *  @return  Reference to this string.
04523        *  @throw  std::length_error  If new length exceeds @c max_size().
04524        *  @throw  std::out_of_range  If @a __pos is beyond the end of this
04525        *  string.
04526        *
04527        *  Inserts @a __n copies of character @a __c starting at index
04528        *  @a __pos.  If adding characters causes the length to exceed
04529        *  max_size(), length_error is thrown.  If @a __pos > length(),
04530        *  out_of_range is thrown.  The value of the string doesn't
04531        *  change if an error is thrown.
04532       */
04533       basic_string&
04534       insert(size_type __pos, size_type __n, _CharT __c)
04535       { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
04536                               size_type(0), __n, __c); }
04537 
04538       /**
04539        *  @brief  Insert one character.
04540        *  @param __p  Iterator referencing position in string to insert at.
04541        *  @param __c  The character to insert.
04542        *  @return  Iterator referencing newly inserted char.
04543        *  @throw  std::length_error  If new length exceeds @c max_size().
04544        *
04545        *  Inserts character @a __c at position referenced by @a __p.
04546        *  If adding character causes the length to exceed max_size(),
04547        *  length_error is thrown.  If @a __p is beyond end of string,
04548        *  out_of_range is thrown.  The value of the string doesn't
04549        *  change if an error is thrown.
04550       */
04551       iterator
04552       insert(iterator __p, _CharT __c)
04553       {
04554         _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
04555         const size_type __pos = __p - _M_ibegin();
04556         _M_replace_aux(__pos, size_type(0), size_type(1), __c);
04557         _M_rep()->_M_set_leaked();
04558         return iterator(_M_data() + __pos);
04559       }
04560 
04561 #if __cplusplus > 201402L
04562       /**
04563        *  @brief  Insert a string_view.
04564        *  @param __pos  Iterator referencing position in string to insert at.
04565        *  @param __svt  The object convertible to string_view to insert.
04566        *  @return  Reference to this string.
04567       */
04568       template<typename _Tp>
04569         _If_sv<_Tp, basic_string&>
04570         insert(size_type __pos, const _Tp& __svt)
04571         {
04572           __sv_type __sv = __svt;
04573           return this->insert(__pos, __sv.data(), __sv.size());
04574         }
04575 
04576       /**
04577        *  @brief  Insert a string_view.
04578        *  @param __pos  Iterator referencing position in string to insert at.
04579        *  @param __svt  The object convertible to string_view to insert from.
04580        *  @param __pos  Iterator referencing position in string_view to insert
04581        *  from.
04582        *  @param __n    The number of characters to insert.
04583        *  @return  Reference to this string.
04584       */
04585       template<typename _Tp>
04586         _If_sv<_Tp, basic_string&>
04587         insert(size_type __pos1, const _Tp& __svt,
04588                size_type __pos2, size_type __n = npos)
04589         {
04590           __sv_type __sv = __svt;
04591           return this->replace(__pos1, size_type(0), __sv.data()
04592                                + __sv._M_check(__pos2, "basic_string::insert"),
04593                                __sv._M_limit(__pos2, __n));
04594         }
04595 #endif // C++17
04596 
04597       /**
04598        *  @brief  Remove characters.
04599        *  @param __pos  Index of first character to remove (default 0).
04600        *  @param __n  Number of characters to remove (default remainder).
04601        *  @return  Reference to this string.
04602        *  @throw  std::out_of_range  If @a pos is beyond the end of this
04603        *  string.
04604        *
04605        *  Removes @a __n characters from this string starting at @a
04606        *  __pos.  The length of the string is reduced by @a __n.  If
04607        *  there are < @a __n characters to remove, the remainder of
04608        *  the string is truncated.  If @a __p is beyond end of string,
04609        *  out_of_range is thrown.  The value of the string doesn't
04610        *  change if an error is thrown.
04611       */
04612       basic_string&
04613       erase(size_type __pos = 0, size_type __n = npos)
04614       { 
04615         _M_mutate(_M_check(__pos, "basic_string::erase"),
04616                   _M_limit(__pos, __n), size_type(0));
04617         return *this;
04618       }
04619 
04620       /**
04621        *  @brief  Remove one character.
04622        *  @param __position  Iterator referencing the character to remove.
04623        *  @return  iterator referencing same location after removal.
04624        *
04625        *  Removes the character at @a __position from this string. The value
04626        *  of the string doesn't change if an error is thrown.
04627       */
04628       iterator
04629       erase(iterator __position)
04630       {
04631         _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
04632                                  && __position < _M_iend());
04633         const size_type __pos = __position - _M_ibegin();
04634         _M_mutate(__pos, size_type(1), size_type(0));
04635         _M_rep()->_M_set_leaked();
04636         return iterator(_M_data() + __pos);
04637       }
04638 
04639       /**
04640        *  @brief  Remove a range of characters.
04641        *  @param __first  Iterator referencing the first character to remove.
04642        *  @param __last  Iterator referencing the end of the range.
04643        *  @return  Iterator referencing location of first after removal.
04644        *
04645        *  Removes the characters in the range [first,last) from this string.
04646        *  The value of the string doesn't change if an error is thrown.
04647       */
04648       iterator
04649       erase(iterator __first, iterator __last);
04650  
04651 #if __cplusplus >= 201103L
04652       /**
04653        *  @brief  Remove the last character.
04654        *
04655        *  The string must be non-empty.
04656        */
04657       void
04658       pop_back() // FIXME C++11: should be noexcept.
04659       {
04660         __glibcxx_assert(!empty());
04661         erase(size() - 1, 1);
04662       }
04663 #endif // C++11
04664 
04665       /**
04666        *  @brief  Replace characters with value from another string.
04667        *  @param __pos  Index of first character to replace.
04668        *  @param __n  Number of characters to be replaced.
04669        *  @param __str  String to insert.
04670        *  @return  Reference to this string.
04671        *  @throw  std::out_of_range  If @a pos is beyond the end of this
04672        *  string.
04673        *  @throw  std::length_error  If new length exceeds @c max_size().
04674        *
04675        *  Removes the characters in the range [__pos,__pos+__n) from
04676        *  this string.  In place, the value of @a __str is inserted.
04677        *  If @a __pos is beyond end of string, out_of_range is thrown.
04678        *  If the length of the result exceeds max_size(), length_error
04679        *  is thrown.  The value of the string doesn't change if an
04680        *  error is thrown.
04681       */
04682       basic_string&
04683       replace(size_type __pos, size_type __n, const basic_string& __str)
04684       { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
04685 
04686       /**
04687        *  @brief  Replace characters with value from another string.
04688        *  @param __pos1  Index of first character to replace.
04689        *  @param __n1  Number of characters to be replaced.
04690        *  @param __str  String to insert.
04691        *  @param __pos2  Index of first character of str to use.
04692        *  @param __n2  Number of characters from str to use.
04693        *  @return  Reference to this string.
04694        *  @throw  std::out_of_range  If @a __pos1 > size() or @a __pos2 >
04695        *  __str.size().
04696        *  @throw  std::length_error  If new length exceeds @c max_size().
04697        *
04698        *  Removes the characters in the range [__pos1,__pos1 + n) from this
04699        *  string.  In place, the value of @a __str is inserted.  If @a __pos is
04700        *  beyond end of string, out_of_range is thrown.  If the length of the
04701        *  result exceeds max_size(), length_error is thrown.  The value of the
04702        *  string doesn't change if an error is thrown.
04703       */
04704       basic_string&
04705       replace(size_type __pos1, size_type __n1, const basic_string& __str,
04706               size_type __pos2, size_type __n2)
04707       { return this->replace(__pos1, __n1, __str._M_data()
04708                              + __str._M_check(__pos2, "basic_string::replace"),
04709                              __str._M_limit(__pos2, __n2)); }
04710 
04711       /**
04712        *  @brief  Replace characters with value of a C substring.
04713        *  @param __pos  Index of first character to replace.
04714        *  @param __n1  Number of characters to be replaced.
04715        *  @param __s  C string to insert.
04716        *  @param __n2  Number of characters from @a s to use.
04717        *  @return  Reference to this string.
04718        *  @throw  std::out_of_range  If @a pos1 > size().
04719        *  @throw  std::length_error  If new length exceeds @c max_size().
04720        *
04721        *  Removes the characters in the range [__pos,__pos + __n1)
04722        *  from this string.  In place, the first @a __n2 characters of
04723        *  @a __s are inserted, or all of @a __s if @a __n2 is too large.  If
04724        *  @a __pos is beyond end of string, out_of_range is thrown.  If
04725        *  the length of result exceeds max_size(), length_error is
04726        *  thrown.  The value of the string doesn't change if an error
04727        *  is thrown.
04728       */
04729       basic_string&
04730       replace(size_type __pos, size_type __n1, const _CharT* __s,
04731               size_type __n2);
04732 
04733       /**
04734        *  @brief  Replace characters with value of a C string.
04735        *  @param __pos  Index of first character to replace.
04736        *  @param __n1  Number of characters to be replaced.
04737        *  @param __s  C string to insert.
04738        *  @return  Reference to this string.
04739        *  @throw  std::out_of_range  If @a pos > size().
04740        *  @throw  std::length_error  If new length exceeds @c max_size().
04741        *
04742        *  Removes the characters in the range [__pos,__pos + __n1)
04743        *  from this string.  In place, the characters of @a __s are
04744        *  inserted.  If @a __pos is beyond end of string, out_of_range
04745        *  is thrown.  If the length of result exceeds max_size(),
04746        *  length_error is thrown.  The value of the string doesn't
04747        *  change if an error is thrown.
04748       */
04749       basic_string&
04750       replace(size_type __pos, size_type __n1, const _CharT* __s)
04751       {
04752         __glibcxx_requires_string(__s);
04753         return this->replace(__pos, __n1, __s, traits_type::length(__s));
04754       }
04755 
04756       /**
04757        *  @brief  Replace characters with multiple characters.
04758        *  @param __pos  Index of first character to replace.
04759        *  @param __n1  Number of characters to be replaced.
04760        *  @param __n2  Number of characters to insert.
04761        *  @param __c  Character to insert.
04762        *  @return  Reference to this string.
04763        *  @throw  std::out_of_range  If @a __pos > size().
04764        *  @throw  std::length_error  If new length exceeds @c max_size().
04765        *
04766        *  Removes the characters in the range [pos,pos + n1) from this
04767        *  string.  In place, @a __n2 copies of @a __c are inserted.
04768        *  If @a __pos is beyond end of string, out_of_range is thrown.
04769        *  If the length of result exceeds max_size(), length_error is
04770        *  thrown.  The value of the string doesn't change if an error
04771        *  is thrown.
04772       */
04773       basic_string&
04774       replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
04775       { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
04776                               _M_limit(__pos, __n1), __n2, __c); }
04777 
04778       /**
04779        *  @brief  Replace range of characters with string.
04780        *  @param __i1  Iterator referencing start of range to replace.
04781        *  @param __i2  Iterator referencing end of range to replace.
04782        *  @param __str  String value to insert.
04783        *  @return  Reference to this string.
04784        *  @throw  std::length_error  If new length exceeds @c max_size().
04785        *
04786        *  Removes the characters in the range [__i1,__i2).  In place,
04787        *  the value of @a __str is inserted.  If the length of result
04788        *  exceeds max_size(), length_error is thrown.  The value of
04789        *  the string doesn't change if an error is thrown.
04790       */
04791       basic_string&
04792       replace(iterator __i1, iterator __i2, const basic_string& __str)
04793       { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
04794 
04795       /**
04796        *  @brief  Replace range of characters with C substring.
04797        *  @param __i1  Iterator referencing start of range to replace.
04798        *  @param __i2  Iterator referencing end of range to replace.
04799        *  @param __s  C string value to insert.
04800        *  @param __n  Number of characters from s to insert.
04801        *  @return  Reference to this string.
04802        *  @throw  std::length_error  If new length exceeds @c max_size().
04803        *
04804        *  Removes the characters in the range [__i1,__i2).  In place,
04805        *  the first @a __n characters of @a __s are inserted.  If the
04806        *  length of result exceeds max_size(), length_error is thrown.
04807        *  The value of the string doesn't change if an error is
04808        *  thrown.
04809       */
04810       basic_string&
04811       replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
04812       {
04813         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
04814                                  && __i2 <= _M_iend());
04815         return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
04816       }
04817 
04818       /**
04819        *  @brief  Replace range of characters with C string.
04820        *  @param __i1  Iterator referencing start of range to replace.
04821        *  @param __i2  Iterator referencing end of range to replace.
04822        *  @param __s  C string value to insert.
04823        *  @return  Reference to this string.
04824        *  @throw  std::length_error  If new length exceeds @c max_size().
04825        *
04826        *  Removes the characters in the range [__i1,__i2).  In place,
04827        *  the characters of @a __s are inserted.  If the length of
04828        *  result exceeds max_size(), length_error is thrown.  The
04829        *  value of the string doesn't change if an error is thrown.
04830       */
04831       basic_string&
04832       replace(iterator __i1, iterator __i2, const _CharT* __s)
04833       {
04834         __glibcxx_requires_string(__s);
04835         return this->replace(__i1, __i2, __s, traits_type::length(__s));
04836       }
04837 
04838       /**
04839        *  @brief  Replace range of characters with multiple characters
04840        *  @param __i1  Iterator referencing start of range to replace.
04841        *  @param __i2  Iterator referencing end of range to replace.
04842        *  @param __n  Number of characters to insert.
04843        *  @param __c  Character to insert.
04844        *  @return  Reference to this string.
04845        *  @throw  std::length_error  If new length exceeds @c max_size().
04846        *
04847        *  Removes the characters in the range [__i1,__i2).  In place,
04848        *  @a __n copies of @a __c are inserted.  If the length of
04849        *  result exceeds max_size(), length_error is thrown.  The
04850        *  value of the string doesn't change if an error is thrown.
04851       */
04852       basic_string&
04853       replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
04854       {
04855         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
04856                                  && __i2 <= _M_iend());
04857         return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
04858       }
04859 
04860       /**
04861        *  @brief  Replace range of characters with range.
04862        *  @param __i1  Iterator referencing start of range to replace.
04863        *  @param __i2  Iterator referencing end of range to replace.
04864        *  @param __k1  Iterator referencing start of range to insert.
04865        *  @param __k2  Iterator referencing end of range to insert.
04866        *  @return  Reference to this string.
04867        *  @throw  std::length_error  If new length exceeds @c max_size().
04868        *
04869        *  Removes the characters in the range [__i1,__i2).  In place,
04870        *  characters in the range [__k1,__k2) are inserted.  If the
04871        *  length of result exceeds max_size(), length_error is thrown.
04872        *  The value of the string doesn't change if an error is
04873        *  thrown.
04874       */
04875       template<class _InputIterator>
04876         basic_string&
04877         replace(iterator __i1, iterator __i2,
04878                 _InputIterator __k1, _InputIterator __k2)
04879         {
04880           _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
04881                                    && __i2 <= _M_iend());
04882           __glibcxx_requires_valid_range(__k1, __k2);
04883           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
04884           return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
04885         }
04886 
04887       // Specializations for the common case of pointer and iterator:
04888       // useful to avoid the overhead of temporary buffering in _M_replace.
04889       basic_string&
04890       replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
04891       {
04892         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
04893                                  && __i2 <= _M_iend());
04894         __glibcxx_requires_valid_range(__k1, __k2);
04895         return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
04896                              __k1, __k2 - __k1);
04897       }
04898 
04899       basic_string&
04900       replace(iterator __i1, iterator __i2,
04901               const _CharT* __k1, const _CharT* __k2)
04902       {
04903         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
04904                                  && __i2 <= _M_iend());
04905         __glibcxx_requires_valid_range(__k1, __k2);
04906         return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
04907                              __k1, __k2 - __k1);
04908       }
04909 
04910       basic_string&
04911       replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
04912       {
04913         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
04914                                  && __i2 <= _M_iend());
04915         __glibcxx_requires_valid_range(__k1, __k2);
04916         return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
04917                              __k1.base(), __k2 - __k1);
04918       }
04919 
04920       basic_string&
04921       replace(iterator __i1, iterator __i2,
04922               const_iterator __k1, const_iterator __k2)
04923       {
04924         _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
04925                                  && __i2 <= _M_iend());
04926         __glibcxx_requires_valid_range(__k1, __k2);
04927         return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
04928                              __k1.base(), __k2 - __k1);
04929       }
04930 
04931 #if __cplusplus >= 201103L
04932       /**
04933        *  @brief  Replace range of characters with initializer_list.
04934        *  @param __i1  Iterator referencing start of range to replace.
04935        *  @param __i2  Iterator referencing end of range to replace.
04936        *  @param __l  The initializer_list of characters to insert.
04937        *  @return  Reference to this string.
04938        *  @throw  std::length_error  If new length exceeds @c max_size().
04939        *
04940        *  Removes the characters in the range [__i1,__i2).  In place,
04941        *  characters in the range [__k1,__k2) are inserted.  If the
04942        *  length of result exceeds max_size(), length_error is thrown.
04943        *  The value of the string doesn't change if an error is
04944        *  thrown.
04945       */
04946       basic_string& replace(iterator __i1, iterator __i2,
04947                             initializer_list<_CharT> __l)
04948       { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
04949 #endif // C++11
04950 
04951 #if __cplusplus > 201402L
04952       /**
04953        *  @brief  Replace range of characters with string_view.
04954        *  @param __pos  The position to replace at.
04955        *  @param __n    The number of characters to replace.
04956        *  @param __svt  The object convertible to string_view to insert.
04957        *  @return  Reference to this string.
04958       */
04959       template<typename _Tp>
04960         _If_sv<_Tp, basic_string&>
04961         replace(size_type __pos, size_type __n, const _Tp& __svt)
04962         {
04963           __sv_type __sv = __svt;
04964           return this->replace(__pos, __n, __sv.data(), __sv.size());
04965         }
04966 
04967       /**
04968        *  @brief  Replace range of characters with string_view.
04969        *  @param __pos1  The position to replace at.
04970        *  @param __n1    The number of characters to replace.
04971        *  @param __svt   The object convertible to string_view to insert from.
04972        *  @param __pos2  The position in the string_view to insert from.
04973        *  @param __n2    The number of characters to insert.
04974        *  @return  Reference to this string.
04975       */
04976       template<typename _Tp>
04977         _If_sv<_Tp, basic_string&>
04978         replace(size_type __pos1, size_type __n1, const _Tp& __svt,
04979                 size_type __pos2, size_type __n2 = npos)
04980         {
04981           __sv_type __sv = __svt;
04982           return this->replace(__pos1, __n1,
04983               __sv.data() + __sv._M_check(__pos2, "basic_string::replace"),
04984               __sv._M_limit(__pos2, __n2));
04985         }
04986 
04987       /**
04988        *  @brief  Replace range of characters with string_view.
04989        *  @param __i1    An iterator referencing the start position
04990           to replace at.
04991        *  @param __i2    An iterator referencing the end position
04992           for the replace.
04993        *  @param __svt   The object convertible to string_view to insert from.
04994        *  @return  Reference to this string.
04995       */
04996       template<typename _Tp>
04997         _If_sv<_Tp, basic_string&>
04998         replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
04999         {
05000           __sv_type __sv = __svt;
05001           return this->replace(__i1 - begin(), __i2 - __i1, __sv);
05002         }
05003 #endif // C++17
05004 
05005     private:
05006       template<class _Integer>
05007         basic_string&
05008         _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
05009                             _Integer __val, __true_type)
05010         { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
05011 
05012       template<class _InputIterator>
05013         basic_string&
05014         _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
05015                             _InputIterator __k2, __false_type);
05016 
05017       basic_string&
05018       _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
05019                      _CharT __c);
05020 
05021       basic_string&
05022       _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
05023                       size_type __n2);
05024 
05025       // _S_construct_aux is used to implement the 21.3.1 para 15 which
05026       // requires special behaviour if _InIter is an integral type
05027       template<class _InIterator>
05028         static _CharT*
05029         _S_construct_aux(_InIterator __beg, _InIterator __end,
05030                          const _Alloc& __a, __false_type)
05031         {
05032           typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
05033           return _S_construct(__beg, __end, __a, _Tag());
05034         }
05035 
05036       // _GLIBCXX_RESOLVE_LIB_DEFECTS
05037       // 438. Ambiguity in the "do the right thing" clause
05038       template<class _Integer>
05039         static _CharT*
05040         _S_construct_aux(_Integer __beg, _Integer __end,
05041                          const _Alloc& __a, __true_type)
05042         { return _S_construct_aux_2(static_cast<size_type>(__beg),
05043                                     __end, __a); }
05044 
05045       static _CharT*
05046       _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
05047       { return _S_construct(__req, __c, __a); }
05048 
05049       template<class _InIterator>
05050         static _CharT*
05051         _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
05052         {
05053           typedef typename std::__is_integer<_InIterator>::__type _Integral;
05054           return _S_construct_aux(__beg, __end, __a, _Integral());
05055         }
05056 
05057       // For Input Iterators, used in istreambuf_iterators, etc.
05058       template<class _InIterator>
05059         static _CharT*
05060          _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
05061                       input_iterator_tag);
05062 
05063       // For forward_iterators up to random_access_iterators, used for
05064       // string::iterator, _CharT*, etc.
05065       template<class _FwdIterator>
05066         static _CharT*
05067         _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
05068                      forward_iterator_tag);
05069 
05070       static _CharT*
05071       _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
05072 
05073     public:
05074 
05075       /**
05076        *  @brief  Copy substring into C string.
05077        *  @param __s  C string to copy value into.
05078        *  @param __n  Number of characters to copy.
05079        *  @param __pos  Index of first character to copy.
05080        *  @return  Number of characters actually copied
05081        *  @throw  std::out_of_range  If __pos > size().
05082        *
05083        *  Copies up to @a __n characters starting at @a __pos into the
05084        *  C string @a __s.  If @a __pos is %greater than size(),
05085        *  out_of_range is thrown.
05086       */
05087       size_type
05088       copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
05089 
05090       /**
05091        *  @brief  Swap contents with another string.
05092        *  @param __s  String to swap with.
05093        *
05094        *  Exchanges the contents of this string with that of @a __s in constant
05095        *  time.
05096       */
05097       // PR 58265, this should be noexcept.
05098       void
05099       swap(basic_string& __s);
05100 
05101       // String operations:
05102       /**
05103        *  @brief  Return const pointer to null-terminated contents.
05104        *
05105        *  This is a handle to internal data.  Do not modify or dire things may
05106        *  happen.
05107       */
05108       const _CharT*
05109       c_str() const _GLIBCXX_NOEXCEPT
05110       { return _M_data(); }
05111 
05112       /**
05113        *  @brief  Return const pointer to contents.
05114        *
05115        *  This is a pointer to internal data.  It is undefined to modify
05116        *  the contents through the returned pointer. To get a pointer that
05117        *  allows modifying the contents use @c &str[0] instead,
05118        *  (or in C++17 the non-const @c str.data() overload).
05119       */
05120       const _CharT*
05121       data() const _GLIBCXX_NOEXCEPT
05122       { return _M_data(); }
05123 
05124 #if __cplusplus > 201402L
05125       /**
05126        *  @brief  Return non-const pointer to contents.
05127        *
05128        *  This is a pointer to the character sequence held by the string.
05129        *  Modifying the characters in the sequence is allowed.
05130       */
05131       _CharT*
05132       data() noexcept
05133       { return _M_data(); }
05134 #endif
05135 
05136       /**
05137        *  @brief  Return copy of allocator used to construct this string.
05138       */
05139       allocator_type
05140       get_allocator() const _GLIBCXX_NOEXCEPT
05141       { return _M_dataplus; }
05142 
05143       /**
05144        *  @brief  Find position of a C substring.
05145        *  @param __s  C string to locate.
05146        *  @param __pos  Index of character to search from.
05147        *  @param __n  Number of characters from @a s to search for.
05148        *  @return  Index of start of first occurrence.
05149        *
05150        *  Starting from @a __pos, searches forward for the first @a
05151        *  __n characters in @a __s within this string.  If found,
05152        *  returns the index where it begins.  If not found, returns
05153        *  npos.
05154       */
05155       size_type
05156       find(const _CharT* __s, size_type __pos, size_type __n) const
05157       _GLIBCXX_NOEXCEPT;
05158 
05159       /**
05160        *  @brief  Find position of a string.
05161        *  @param __str  String to locate.
05162        *  @param __pos  Index of character to search from (default 0).
05163        *  @return  Index of start of first occurrence.
05164        *
05165        *  Starting from @a __pos, searches forward for value of @a __str within
05166        *  this string.  If found, returns the index where it begins.  If not
05167        *  found, returns npos.
05168       */
05169       size_type
05170       find(const basic_string& __str, size_type __pos = 0) const
05171       _GLIBCXX_NOEXCEPT
05172       { return this->find(__str.data(), __pos, __str.size()); }
05173 
05174       /**
05175        *  @brief  Find position of a C string.
05176        *  @param __s  C string to locate.
05177        *  @param __pos  Index of character to search from (default 0).
05178        *  @return  Index of start of first occurrence.
05179        *
05180        *  Starting from @a __pos, searches forward for the value of @a
05181        *  __s within this string.  If found, returns the index where
05182        *  it begins.  If not found, returns npos.
05183       */
05184       size_type
05185       find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
05186       {
05187         __glibcxx_requires_string(__s);
05188         return this->find(__s, __pos, traits_type::length(__s));
05189       }
05190 
05191       /**
05192        *  @brief  Find position of a character.
05193        *  @param __c  Character to locate.
05194        *  @param __pos  Index of character to search from (default 0).
05195        *  @return  Index of first occurrence.
05196        *
05197        *  Starting from @a __pos, searches forward for @a __c within
05198        *  this string.  If found, returns the index where it was
05199        *  found.  If not found, returns npos.
05200       */
05201       size_type
05202       find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
05203 
05204 #if __cplusplus > 201402L
05205       /**
05206        *  @brief  Find position of a string_view.
05207        *  @param __svt  The object convertible to string_view to locate.
05208        *  @param __pos  Index of character to search from (default 0).
05209        *  @return  Index of start of first occurrence.
05210       */
05211       template<typename _Tp>
05212         _If_sv<_Tp, size_type>
05213         find(const _Tp& __svt, size_type __pos = 0) const
05214         noexcept(is_same<_Tp, __sv_type>::value)
05215         {
05216           __sv_type __sv = __svt;
05217           return this->find(__sv.data(), __pos, __sv.size());
05218         }
05219 #endif // C++17
05220 
05221       /**
05222        *  @brief  Find last position of a string.
05223        *  @param __str  String to locate.
05224        *  @param __pos  Index of character to search back from (default end).
05225        *  @return  Index of start of last occurrence.
05226        *
05227        *  Starting from @a __pos, searches backward for value of @a
05228        *  __str within this string.  If found, returns the index where
05229        *  it begins.  If not found, returns npos.
05230       */
05231       size_type
05232       rfind(const basic_string& __str, size_type __pos = npos) const
05233       _GLIBCXX_NOEXCEPT
05234       { return this->rfind(__str.data(), __pos, __str.size()); }
05235 
05236       /**
05237        *  @brief  Find last position of a C substring.
05238        *  @param __s  C string to locate.
05239        *  @param __pos  Index of character to search back from.
05240        *  @param __n  Number of characters from s to search for.
05241        *  @return  Index of start of last occurrence.
05242        *
05243        *  Starting from @a __pos, searches backward for the first @a
05244        *  __n characters in @a __s within this string.  If found,
05245        *  returns the index where it begins.  If not found, returns
05246        *  npos.
05247       */
05248       size_type
05249       rfind(const _CharT* __s, size_type __pos, size_type __n) const
05250       _GLIBCXX_NOEXCEPT;
05251 
05252       /**
05253        *  @brief  Find last position of a C string.
05254        *  @param __s  C string to locate.
05255        *  @param __pos  Index of character to start search at (default end).
05256        *  @return  Index of start of  last occurrence.
05257        *
05258        *  Starting from @a __pos, searches backward for the value of
05259        *  @a __s within this string.  If found, returns the index
05260        *  where it begins.  If not found, returns npos.
05261       */
05262       size_type
05263       rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
05264       {
05265         __glibcxx_requires_string(__s);
05266         return this->rfind(__s, __pos, traits_type::length(__s));
05267       }
05268 
05269       /**
05270        *  @brief  Find last position of a character.
05271        *  @param __c  Character to locate.
05272        *  @param __pos  Index of character to search back from (default end).
05273        *  @return  Index of last occurrence.
05274        *
05275        *  Starting from @a __pos, searches backward for @a __c within
05276        *  this string.  If found, returns the index where it was
05277        *  found.  If not found, returns npos.
05278       */
05279       size_type
05280       rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
05281 
05282 #if __cplusplus > 201402L
05283       /**
05284        *  @brief  Find last position of a string_view.
05285        *  @param __svt  The object convertible to string_view to locate.
05286        *  @param __pos  Index of character to search back from (default end).
05287        *  @return  Index of start of last occurrence.
05288       */
05289       template<typename _Tp>
05290         _If_sv<_Tp, size_type>
05291         rfind(const _Tp& __svt, size_type __pos = npos) const
05292         noexcept(is_same<_Tp, __sv_type>::value)
05293         {
05294           __sv_type __sv = __svt;
05295           return this->rfind(__sv.data(), __pos, __sv.size());
05296         }
05297 #endif // C++17
05298 
05299       /**
05300        *  @brief  Find position of a character of string.
05301        *  @param __str  String containing characters to locate.
05302        *  @param __pos  Index of character to search from (default 0).
05303        *  @return  Index of first occurrence.
05304        *
05305        *  Starting from @a __pos, searches forward for one of the
05306        *  characters of @a __str within this string.  If found,
05307        *  returns the index where it was found.  If not found, returns
05308        *  npos.
05309       */
05310       size_type
05311       find_first_of(const basic_string& __str, size_type __pos = 0) const
05312       _GLIBCXX_NOEXCEPT
05313       { return this->find_first_of(__str.data(), __pos, __str.size()); }
05314 
05315       /**
05316        *  @brief  Find position of a character of C substring.
05317        *  @param __s  String containing characters to locate.
05318        *  @param __pos  Index of character to search from.
05319        *  @param __n  Number of characters from s to search for.
05320        *  @return  Index of first occurrence.
05321        *
05322        *  Starting from @a __pos, searches forward for one of the
05323        *  first @a __n characters of @a __s within this string.  If
05324        *  found, returns the index where it was found.  If not found,
05325        *  returns npos.
05326       */
05327       size_type
05328       find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
05329       _GLIBCXX_NOEXCEPT;
05330 
05331       /**
05332        *  @brief  Find position of a character of C string.
05333        *  @param __s  String containing characters to locate.
05334        *  @param __pos  Index of character to search from (default 0).
05335        *  @return  Index of first occurrence.
05336        *
05337        *  Starting from @a __pos, searches forward for one of the
05338        *  characters of @a __s within this string.  If found, returns
05339        *  the index where it was found.  If not found, returns npos.
05340       */
05341       size_type
05342       find_first_of(const _CharT* __s, size_type __pos = 0) const
05343       _GLIBCXX_NOEXCEPT
05344       {
05345         __glibcxx_requires_string(__s);
05346         return this->find_first_of(__s, __pos, traits_type::length(__s));
05347       }
05348 
05349       /**
05350        *  @brief  Find position of a character.
05351        *  @param __c  Character to locate.
05352        *  @param __pos  Index of character to search from (default 0).
05353        *  @return  Index of first occurrence.
05354        *
05355        *  Starting from @a __pos, searches forward for the character
05356        *  @a __c within this string.  If found, returns the index
05357        *  where it was found.  If not found, returns npos.
05358        *
05359        *  Note: equivalent to find(__c, __pos).
05360       */
05361       size_type
05362       find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
05363       { return this->find(__c, __pos); }
05364 
05365 #if __cplusplus > 201402L
05366       /**
05367        *  @brief  Find position of a character of a string_view.
05368        *  @param __svt  An object convertible to string_view containing
05369        *                characters to locate.
05370        *  @param __pos  Index of character to search from (default 0).
05371        *  @return  Index of first occurrence.
05372       */
05373       template<typename _Tp>
05374         _If_sv<_Tp, size_type>
05375         find_first_of(const _Tp& __svt, size_type __pos = 0) const
05376         noexcept(is_same<_Tp, __sv_type>::value)
05377         {
05378           __sv_type __sv = __svt;
05379           return this->find_first_of(__sv.data(), __pos, __sv.size());
05380         }
05381 #endif // C++17
05382 
05383       /**
05384        *  @brief  Find last position of a character of string.
05385        *  @param __str  String containing characters to locate.
05386        *  @param __pos  Index of character to search back from (default end).
05387        *  @return  Index of last occurrence.
05388        *
05389        *  Starting from @a __pos, searches backward for one of the
05390        *  characters of @a __str within this string.  If found,
05391        *  returns the index where it was found.  If not found, returns
05392        *  npos.
05393       */
05394       size_type
05395       find_last_of(const basic_string& __str, size_type __pos = npos) const
05396       _GLIBCXX_NOEXCEPT
05397       { return this->find_last_of(__str.data(), __pos, __str.size()); }
05398 
05399       /**
05400        *  @brief  Find last position of a character of C substring.
05401        *  @param __s  C string containing characters to locate.
05402        *  @param __pos  Index of character to search back from.
05403        *  @param __n  Number of characters from s to search for.
05404        *  @return  Index of last occurrence.
05405        *
05406        *  Starting from @a __pos, searches backward for one of the
05407        *  first @a __n characters of @a __s within this string.  If
05408        *  found, returns the index where it was found.  If not found,
05409        *  returns npos.
05410       */
05411       size_type
05412       find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
05413       _GLIBCXX_NOEXCEPT;
05414 
05415       /**
05416        *  @brief  Find last position of a character of C string.
05417        *  @param __s  C string containing characters to locate.
05418        *  @param __pos  Index of character to search back from (default end).
05419        *  @return  Index of last occurrence.
05420        *
05421        *  Starting from @a __pos, searches backward for one of the
05422        *  characters of @a __s within this string.  If found, returns
05423        *  the index where it was found.  If not found, returns npos.
05424       */
05425       size_type
05426       find_last_of(const _CharT* __s, size_type __pos = npos) const
05427       _GLIBCXX_NOEXCEPT
05428       {
05429         __glibcxx_requires_string(__s);
05430         return this->find_last_of(__s, __pos, traits_type::length(__s));
05431       }
05432 
05433       /**
05434        *  @brief  Find last position of a character.
05435        *  @param __c  Character to locate.
05436        *  @param __pos  Index of character to search back from (default end).
05437        *  @return  Index of last occurrence.
05438        *
05439        *  Starting from @a __pos, searches backward for @a __c within
05440        *  this string.  If found, returns the index where it was
05441        *  found.  If not found, returns npos.
05442        *
05443        *  Note: equivalent to rfind(__c, __pos).
05444       */
05445       size_type
05446       find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
05447       { return this->rfind(__c, __pos); }
05448 
05449 #if __cplusplus > 201402L
05450       /**
05451        *  @brief  Find last position of a character of string.
05452        *  @param __svt  An object convertible to string_view containing
05453        *                characters to locate.
05454        *  @param __pos  Index of character to search back from (default end).
05455        *  @return  Index of last occurrence.
05456       */
05457       template<typename _Tp>
05458         _If_sv<_Tp, size_type>
05459         find_last_of(const _Tp& __svt, size_type __pos = npos) const
05460         noexcept(is_same<_Tp, __sv_type>::value)
05461         {
05462           __sv_type __sv = __svt;
05463           return this->find_last_of(__sv.data(), __pos, __sv.size());
05464         }
05465 #endif // C++17
05466 
05467       /**
05468        *  @brief  Find position of a character not in string.
05469        *  @param __str  String containing characters to avoid.
05470        *  @param __pos  Index of character to search from (default 0).
05471        *  @return  Index of first occurrence.
05472        *
05473        *  Starting from @a __pos, searches forward for a character not contained
05474        *  in @a __str within this string.  If found, returns the index where it
05475        *  was found.  If not found, returns npos.
05476       */
05477       size_type
05478       find_first_not_of(const basic_string& __str, size_type __pos = 0) const
05479       _GLIBCXX_NOEXCEPT
05480       { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
05481 
05482       /**
05483        *  @brief  Find position of a character not in C substring.
05484        *  @param __s  C string containing characters to avoid.
05485        *  @param __pos  Index of character to search from.
05486        *  @param __n  Number of characters from __s to consider.
05487        *  @return  Index of first occurrence.
05488        *
05489        *  Starting from @a __pos, searches forward for a character not
05490        *  contained in the first @a __n characters of @a __s within
05491        *  this string.  If found, returns the index where it was
05492        *  found.  If not found, returns npos.
05493       */
05494       size_type
05495       find_first_not_of(const _CharT* __s, size_type __pos,
05496                         size_type __n) const _GLIBCXX_NOEXCEPT;
05497 
05498       /**
05499        *  @brief  Find position of a character not in C string.
05500        *  @param __s  C string containing characters to avoid.
05501        *  @param __pos  Index of character to search from (default 0).
05502        *  @return  Index of first occurrence.
05503        *
05504        *  Starting from @a __pos, searches forward for a character not
05505        *  contained in @a __s within this string.  If found, returns
05506        *  the index where it was found.  If not found, returns npos.
05507       */
05508       size_type
05509       find_first_not_of(const _CharT* __s, size_type __pos = 0) const
05510       _GLIBCXX_NOEXCEPT
05511       {
05512         __glibcxx_requires_string(__s);
05513         return this->find_first_not_of(__s, __pos, traits_type::length(__s));
05514       }
05515 
05516       /**
05517        *  @brief  Find position of a different character.
05518        *  @param __c  Character to avoid.
05519        *  @param __pos  Index of character to search from (default 0).
05520        *  @return  Index of first occurrence.
05521        *
05522        *  Starting from @a __pos, searches forward for a character
05523        *  other than @a __c within this string.  If found, returns the
05524        *  index where it was found.  If not found, returns npos.
05525       */
05526       size_type
05527       find_first_not_of(_CharT __c, size_type __pos = 0) const
05528       _GLIBCXX_NOEXCEPT;
05529 
05530 #if __cplusplus > 201402L
05531       /**
05532        *  @brief  Find position of a character not in a string_view.
05533        *  @param __svt  An object convertible to string_view containing
05534        *                characters to avoid.
05535        *  @param __pos  Index of character to search from (default 0).
05536        *  @return  Index of first occurrence.
05537        */
05538       template<typename _Tp>
05539         _If_sv<_Tp, size_type>
05540         find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
05541         noexcept(is_same<_Tp, __sv_type>::value)
05542         {
05543           __sv_type __sv = __svt;
05544           return this->find_first_not_of(__sv.data(), __pos, __sv.size());
05545         }
05546 #endif // C++17
05547 
05548       /**
05549        *  @brief  Find last position of a character not in string.
05550        *  @param __str  String containing characters to avoid.
05551        *  @param __pos  Index of character to search back from (default end).
05552        *  @return  Index of last occurrence.
05553        *
05554        *  Starting from @a __pos, searches backward for a character
05555        *  not contained in @a __str within this string.  If found,
05556        *  returns the index where it was found.  If not found, returns
05557        *  npos.
05558       */
05559       size_type
05560       find_last_not_of(const basic_string& __str, size_type __pos = npos) const
05561       _GLIBCXX_NOEXCEPT
05562       { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
05563 
05564       /**
05565        *  @brief  Find last position of a character not in C substring.
05566        *  @param __s  C string containing characters to avoid.
05567        *  @param __pos  Index of character to search back from.
05568        *  @param __n  Number of characters from s to consider.
05569        *  @return  Index of last occurrence.
05570        *
05571        *  Starting from @a __pos, searches backward for a character not
05572        *  contained in the first @a __n characters of @a __s within this string.
05573        *  If found, returns the index where it was found.  If not found,
05574        *  returns npos.
05575       */
05576       size_type
05577       find_last_not_of(const _CharT* __s, size_type __pos,
05578                        size_type __n) const _GLIBCXX_NOEXCEPT;
05579       /**
05580        *  @brief  Find last position of a character not in C string.
05581        *  @param __s  C string containing characters to avoid.
05582        *  @param __pos  Index of character to search back from (default end).
05583        *  @return  Index of last occurrence.
05584        *
05585        *  Starting from @a __pos, searches backward for a character
05586        *  not contained in @a __s within this string.  If found,
05587        *  returns the index where it was found.  If not found, returns
05588        *  npos.
05589       */
05590       size_type
05591       find_last_not_of(const _CharT* __s, size_type __pos = npos) const
05592       _GLIBCXX_NOEXCEPT
05593       {
05594         __glibcxx_requires_string(__s);
05595         return this->find_last_not_of(__s, __pos, traits_type::length(__s));
05596       }
05597 
05598       /**
05599        *  @brief  Find last position of a different character.
05600        *  @param __c  Character to avoid.
05601        *  @param __pos  Index of character to search back from (default end).
05602        *  @return  Index of last occurrence.
05603        *
05604        *  Starting from @a __pos, searches backward for a character other than
05605        *  @a __c within this string.  If found, returns the index where it was
05606        *  found.  If not found, returns npos.
05607       */
05608       size_type
05609       find_last_not_of(_CharT __c, size_type __pos = npos) const
05610       _GLIBCXX_NOEXCEPT;
05611 
05612 #if __cplusplus > 201402L
05613       /**
05614        *  @brief  Find last position of a character not in a string_view.
05615        *  @param __svt  An object convertible to string_view containing
05616        *                characters to avoid.
05617        *  @param __pos  Index of character to search back from (default end).
05618        *  @return  Index of last occurrence.
05619        */
05620       template<typename _Tp>
05621         _If_sv<_Tp, size_type>
05622         find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
05623         noexcept(is_same<_Tp, __sv_type>::value)
05624         {
05625           __sv_type __sv = __svt;
05626           return this->find_last_not_of(__sv.data(), __pos, __sv.size());
05627         }
05628 #endif // C++17
05629 
05630       /**
05631        *  @brief  Get a substring.
05632        *  @param __pos  Index of first character (default 0).
05633        *  @param __n  Number of characters in substring (default remainder).
05634        *  @return  The new string.
05635        *  @throw  std::out_of_range  If __pos > size().
05636        *
05637        *  Construct and return a new string using the @a __n
05638        *  characters starting at @a __pos.  If the string is too
05639        *  short, use the remainder of the characters.  If @a __pos is
05640        *  beyond the end of the string, out_of_range is thrown.
05641       */
05642       basic_string
05643       substr(size_type __pos = 0, size_type __n = npos) const
05644       { return basic_string(*this,
05645                             _M_check(__pos, "basic_string::substr"), __n); }
05646 
05647       /**
05648        *  @brief  Compare to a string.
05649        *  @param __str  String to compare against.
05650        *  @return  Integer < 0, 0, or > 0.
05651        *
05652        *  Returns an integer < 0 if this string is ordered before @a
05653        *  __str, 0 if their values are equivalent, or > 0 if this
05654        *  string is ordered after @a __str.  Determines the effective
05655        *  length rlen of the strings to compare as the smallest of
05656        *  size() and str.size().  The function then compares the two
05657        *  strings by calling traits::compare(data(), str.data(),rlen).
05658        *  If the result of the comparison is nonzero returns it,
05659        *  otherwise the shorter one is ordered first.
05660       */
05661       int
05662       compare(const basic_string& __str) const
05663       {
05664         const size_type __size = this->size();
05665         const size_type __osize = __str.size();
05666         const size_type __len = std::min(__size, __osize);
05667 
05668         int __r = traits_type::compare(_M_data(), __str.data(), __len);
05669         if (!__r)
05670           __r = _S_compare(__size, __osize);
05671         return __r;
05672       }
05673 
05674 #if __cplusplus > 201402L
05675       /**
05676        *  @brief  Compare to a string_view.
05677        *  @param __svt An object convertible to string_view to compare against.
05678        *  @return  Integer < 0, 0, or > 0.
05679        */
05680       template<typename _Tp>
05681         _If_sv<_Tp, int>
05682         compare(const _Tp& __svt) const
05683         noexcept(is_same<_Tp, __sv_type>::value)
05684         {
05685            __sv_type __sv = __svt;
05686           const size_type __size = this->size();
05687           const size_type __osize = __sv.size();
05688           const size_type __len = std::min(__size, __osize);
05689 
05690           int __r = traits_type::compare(_M_data(), __sv.data(), __len);
05691           if (!__r)
05692             __r = _S_compare(__size, __osize);
05693           return __r;
05694         }
05695 
05696       /**
05697        *  @brief  Compare to a string_view.
05698        *  @param __pos  A position in the string to start comparing from.
05699        *  @param __n  The number of characters to compare.
05700        *  @param __svt  An object convertible to string_view to compare
05701        *                against.
05702        *  @return  Integer < 0, 0, or > 0.
05703        */
05704       template<typename _Tp>
05705         _If_sv<_Tp, int>
05706         compare(size_type __pos, size_type __n, const _Tp& __svt) const
05707         noexcept(is_same<_Tp, __sv_type>::value)
05708         {
05709           __sv_type __sv = __svt;
05710           return __sv_type(*this).substr(__pos, __n).compare(__sv);
05711         }
05712 
05713       /**
05714        *  @brief  Compare to a string_view.
05715        *  @param __pos1  A position in the string to start comparing from.
05716        *  @param __n1  The number of characters to compare.
05717        *  @param __svt   An object convertible to string_view to compare
05718        *                 against.
05719        *  @param __pos2  A position in the string_view to start comparing from.
05720        *  @param __n2  The number of characters to compare.
05721        *  @return  Integer < 0, 0, or > 0.
05722        */
05723       template<typename _Tp>
05724         _If_sv<_Tp, int>
05725         compare(size_type __pos1, size_type __n1, const _Tp& __svt,
05726                 size_type __pos2, size_type __n2 = npos) const
05727         noexcept(is_same<_Tp, __sv_type>::value)
05728         {
05729           __sv_type __sv = __svt;
05730           return __sv_type(*this)
05731             .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
05732         }
05733 #endif // C++17
05734 
05735       /**
05736        *  @brief  Compare substring to a string.
05737        *  @param __pos  Index of first character of substring.
05738        *  @param __n  Number of characters in substring.
05739        *  @param __str  String to compare against.
05740        *  @return  Integer < 0, 0, or > 0.
05741        *
05742        *  Form the substring of this string from the @a __n characters
05743        *  starting at @a __pos.  Returns an integer < 0 if the
05744        *  substring is ordered before @a __str, 0 if their values are
05745        *  equivalent, or > 0 if the substring is ordered after @a
05746        *  __str.  Determines the effective length rlen of the strings
05747        *  to compare as the smallest of the length of the substring
05748        *  and @a __str.size().  The function then compares the two
05749        *  strings by calling
05750        *  traits::compare(substring.data(),str.data(),rlen).  If the
05751        *  result of the comparison is nonzero returns it, otherwise
05752        *  the shorter one is ordered first.
05753       */
05754       int
05755       compare(size_type __pos, size_type __n, const basic_string& __str) const;
05756 
05757       /**
05758        *  @brief  Compare substring to a substring.
05759        *  @param __pos1  Index of first character of substring.
05760        *  @param __n1  Number of characters in substring.
05761        *  @param __str  String to compare against.
05762        *  @param __pos2  Index of first character of substring of str.
05763        *  @param __n2  Number of characters in substring of str.
05764        *  @return  Integer < 0, 0, or > 0.
05765        *
05766        *  Form the substring of this string from the @a __n1
05767        *  characters starting at @a __pos1.  Form the substring of @a
05768        *  __str from the @a __n2 characters starting at @a __pos2.
05769        *  Returns an integer < 0 if this substring is ordered before
05770        *  the substring of @a __str, 0 if their values are equivalent,
05771        *  or > 0 if this substring is ordered after the substring of
05772        *  @a __str.  Determines the effective length rlen of the
05773        *  strings to compare as the smallest of the lengths of the
05774        *  substrings.  The function then compares the two strings by
05775        *  calling
05776        *  traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
05777        *  If the result of the comparison is nonzero returns it,
05778        *  otherwise the shorter one is ordered first.
05779       */
05780       int
05781       compare(size_type __pos1, size_type __n1, const basic_string& __str,
05782               size_type __pos2, size_type __n2) const;
05783 
05784       /**
05785        *  @brief  Compare to a C string.
05786        *  @param __s  C string to compare against.
05787        *  @return  Integer < 0, 0, or > 0.
05788        *
05789        *  Returns an integer < 0 if this string is ordered before @a __s, 0 if
05790        *  their values are equivalent, or > 0 if this string is ordered after
05791        *  @a __s.  Determines the effective length rlen of the strings to
05792        *  compare as the smallest of size() and the length of a string
05793        *  constructed from @a __s.  The function then compares the two strings
05794        *  by calling traits::compare(data(),s,rlen).  If the result of the
05795        *  comparison is nonzero returns it, otherwise the shorter one is
05796        *  ordered first.
05797       */
05798       int
05799       compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
05800 
05801       // _GLIBCXX_RESOLVE_LIB_DEFECTS
05802       // 5 String::compare specification questionable
05803       /**
05804        *  @brief  Compare substring to a C string.
05805        *  @param __pos  Index of first character of substring.
05806        *  @param __n1  Number of characters in substring.
05807        *  @param __s  C string to compare against.
05808        *  @return  Integer < 0, 0, or > 0.
05809        *
05810        *  Form the substring of this string from the @a __n1
05811        *  characters starting at @a pos.  Returns an integer < 0 if
05812        *  the substring is ordered before @a __s, 0 if their values
05813        *  are equivalent, or > 0 if the substring is ordered after @a
05814        *  __s.  Determines the effective length rlen of the strings to
05815        *  compare as the smallest of the length of the substring and
05816        *  the length of a string constructed from @a __s.  The
05817        *  function then compares the two string by calling
05818        *  traits::compare(substring.data(),__s,rlen).  If the result of
05819        *  the comparison is nonzero returns it, otherwise the shorter
05820        *  one is ordered first.
05821       */
05822       int
05823       compare(size_type __pos, size_type __n1, const _CharT* __s) const;
05824 
05825       /**
05826        *  @brief  Compare substring against a character %array.
05827        *  @param __pos  Index of first character of substring.
05828        *  @param __n1  Number of characters in substring.
05829        *  @param __s  character %array to compare against.
05830        *  @param __n2  Number of characters of s.
05831        *  @return  Integer < 0, 0, or > 0.
05832        *
05833        *  Form the substring of this string from the @a __n1
05834        *  characters starting at @a __pos.  Form a string from the
05835        *  first @a __n2 characters of @a __s.  Returns an integer < 0
05836        *  if this substring is ordered before the string from @a __s,
05837        *  0 if their values are equivalent, or > 0 if this substring
05838        *  is ordered after the string from @a __s.  Determines the
05839        *  effective length rlen of the strings to compare as the
05840        *  smallest of the length of the substring and @a __n2.  The
05841        *  function then compares the two strings by calling
05842        *  traits::compare(substring.data(),s,rlen).  If the result of
05843        *  the comparison is nonzero returns it, otherwise the shorter
05844        *  one is ordered first.
05845        *
05846        *  NB: s must have at least n2 characters, &apos;\\0&apos; has
05847        *  no special meaning.
05848       */
05849       int
05850       compare(size_type __pos, size_type __n1, const _CharT* __s,
05851               size_type __n2) const;
05852 
05853 # ifdef _GLIBCXX_TM_TS_INTERNAL
05854       friend void
05855       ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
05856                                             void* exc);
05857       friend const char*
05858       ::_txnal_cow_string_c_str(const void *that);
05859       friend void
05860       ::_txnal_cow_string_D1(void *that);
05861       friend void
05862       ::_txnal_cow_string_D1_commit(void *that);
05863 # endif
05864   };
05865 #endif  // !_GLIBCXX_USE_CXX11_ABI
05866 
05867   // operator+
05868   /**
05869    *  @brief  Concatenate two strings.
05870    *  @param __lhs  First string.
05871    *  @param __rhs  Last string.
05872    *  @return  New string with value of @a __lhs followed by @a __rhs.
05873    */
05874   template<typename _CharT, typename _Traits, typename _Alloc>
05875     basic_string<_CharT, _Traits, _Alloc>
05876     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
05877               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
05878     {
05879       basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
05880       __str.append(__rhs);
05881       return __str;
05882     }
05883 
05884   /**
05885    *  @brief  Concatenate C string and string.
05886    *  @param __lhs  First string.
05887    *  @param __rhs  Last string.
05888    *  @return  New string with value of @a __lhs followed by @a __rhs.
05889    */
05890   template<typename _CharT, typename _Traits, typename _Alloc>
05891     basic_string<_CharT,_Traits,_Alloc>
05892     operator+(const _CharT* __lhs,
05893               const basic_string<_CharT,_Traits,_Alloc>& __rhs);
05894 
05895   /**
05896    *  @brief  Concatenate character and string.
05897    *  @param __lhs  First string.
05898    *  @param __rhs  Last string.
05899    *  @return  New string with @a __lhs followed by @a __rhs.
05900    */
05901   template<typename _CharT, typename _Traits, typename _Alloc>
05902     basic_string<_CharT,_Traits,_Alloc>
05903     operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
05904 
05905   /**
05906    *  @brief  Concatenate string and C string.
05907    *  @param __lhs  First string.
05908    *  @param __rhs  Last string.
05909    *  @return  New string with @a __lhs followed by @a __rhs.
05910    */
05911   template<typename _CharT, typename _Traits, typename _Alloc>
05912     inline basic_string<_CharT, _Traits, _Alloc>
05913     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
05914               const _CharT* __rhs)
05915     {
05916       basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
05917       __str.append(__rhs);
05918       return __str;
05919     }
05920 
05921   /**
05922    *  @brief  Concatenate string and character.
05923    *  @param __lhs  First string.
05924    *  @param __rhs  Last string.
05925    *  @return  New string with @a __lhs followed by @a __rhs.
05926    */
05927   template<typename _CharT, typename _Traits, typename _Alloc>
05928     inline basic_string<_CharT, _Traits, _Alloc>
05929     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
05930     {
05931       typedef basic_string<_CharT, _Traits, _Alloc>     __string_type;
05932       typedef typename __string_type::size_type         __size_type;
05933       __string_type __str(__lhs);
05934       __str.append(__size_type(1), __rhs);
05935       return __str;
05936     }
05937 
05938 #if __cplusplus >= 201103L
05939   template<typename _CharT, typename _Traits, typename _Alloc>
05940     inline basic_string<_CharT, _Traits, _Alloc>
05941     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
05942               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
05943     { return std::move(__lhs.append(__rhs)); }
05944 
05945   template<typename _CharT, typename _Traits, typename _Alloc>
05946     inline basic_string<_CharT, _Traits, _Alloc>
05947     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
05948               basic_string<_CharT, _Traits, _Alloc>&& __rhs)
05949     { return std::move(__rhs.insert(0, __lhs)); }
05950 
05951   template<typename _CharT, typename _Traits, typename _Alloc>
05952     inline basic_string<_CharT, _Traits, _Alloc>
05953     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
05954               basic_string<_CharT, _Traits, _Alloc>&& __rhs)
05955     {
05956       const auto __size = __lhs.size() + __rhs.size();
05957       const bool __cond = (__size > __lhs.capacity()
05958                            && __size <= __rhs.capacity());
05959       return __cond ? std::move(__rhs.insert(0, __lhs))
05960                     : std::move(__lhs.append(__rhs));
05961     }
05962 
05963   template<typename _CharT, typename _Traits, typename _Alloc>
05964     inline basic_string<_CharT, _Traits, _Alloc>
05965     operator+(const _CharT* __lhs,
05966               basic_string<_CharT, _Traits, _Alloc>&& __rhs)
05967     { return std::move(__rhs.insert(0, __lhs)); }
05968 
05969   template<typename _CharT, typename _Traits, typename _Alloc>
05970     inline basic_string<_CharT, _Traits, _Alloc>
05971     operator+(_CharT __lhs,
05972               basic_string<_CharT, _Traits, _Alloc>&& __rhs)
05973     { return std::move(__rhs.insert(0, 1, __lhs)); }
05974 
05975   template<typename _CharT, typename _Traits, typename _Alloc>
05976     inline basic_string<_CharT, _Traits, _Alloc>
05977     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
05978               const _CharT* __rhs)
05979     { return std::move(__lhs.append(__rhs)); }
05980 
05981   template<typename _CharT, typename _Traits, typename _Alloc>
05982     inline basic_string<_CharT, _Traits, _Alloc>
05983     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
05984               _CharT __rhs)
05985     { return std::move(__lhs.append(1, __rhs)); }
05986 #endif
05987 
05988   // operator ==
05989   /**
05990    *  @brief  Test equivalence of two strings.
05991    *  @param __lhs  First string.
05992    *  @param __rhs  Second string.
05993    *  @return  True if @a __lhs.compare(@a __rhs) == 0.  False otherwise.
05994    */
05995   template<typename _CharT, typename _Traits, typename _Alloc>
05996     inline bool
05997     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
05998                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
05999     _GLIBCXX_NOEXCEPT
06000     { return __lhs.compare(__rhs) == 0; }
06001 
06002   template<typename _CharT>
06003     inline
06004     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
06005     operator==(const basic_string<_CharT>& __lhs,
06006                const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPT
06007     { return (__lhs.size() == __rhs.size()
06008               && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
06009                                                     __lhs.size())); }
06010 
06011   /**
06012    *  @brief  Test equivalence of C string and string.
06013    *  @param __lhs  C string.
06014    *  @param __rhs  String.
06015    *  @return  True if @a __rhs.compare(@a __lhs) == 0.  False otherwise.
06016    */
06017   template<typename _CharT, typename _Traits, typename _Alloc>
06018     inline bool
06019     operator==(const _CharT* __lhs,
06020                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
06021     { return __rhs.compare(__lhs) == 0; }
06022 
06023   /**
06024    *  @brief  Test equivalence of string and C string.
06025    *  @param __lhs  String.
06026    *  @param __rhs  C string.
06027    *  @return  True if @a __lhs.compare(@a __rhs) == 0.  False otherwise.
06028    */
06029   template<typename _CharT, typename _Traits, typename _Alloc>
06030     inline bool
06031     operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
06032                const _CharT* __rhs)
06033     { return __lhs.compare(__rhs) == 0; }
06034 
06035   // operator !=
06036   /**
06037    *  @brief  Test difference of two strings.
06038    *  @param __lhs  First string.
06039    *  @param __rhs  Second string.
06040    *  @return  True if @a __lhs.compare(@a __rhs) != 0.  False otherwise.
06041    */
06042   template<typename _CharT, typename _Traits, typename _Alloc>
06043     inline bool
06044     operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
06045                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
06046     _GLIBCXX_NOEXCEPT
06047     { return !(__lhs == __rhs); }
06048 
06049   /**
06050    *  @brief  Test difference of C string and string.
06051    *  @param __lhs  C string.
06052    *  @param __rhs  String.
06053    *  @return  True if @a __rhs.compare(@a __lhs) != 0.  False otherwise.
06054    */
06055   template<typename _CharT, typename _Traits, typename _Alloc>
06056     inline bool
06057     operator!=(const _CharT* __lhs,
06058                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
06059     { return !(__lhs == __rhs); }
06060 
06061   /**
06062    *  @brief  Test difference of string and C string.
06063    *  @param __lhs  String.
06064    *  @param __rhs  C string.
06065    *  @return  True if @a __lhs.compare(@a __rhs) != 0.  False otherwise.
06066    */
06067   template<typename _CharT, typename _Traits, typename _Alloc>
06068     inline bool
06069     operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
06070                const _CharT* __rhs)
06071     { return !(__lhs == __rhs); }
06072 
06073   // operator <
06074   /**
06075    *  @brief  Test if string precedes string.
06076    *  @param __lhs  First string.
06077    *  @param __rhs  Second string.
06078    *  @return  True if @a __lhs precedes @a __rhs.  False otherwise.
06079    */
06080   template<typename _CharT, typename _Traits, typename _Alloc>
06081     inline bool
06082     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
06083               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
06084     _GLIBCXX_NOEXCEPT
06085     { return __lhs.compare(__rhs) < 0; }
06086 
06087   /**
06088    *  @brief  Test if string precedes C string.
06089    *  @param __lhs  String.
06090    *  @param __rhs  C string.
06091    *  @return  True if @a __lhs precedes @a __rhs.  False otherwise.
06092    */
06093   template<typename _CharT, typename _Traits, typename _Alloc>
06094     inline bool
06095     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
06096               const _CharT* __rhs)
06097     { return __lhs.compare(__rhs) < 0; }
06098 
06099   /**
06100    *  @brief  Test if C string precedes string.
06101    *  @param __lhs  C string.
06102    *  @param __rhs  String.
06103    *  @return  True if @a __lhs precedes @a __rhs.  False otherwise.
06104    */
06105   template<typename _CharT, typename _Traits, typename _Alloc>
06106     inline bool
06107     operator<(const _CharT* __lhs,
06108               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
06109     { return __rhs.compare(__lhs) > 0; }
06110 
06111   // operator >
06112   /**
06113    *  @brief  Test if string follows string.
06114    *  @param __lhs  First string.
06115    *  @param __rhs  Second string.
06116    *  @return  True if @a __lhs follows @a __rhs.  False otherwise.
06117    */
06118   template<typename _CharT, typename _Traits, typename _Alloc>
06119     inline bool
06120     operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
06121               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
06122     _GLIBCXX_NOEXCEPT
06123     { return __lhs.compare(__rhs) > 0; }
06124 
06125   /**
06126    *  @brief  Test if string follows C string.
06127    *  @param __lhs  String.
06128    *  @param __rhs  C string.
06129    *  @return  True if @a __lhs follows @a __rhs.  False otherwise.
06130    */
06131   template<typename _CharT, typename _Traits, typename _Alloc>
06132     inline bool
06133     operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
06134               const _CharT* __rhs)
06135     { return __lhs.compare(__rhs) > 0; }
06136 
06137   /**
06138    *  @brief  Test if C string follows string.
06139    *  @param __lhs  C string.
06140    *  @param __rhs  String.
06141    *  @return  True if @a __lhs follows @a __rhs.  False otherwise.
06142    */
06143   template<typename _CharT, typename _Traits, typename _Alloc>
06144     inline bool
06145     operator>(const _CharT* __lhs,
06146               const basic_string<_CharT, _Traits, _Alloc>& __rhs)
06147     { return __rhs.compare(__lhs) < 0; }
06148 
06149   // operator <=
06150   /**
06151    *  @brief  Test if string doesn't follow string.
06152    *  @param __lhs  First string.
06153    *  @param __rhs  Second string.
06154    *  @return  True if @a __lhs doesn't follow @a __rhs.  False otherwise.
06155    */
06156   template<typename _CharT, typename _Traits, typename _Alloc>
06157     inline bool
06158     operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
06159                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
06160     _GLIBCXX_NOEXCEPT
06161     { return __lhs.compare(__rhs) <= 0; }
06162 
06163   /**
06164    *  @brief  Test if string doesn't follow C string.
06165    *  @param __lhs  String.
06166    *  @param __rhs  C string.
06167    *  @return  True if @a __lhs doesn't follow @a __rhs.  False otherwise.
06168    */
06169   template<typename _CharT, typename _Traits, typename _Alloc>
06170     inline bool
06171     operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
06172                const _CharT* __rhs)
06173     { return __lhs.compare(__rhs) <= 0; }
06174 
06175   /**
06176    *  @brief  Test if C string doesn't follow string.
06177    *  @param __lhs  C string.
06178    *  @param __rhs  String.
06179    *  @return  True if @a __lhs doesn't follow @a __rhs.  False otherwise.
06180    */
06181   template<typename _CharT, typename _Traits, typename _Alloc>
06182     inline bool
06183     operator<=(const _CharT* __lhs,
06184                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
06185     { return __rhs.compare(__lhs) >= 0; }
06186 
06187   // operator >=
06188   /**
06189    *  @brief  Test if string doesn't precede string.
06190    *  @param __lhs  First string.
06191    *  @param __rhs  Second string.
06192    *  @return  True if @a __lhs doesn't precede @a __rhs.  False otherwise.
06193    */
06194   template<typename _CharT, typename _Traits, typename _Alloc>
06195     inline bool
06196     operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
06197                const basic_string<_CharT, _Traits, _Alloc>& __rhs)
06198     _GLIBCXX_NOEXCEPT
06199     { return __lhs.compare(__rhs) >= 0; }
06200 
06201   /**
06202    *  @brief  Test if string doesn't precede C string.
06203    *  @param __lhs  String.
06204    *  @param __rhs  C string.
06205    *  @return  True if @a __lhs doesn't precede @a __rhs.  False otherwise.
06206    */
06207   template<typename _CharT, typename _Traits, typename _Alloc>
06208     inline bool
06209     operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
06210                const _CharT* __rhs)
06211     { return __lhs.compare(__rhs) >= 0; }
06212 
06213   /**
06214    *  @brief  Test if C string doesn't precede string.
06215    *  @param __lhs  C string.
06216    *  @param __rhs  String.
06217    *  @return  True if @a __lhs doesn't precede @a __rhs.  False otherwise.
06218    */
06219   template<typename _CharT, typename _Traits, typename _Alloc>
06220     inline bool
06221     operator>=(const _CharT* __lhs,
06222              const basic_string<_CharT, _Traits, _Alloc>& __rhs)
06223     { return __rhs.compare(__lhs) <= 0; }
06224 
06225   /**
06226    *  @brief  Swap contents of two strings.
06227    *  @param __lhs  First string.
06228    *  @param __rhs  Second string.
06229    *
06230    *  Exchanges the contents of @a __lhs and @a __rhs in constant time.
06231    */
06232   template<typename _CharT, typename _Traits, typename _Alloc>
06233     inline void
06234     swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
06235          basic_string<_CharT, _Traits, _Alloc>& __rhs)
06236     _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
06237     { __lhs.swap(__rhs); }
06238 
06239 
06240   /**
06241    *  @brief  Read stream into a string.
06242    *  @param __is  Input stream.
06243    *  @param __str  Buffer to store into.
06244    *  @return  Reference to the input stream.
06245    *
06246    *  Stores characters from @a __is into @a __str until whitespace is
06247    *  found, the end of the stream is encountered, or str.max_size()
06248    *  is reached.  If is.width() is non-zero, that is the limit on the
06249    *  number of characters stored into @a __str.  Any previous
06250    *  contents of @a __str are erased.
06251    */
06252   template<typename _CharT, typename _Traits, typename _Alloc>
06253     basic_istream<_CharT, _Traits>&
06254     operator>>(basic_istream<_CharT, _Traits>& __is,
06255                basic_string<_CharT, _Traits, _Alloc>& __str);
06256 
06257   template<>
06258     basic_istream<char>&
06259     operator>>(basic_istream<char>& __is, basic_string<char>& __str);
06260 
06261   /**
06262    *  @brief  Write string to a stream.
06263    *  @param __os  Output stream.
06264    *  @param __str  String to write out.
06265    *  @return  Reference to the output stream.
06266    *
06267    *  Output characters of @a __str into os following the same rules as for
06268    *  writing a C string.
06269    */
06270   template<typename _CharT, typename _Traits, typename _Alloc>
06271     inline basic_ostream<_CharT, _Traits>&
06272     operator<<(basic_ostream<_CharT, _Traits>& __os,
06273                const basic_string<_CharT, _Traits, _Alloc>& __str)
06274     {
06275       // _GLIBCXX_RESOLVE_LIB_DEFECTS
06276       // 586. string inserter not a formatted function
06277       return __ostream_insert(__os, __str.data(), __str.size());
06278     }
06279 
06280   /**
06281    *  @brief  Read a line from stream into a string.
06282    *  @param __is  Input stream.
06283    *  @param __str  Buffer to store into.
06284    *  @param __delim  Character marking end of line.
06285    *  @return  Reference to the input stream.
06286    *
06287    *  Stores characters from @a __is into @a __str until @a __delim is
06288    *  found, the end of the stream is encountered, or str.max_size()
06289    *  is reached.  Any previous contents of @a __str are erased.  If
06290    *  @a __delim is encountered, it is extracted but not stored into
06291    *  @a __str.
06292    */
06293   template<typename _CharT, typename _Traits, typename _Alloc>
06294     basic_istream<_CharT, _Traits>&
06295     getline(basic_istream<_CharT, _Traits>& __is,
06296             basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
06297 
06298   /**
06299    *  @brief  Read a line from stream into a string.
06300    *  @param __is  Input stream.
06301    *  @param __str  Buffer to store into.
06302    *  @return  Reference to the input stream.
06303    *
06304    *  Stores characters from is into @a __str until &apos;\n&apos; is
06305    *  found, the end of the stream is encountered, or str.max_size()
06306    *  is reached.  Any previous contents of @a __str are erased.  If
06307    *  end of line is encountered, it is extracted but not stored into
06308    *  @a __str.
06309    */
06310   template<typename _CharT, typename _Traits, typename _Alloc>
06311     inline basic_istream<_CharT, _Traits>&
06312     getline(basic_istream<_CharT, _Traits>& __is,
06313             basic_string<_CharT, _Traits, _Alloc>& __str)
06314     { return std::getline(__is, __str, __is.widen('\n')); }
06315 
06316 #if __cplusplus >= 201103L
06317   /// Read a line from an rvalue stream into a string.
06318   template<typename _CharT, typename _Traits, typename _Alloc>
06319     inline basic_istream<_CharT, _Traits>&
06320     getline(basic_istream<_CharT, _Traits>&& __is,
06321             basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
06322     { return std::getline(__is, __str, __delim); }
06323 
06324   /// Read a line from an rvalue stream into a string.
06325   template<typename _CharT, typename _Traits, typename _Alloc>
06326     inline basic_istream<_CharT, _Traits>&
06327     getline(basic_istream<_CharT, _Traits>&& __is,
06328             basic_string<_CharT, _Traits, _Alloc>& __str)
06329     { return std::getline(__is, __str); }
06330 #endif
06331 
06332   template<>
06333     basic_istream<char>&
06334     getline(basic_istream<char>& __in, basic_string<char>& __str,
06335             char __delim);
06336 
06337 #ifdef _GLIBCXX_USE_WCHAR_T
06338   template<>
06339     basic_istream<wchar_t>&
06340     getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
06341             wchar_t __delim);
06342 #endif  
06343 
06344 _GLIBCXX_END_NAMESPACE_VERSION
06345 } // namespace
06346 
06347 #if __cplusplus >= 201103L
06348 
06349 #include <ext/string_conversions.h>
06350 
06351 namespace std _GLIBCXX_VISIBILITY(default)
06352 {
06353 _GLIBCXX_BEGIN_NAMESPACE_VERSION
06354 _GLIBCXX_BEGIN_NAMESPACE_CXX11
06355 
06356 #if _GLIBCXX_USE_C99_STDLIB
06357   // 21.4 Numeric Conversions [string.conversions].
06358   inline int
06359   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
06360   { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
06361                                         __idx, __base); }
06362 
06363   inline long
06364   stol(const string& __str, size_t* __idx = 0, int __base = 10)
06365   { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
06366                              __idx, __base); }
06367 
06368   inline unsigned long
06369   stoul(const string& __str, size_t* __idx = 0, int __base = 10)
06370   { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
06371                              __idx, __base); }
06372 
06373   inline long long
06374   stoll(const string& __str, size_t* __idx = 0, int __base = 10)
06375   { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
06376                              __idx, __base); }
06377 
06378   inline unsigned long long
06379   stoull(const string& __str, size_t* __idx = 0, int __base = 10)
06380   { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
06381                              __idx, __base); }
06382 
06383   // NB: strtof vs strtod.
06384   inline float
06385   stof(const string& __str, size_t* __idx = 0)
06386   { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
06387 
06388   inline double
06389   stod(const string& __str, size_t* __idx = 0)
06390   { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
06391 
06392   inline long double
06393   stold(const string& __str, size_t* __idx = 0)
06394   { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
06395 #endif // _GLIBCXX_USE_C99_STDLIB
06396 
06397 #if _GLIBCXX_USE_C99_STDIO
06398   // NB: (v)snprintf vs sprintf.
06399 
06400   // DR 1261.
06401   inline string
06402   to_string(int __val)
06403   { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(int),
06404                                            "%d", __val); }
06405 
06406   inline string
06407   to_string(unsigned __val)
06408   { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
06409                                            4 * sizeof(unsigned),
06410                                            "%u", __val); }
06411 
06412   inline string
06413   to_string(long __val)
06414   { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, 4 * sizeof(long),
06415                                            "%ld", __val); }
06416 
06417   inline string
06418   to_string(unsigned long __val)
06419   { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
06420                                            4 * sizeof(unsigned long),
06421                                            "%lu", __val); }
06422 
06423   inline string
06424   to_string(long long __val)
06425   { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
06426                                            4 * sizeof(long long),
06427                                            "%lld", __val); }
06428 
06429   inline string
06430   to_string(unsigned long long __val)
06431   { return __gnu_cxx::__to_xstring<string>(&std::vsnprintf,
06432                                            4 * sizeof(unsigned long long),
06433                                            "%llu", __val); }
06434 
06435   inline string
06436   to_string(float __val)
06437   {
06438     const int __n = 
06439       __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
06440     return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
06441                                            "%f", __val);
06442   }
06443 
06444   inline string
06445   to_string(double __val)
06446   {
06447     const int __n = 
06448       __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
06449     return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
06450                                            "%f", __val);
06451   }
06452 
06453   inline string
06454   to_string(long double __val)
06455   {
06456     const int __n = 
06457       __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
06458     return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
06459                                            "%Lf", __val);
06460   }
06461 #endif // _GLIBCXX_USE_C99_STDIO
06462 
06463 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
06464   inline int 
06465   stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
06466   { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
06467                                         __idx, __base); }
06468 
06469   inline long 
06470   stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
06471   { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
06472                              __idx, __base); }
06473 
06474   inline unsigned long
06475   stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
06476   { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
06477                              __idx, __base); }
06478 
06479   inline long long
06480   stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
06481   { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
06482                              __idx, __base); }
06483 
06484   inline unsigned long long
06485   stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
06486   { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
06487                              __idx, __base); }
06488 
06489   // NB: wcstof vs wcstod.
06490   inline float
06491   stof(const wstring& __str, size_t* __idx = 0)
06492   { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
06493 
06494   inline double
06495   stod(const wstring& __str, size_t* __idx = 0)
06496   { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
06497 
06498   inline long double
06499   stold(const wstring& __str, size_t* __idx = 0)
06500   { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
06501 
06502 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
06503   // DR 1261.
06504   inline wstring
06505   to_wstring(int __val)
06506   { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
06507                                             L"%d", __val); }
06508 
06509   inline wstring
06510   to_wstring(unsigned __val)
06511   { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
06512                                             4 * sizeof(unsigned),
06513                                             L"%u", __val); }
06514 
06515   inline wstring
06516   to_wstring(long __val)
06517   { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
06518                                             L"%ld", __val); }
06519 
06520   inline wstring
06521   to_wstring(unsigned long __val)
06522   { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
06523                                             4 * sizeof(unsigned long),
06524                                             L"%lu", __val); }
06525 
06526   inline wstring
06527   to_wstring(long long __val)
06528   { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
06529                                             4 * sizeof(long long),
06530                                             L"%lld", __val); }
06531 
06532   inline wstring
06533   to_wstring(unsigned long long __val)
06534   { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
06535                                             4 * sizeof(unsigned long long),
06536                                             L"%llu", __val); }
06537 
06538   inline wstring
06539   to_wstring(float __val)
06540   {
06541     const int __n =
06542       __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
06543     return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
06544                                             L"%f", __val);
06545   }
06546 
06547   inline wstring
06548   to_wstring(double __val)
06549   {
06550     const int __n =
06551       __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
06552     return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
06553                                             L"%f", __val);
06554   }
06555 
06556   inline wstring
06557   to_wstring(long double __val)
06558   {
06559     const int __n =
06560       __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
06561     return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
06562                                             L"%Lf", __val);
06563   }
06564 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
06565 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
06566 
06567 _GLIBCXX_END_NAMESPACE_CXX11
06568 _GLIBCXX_END_NAMESPACE_VERSION
06569 } // namespace
06570 
06571 #endif /* C++11 */
06572 
06573 #if __cplusplus >= 201103L
06574 
06575 #include <bits/functional_hash.h>
06576 
06577 namespace std _GLIBCXX_VISIBILITY(default)
06578 {
06579 _GLIBCXX_BEGIN_NAMESPACE_VERSION
06580 
06581   // DR 1182.
06582 
06583 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
06584   /// std::hash specialization for string.
06585   template<>
06586     struct hash<string>
06587     : public __hash_base<size_t, string>
06588     {
06589       size_t
06590       operator()(const string& __s) const noexcept
06591       { return std::_Hash_impl::hash(__s.data(), __s.length()); }
06592     };
06593 
06594   template<>
06595     struct __is_fast_hash<hash<string>> : std::false_type
06596     { };
06597 
06598 #ifdef _GLIBCXX_USE_WCHAR_T
06599   /// std::hash specialization for wstring.
06600   template<>
06601     struct hash<wstring>
06602     : public __hash_base<size_t, wstring>
06603     {
06604       size_t
06605       operator()(const wstring& __s) const noexcept
06606       { return std::_Hash_impl::hash(__s.data(),
06607                                      __s.length() * sizeof(wchar_t)); }
06608     };
06609 
06610   template<>
06611     struct __is_fast_hash<hash<wstring>> : std::false_type
06612     { };
06613 #endif
06614 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
06615 
06616 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
06617   /// std::hash specialization for u16string.
06618   template<>
06619     struct hash<u16string>
06620     : public __hash_base<size_t, u16string>
06621     {
06622       size_t
06623       operator()(const u16string& __s) const noexcept
06624       { return std::_Hash_impl::hash(__s.data(),
06625                                      __s.length() * sizeof(char16_t)); }
06626     };
06627 
06628   template<>
06629     struct __is_fast_hash<hash<u16string>> : std::false_type
06630     { };
06631 
06632   /// std::hash specialization for u32string.
06633   template<>
06634     struct hash<u32string>
06635     : public __hash_base<size_t, u32string>
06636     {
06637       size_t
06638       operator()(const u32string& __s) const noexcept
06639       { return std::_Hash_impl::hash(__s.data(),
06640                                      __s.length() * sizeof(char32_t)); }
06641     };
06642 
06643   template<>
06644     struct __is_fast_hash<hash<u32string>> : std::false_type
06645     { };
06646 #endif
06647 
06648 _GLIBCXX_END_NAMESPACE_VERSION
06649 
06650 #if __cplusplus > 201103L
06651 
06652 #define __cpp_lib_string_udls 201304
06653 
06654   inline namespace literals
06655   {
06656   inline namespace string_literals
06657   {
06658 _GLIBCXX_BEGIN_NAMESPACE_VERSION
06659 
06660     _GLIBCXX_DEFAULT_ABI_TAG
06661     inline basic_string<char>
06662     operator""s(const char* __str, size_t __len)
06663     { return basic_string<char>{__str, __len}; }
06664 
06665 #ifdef _GLIBCXX_USE_WCHAR_T
06666     _GLIBCXX_DEFAULT_ABI_TAG
06667     inline basic_string<wchar_t>
06668     operator""s(const wchar_t* __str, size_t __len)
06669     { return basic_string<wchar_t>{__str, __len}; }
06670 #endif
06671 
06672 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
06673     _GLIBCXX_DEFAULT_ABI_TAG
06674     inline basic_string<char16_t>
06675     operator""s(const char16_t* __str, size_t __len)
06676     { return basic_string<char16_t>{__str, __len}; }
06677 
06678     _GLIBCXX_DEFAULT_ABI_TAG
06679     inline basic_string<char32_t>
06680     operator""s(const char32_t* __str, size_t __len)
06681     { return basic_string<char32_t>{__str, __len}; }
06682 #endif
06683 
06684 _GLIBCXX_END_NAMESPACE_VERSION
06685   } // inline namespace string_literals
06686   } // inline namespace literals
06687 
06688 #endif // __cplusplus > 201103L
06689 
06690 } // namespace std
06691 
06692 #endif // C++11
06693 
06694 #endif /* _BASIC_STRING_H */