libstdc++

basic_string.h

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