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