19720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block/*
29720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Copyright (c) 1994
39720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Hewlett-Packard Company
49720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block *
59720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Copyright (c) 1996,1997
69720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Silicon Graphics Computer Systems, Inc.
79720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block *
89720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Copyright (c) 1997
99720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Moscow Center for SPARC Technology
109720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block *
119720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Copyright (c) 1999
129720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Boris Fomitchev
139720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block *
149720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * This material is provided "as is", with absolutely no warranty expressed
159720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * or implied. Any use is at your own risk.
169720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block *
179720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Permission to use or copy this software for any purpose is hereby granted
189720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * without fee, provided the above notices are retained on all copies.
199720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * Permission to modify the code and to distribute modified code is granted,
209720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * provided the above notices are retained, and a notice that the code was
219720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block * modified is included with the above copyright notice.
229720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block */
23e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
249720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#ifndef _STLP_STRING_C
259720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#define _STLP_STRING_C
269720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
279720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#ifndef _STLP_INTERNAL_STRING_H
289720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#  include <stl/_string.h>
299720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#endif
309720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
319720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#ifndef _STLP_INTERNAL_CTRAITS_FUNCTIONS_H
329720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#  include <stl/_ctraits_fns.h>
339720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#endif
349720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
35e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#ifndef _STLP_INTERNAL_FUNCTION_H
36e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#  include <stl/_function.h>
37e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#endif
38e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
399720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
409720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#  define basic_string _STLP_NO_MEM_T_NAME(str)
419720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#elif defined (_STLP_DEBUG)
429720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#  define basic_string _STLP_NON_DBG_NAME(str)
439720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#endif
449720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
459720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#if defined (_STLP_NESTED_TYPE_PARAM_BUG)
469720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#  define __size_type__ size_t
479720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#  define size_type size_t
489720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#  define iterator _CharT*
499720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#else
509720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#  define __size_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_string<_CharT,_Traits,_Alloc>::size_type
519720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#endif
529720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
539720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block_STLP_BEGIN_NAMESPACE
549720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
559720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block_STLP_MOVE_TO_PRIV_NAMESPACE
569720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
579720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// A helper class to use a char_traits as a function object.
589720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _Traits>
599720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockstruct _Not_within_traits : public unary_function<typename _Traits::char_type, bool> {
609720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  typedef typename _Traits::char_type _CharT;
619720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const _CharT* _M_first;
629720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const _CharT* _M_last;
639720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
649720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _Not_within_traits(const _CharT* __f, const _CharT* __l)
659720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    : _M_first(__f), _M_last(__l) {}
669720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
679720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  bool operator()(const _CharT& __x) const {
689720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return find_if(_M_first, _M_last,
699720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                   _STLP_PRIV _Eq_char_bound<_Traits>(__x)) == _M_last;
709720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
719720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block};
729720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
73e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scotttemplate <class _InputIter, class _CharT, class _Traits>
74e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottinline _InputIter __str_find_first_of_aux(_InputIter __first1, _InputIter __last1,
75e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                          const _CharT* __first2, const _CharT* __last2,
76e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                          _Traits*, const __true_type& /* _STLportTraits */)
77e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott{ return __find_first_of(__first1, __last1, __first2, __last2); }
78e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
79e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scotttemplate <class _InputIter, class _CharT, class _Traits>
80e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottinline _InputIter __str_find_first_of_aux(_InputIter __first1, _InputIter __last1,
81e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                          const _CharT* __first2, const _CharT* __last2,
82e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                          _Traits*, const __false_type& /* _STLportTraits */)
83e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott{ return __find_first_of(__first1, __last1, __first2, __last2, _STLP_PRIV _Eq_traits<_Traits>()); }
84e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
85e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scotttemplate <class _InputIter, class _CharT, class _Traits>
86e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottinline _InputIter __str_find_first_of(_InputIter __first1, _InputIter __last1,
87e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                      const _CharT* __first2, const _CharT* __last2,
88e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                      _Traits* __traits) {
89e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#if !defined (__BORLANDC__)
90e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  typedef typename _IsSTLportClass<_Traits>::_Ret _STLportTraits;
91e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#else
92e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  enum { _Is = _IsSTLportClass<_Traits>::_Is };
93e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  typedef typename __bool2type<_Is>::_Ret _STLportTraits;
94e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#endif
95e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  return __str_find_first_of_aux(__first1, __last1, __first2, __last2, __traits, _STLportTraits());
96e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott}
97e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
98e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scotttemplate <class _InputIter, class _CharT, class _Traits>
99e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottinline _InputIter __str_find_first_not_of_aux3(_InputIter __first1, _InputIter __last1,
100e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                               const _CharT* __first2, const _CharT* __last2,
101e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                               _Traits* /* __traits */, const __true_type& __useStrcspnLikeAlgo)
102e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott{ return __find_first_of_aux2(__first1, __last1, __first2, __last2, __first2, not1(_Identity<bool>()), __useStrcspnLikeAlgo); }
103e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
104e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scotttemplate <class _InputIter, class _CharT, class _Traits>
105e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottinline _InputIter __str_find_first_not_of_aux3(_InputIter __first1, _InputIter __last1,
106e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                               const _CharT* __first2, const _CharT* __last2,
107e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                               _Traits* /* __traits */, const __false_type& /* _UseStrcspnLikeAlgo */)
108e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott{ return _STLP_STD::find_if(__first1, __last1, _STLP_PRIV _Not_within_traits<_Traits>(__first2, __last2)); }
109e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
110e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scotttemplate <class _InputIter, class _CharT, class _Tp, class _Traits>
111e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottinline _InputIter __str_find_first_not_of_aux2(_InputIter __first1, _InputIter __last1,
112e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                               const _CharT* __first2, const _CharT* __last2,
113a721beb6d7372334152406a36e8db016650a9691Tareq A. Siraj                                               _Tp* _STLP_UNUSED(__pt), _Traits* __traits) {
114e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  typedef typename _IsIntegral<_Tp>::_Ret _IsIntegral;
115e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  typedef typename _IsCharLikeType<_CharT>::_Ret _IsCharLike;
116e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  typedef typename _Land2<_IsIntegral, _IsCharLike>::_Ret _UseStrcspnLikeAlgo;
117e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  return __str_find_first_not_of_aux3(__first1, __last1, __first2, __last2, __traits, _UseStrcspnLikeAlgo());
118e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott}
119e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
120e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scotttemplate <class _InputIter, class _CharT, class _Traits>
121e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottinline _InputIter __str_find_first_not_of_aux1(_InputIter __first1, _InputIter __last1,
122e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                               const _CharT* __first2, const _CharT* __last2,
123e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                               _Traits* __traits, const __true_type& /* _STLportTraits */)
124e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott{ return __str_find_first_not_of_aux2(__first1, __last1, __first2, __last2,
125e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                      _STLP_VALUE_TYPE(__first1, _InputIter), __traits); }
126e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
127e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scotttemplate <class _InputIter, class _CharT, class _Traits>
128e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottinline _InputIter __str_find_first_not_of_aux1(_InputIter __first1, _InputIter __last1,
129e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                               const _CharT* __first2, const _CharT* __last2,
130e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                               _Traits*, const __false_type& /* _STLportTraits */)
131e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott{ return _STLP_STD::find_if(__first1, __last1, _STLP_PRIV _Not_within_traits<_Traits>(__first2, __last2)); }
132e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
133e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scotttemplate <class _InputIter, class _CharT, class _Traits>
134e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottinline _InputIter __str_find_first_not_of(_InputIter __first1, _InputIter __last1,
135e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                          const _CharT* __first2, const _CharT* __last2,
136e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                          _Traits* __traits) {
137e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#if !defined (__BORLANDC__)
138e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  typedef typename _IsSTLportClass<_Traits>::_Ret _STLportTraits;
139e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#else
140e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  enum { _Is = _IsSTLportClass<_Traits>::_Is };
141e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  typedef typename __bool2type<_Is>::_Ret _STLportTraits;
142e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#endif
143e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  return __str_find_first_not_of_aux1(__first1, __last1, __first2, __last2, __traits, _STLportTraits());
144e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott}
145e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
1469720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// ------------------------------------------------------------
1479720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// Non-inline declarations.
1489720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
1499720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#if !defined (basic_string)
1509720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block_STLP_MOVE_TO_STD_NAMESPACE
1519720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#endif
1529720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
1539720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// Change the string's capacity so that it is large enough to hold
1549720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block//  at least __res_arg elements, plus the terminating _CharT().  Note that,
1559720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block//  if __res_arg < capacity(), this member function may actually decrease
1569720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block//  the string's capacity.
1579720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
1589720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockvoid basic_string<_CharT,_Traits,_Alloc>::reserve(size_type __res_arg) {
1599720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if (__res_arg > max_size())
1609720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    this->_M_throw_length_error();
1619720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
1629720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  size_type __n = (max)(__res_arg, size()) + 1;
163e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  if (__n < this->_M_capacity())
1649720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return;
1659720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
166e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  _M_reserve(__n);
167e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott}
1689720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
169e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scotttemplate <class _CharT, class _Traits, class _Alloc>
170e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scottvoid basic_string<_CharT,_Traits,_Alloc>::_M_reserve(size_type __n) {
171e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  pointer __new_start = this->_M_start_of_storage.allocate(__n, __n);
172e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  pointer __new_finish = _STLP_PRIV __ucopy(this->_M_Start(), this->_M_Finish(), __new_start);
173e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  _M_construct_null(__new_finish);
1749720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  this->_M_deallocate_block();
1759720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  this->_M_reset(__new_start, __new_finish, __new_start + __n);
1769720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
1779720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
1789720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
1799720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc>&
1809720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc>::append(size_type __n, _CharT __c) {
1819720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if (__n > 0) {
182e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    if (__n > max_size() - size())
183e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      this->_M_throw_length_error();
184e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    if (__n >= this->_M_rest())
185e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      _M_reserve(_M_compute_next_size(__n));
1869720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _STLP_PRIV __uninitialized_fill_n(this->_M_finish + 1, __n - 1, __c);
187e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    _M_construct_null(this->_M_finish + __n);
1889720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _Traits::assign(*end(), __c);
1899720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    this->_M_finish += __n;
1909720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
1919720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  return *this;
1929720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
1939720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
1949720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
1959720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT, _Traits, _Alloc>&
1969720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT, _Traits, _Alloc>::_M_append(const _CharT* __first, const _CharT* __last) {
1979720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if (__first != __last) {
198e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    size_type __n = __STATIC_CAST(size_type, __last - __first);
199e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    if (__n >= this->_M_rest()) {
200e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      size_type __len = _M_compute_next_size(__n);
201e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      pointer __new_start = this->_M_start_of_storage.allocate(__len, __len);
202e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      pointer __new_finish = _STLP_PRIV __ucopy(this->_M_Start(), this->_M_Finish(), __new_start);
203e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      __new_finish = _STLP_PRIV __ucopy(__first, __last, __new_finish);
204e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      _M_construct_null(__new_finish);
2059720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_deallocate_block();
2069720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_reset(__new_start, __new_finish, __new_start + __len);
2079720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
2089720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    else {
2099720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      const _CharT* __f1 = __first;
2109720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      ++__f1;
2119720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      _STLP_PRIV __ucopy(__f1, __last, this->_M_finish + 1);
212e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      _M_construct_null(this->_M_finish + __n);
2139720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      _Traits::assign(*end(), *__first);
2149720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_finish += __n;
2159720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
2169720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
2179720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  return *this;
2189720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
2199720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
2209720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
2219720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc>&
2229720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc>::assign(size_type __n, _CharT __c) {
2239720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if (__n <= size()) {
2249720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _Traits::assign(this->_M_Start(), __n, __c);
2259720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    erase(begin() + __n, end());
2269720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
2279720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  else {
2289720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    if (__n < capacity()) {
2299720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      _Traits::assign(this->_M_Start(), size(), __c);
2309720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      append(__n - size(), __c);
2319720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
2329720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    else {
2339720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      _Self __str(__n, __c);
2349720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->swap(__str);
2359720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
2369720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
2379720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  return *this;
2389720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
2399720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
2409720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
2419720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc>&
2429720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc>::_M_assign(const _CharT* __f, const _CharT* __l) {
2439720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  ptrdiff_t __n = __l - __f;
2449720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if (__STATIC_CAST(size_type, __n) <= size()) {
245236ba458f6efaaa919c4b7d342497bfb25e442daEvgeniy Stepanov    _Traits::move(this->_M_Start(), __f, __n);
2469720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    erase(begin() + __n, end());
2479720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
2489720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  else {
249236ba458f6efaaa919c4b7d342497bfb25e442daEvgeniy Stepanov    _Traits::move(this->_M_Start(), __f, size());
2509720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _M_append(__f + size(), __l);
2519720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
2529720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  return *this;
2539720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
2549720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
2559720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
2569720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block_CharT* basic_string<_CharT,_Traits,_Alloc> ::_M_insert_aux(_CharT* __p,
2579720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                                            _CharT __c) {
2589720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  pointer __new_pos = __p;
259e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  if (this->_M_rest() > 1 ) {
2609720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _M_construct_null(this->_M_finish + 1);
2619720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _Traits::move(__p + 1, __p, this->_M_finish - __p);
2629720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _Traits::assign(*__p, __c);
2639720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    ++this->_M_finish;
2649720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
2659720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  else {
266e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    size_type __len = _M_compute_next_size(1);
267e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    pointer __new_start = this->_M_start_of_storage.allocate(__len, __len);
268e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    __new_pos = _STLP_PRIV __ucopy(this->_M_Start(), __p, __new_start);
269e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    _Traits::assign(*__new_pos, __c);
270e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    pointer __new_finish = __new_pos + 1;
271e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    __new_finish = _STLP_PRIV __ucopy(__p, this->_M_finish, __new_finish);
272e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    _M_construct_null(__new_finish);
2739720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    this->_M_deallocate_block();
2749720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    this->_M_reset(__new_start, __new_finish, __new_start + __len);
2759720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
2769720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  return __new_pos;
2779720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
2789720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
2799720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
2809720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockvoid basic_string<_CharT,_Traits,_Alloc>::insert(iterator __pos,
2819720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                                 size_t __n, _CharT __c) {
2829720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if (__n != 0) {
283e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    if (this->_M_rest() > __n) {
2849720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      const size_type __elems_after = this->_M_finish - __pos;
2859720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      pointer __old_finish = this->_M_finish;
2869720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      if (__elems_after >= __n) {
287e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott        _STLP_PRIV __ucopy((this->_M_finish - __n) + 1, this->_M_finish + 1, this->_M_finish + 1);
2889720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        this->_M_finish += __n;
2899720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        _Traits::move(__pos + __n, __pos, (__elems_after - __n) + 1);
2909720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        _Traits::assign(__pos, __n, __c);
2919720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      }
2929720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      else {
2939720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        _STLP_PRIV __uninitialized_fill_n(this->_M_finish + 1, __n - __elems_after - 1, __c);
2949720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        this->_M_finish += __n - __elems_after;
295e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott        _STLP_PRIV __ucopy(__pos, __old_finish + 1, this->_M_finish);
296e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott        this->_M_finish += __elems_after;
2979720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        _Traits::assign(__pos, __elems_after + 1, __c);
2989720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      }
2999720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
3009720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    else {
301e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      size_type __len = _M_compute_next_size(__n);
302e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      pointer __new_start = this->_M_start_of_storage.allocate(__len, __len);
303e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      pointer __new_finish = _STLP_PRIV __ucopy(this->_M_Start(), __pos, __new_start);
304e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      __new_finish = _STLP_PRIV __uninitialized_fill_n(__new_finish, __n, __c);
305e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      __new_finish = _STLP_PRIV __ucopy(__pos, this->_M_finish, __new_finish);
306e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      _M_construct_null(__new_finish);
3079720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_deallocate_block();
3089720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_reset(__new_start, __new_finish, __new_start + __len);
3099720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
3109720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
3119720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
3129720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
3139720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
3149720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockvoid basic_string<_CharT,_Traits,_Alloc>::_M_insert(iterator __pos,
3159720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                                    const _CharT* __first, const _CharT* __last,
3169720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                                    bool __self_ref) {
3179720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  //this version has to take care about the auto referencing
3189720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if (__first != __last) {
319e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    const size_t __n = __last - __first;
320e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    if (this->_M_rest() > __n) {
321e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      const size_t __elems_after = this->_M_finish - __pos;
3229720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      pointer __old_finish = this->_M_finish;
3239720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      if (__elems_after >= __n) {
3249720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        _STLP_PRIV __ucopy((this->_M_finish - __n) + 1, this->_M_finish + 1, this->_M_finish + 1);
3259720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        this->_M_finish += __n;
3269720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        _Traits::move(__pos + __n, __pos, (__elems_after - __n) + 1);
3279720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        if (!__self_ref || __last < __pos) {
3289720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block          _M_copy(__first, __last, __pos);
3299720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        }
3309720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        else {
3319720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block          //We have to check that the source buffer hasn't move
3329720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block          if (__first >= __pos) {
3339720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block            //The source buffer has move
3349720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block            __first += __n;
3359720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block            __last += __n;
3369720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block            _M_copy(__first, __last, __pos);
3379720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block          }
3389720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block          else {
3399720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block            //The source buffer hasn't move, it has been duplicated
3409720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block            _M_move(__first, __last, __pos);
3419720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block          }
3429720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        }
3439720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      }
3449720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      else {
3459720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        const_iterator __mid = __first;
3469720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        __mid += __elems_after + 1;
3479720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        _STLP_PRIV __ucopy(__mid, __last, this->_M_finish + 1);
3489720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        this->_M_finish += __n - __elems_after;
349e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott        _STLP_PRIV __ucopy(__pos, __old_finish + 1, this->_M_finish);
350e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott        this->_M_finish += __elems_after;
3519720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        if (!__self_ref)
3529720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block          _M_copy(__first, __mid, __pos);
3539720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block        else
3549720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block          _M_move(__first, __mid, __pos);
3559720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      }
3569720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
3579720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    else {
358e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      size_type __len = _M_compute_next_size(__n);
359e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      pointer __new_start = this->_M_start_of_storage.allocate(__len, __len);
360e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      pointer __new_finish = _STLP_PRIV __ucopy(this->_M_Start(), __pos, __new_start);
361e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      __new_finish = _STLP_PRIV __ucopy(__first, __last, __new_finish);
362e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      __new_finish = _STLP_PRIV __ucopy(__pos, this->_M_finish, __new_finish);
363e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      _M_construct_null(__new_finish);
3649720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_deallocate_block();
3659720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      this->_M_reset(__new_start, __new_finish, __new_start + __len);
3669720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
3679720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
3689720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
3699720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
3709720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
3719720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc>&
3729720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc> ::replace(iterator __first, iterator __last,
3739720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                              size_type __n, _CharT __c) {
3749720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  size_type __len = (size_type)(__last - __first);
3759720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
3769720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if (__len >= __n) {
3779720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _Traits::assign(__first, __n, __c);
3789720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    erase(__first + __n, __last);
3799720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
3809720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  else {
3819720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _Traits::assign(__first, __len, __c);
3829720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    insert(__last, __n - __len, __c);
3839720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
3849720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  return *this;
3859720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
3869720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
3879720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
3889720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc>&
3899720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc> ::_M_replace(iterator __first, iterator __last,
3909720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                                 const _CharT* __f, const _CharT* __l,
3919720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                                 bool __self_ref) {
3929720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const ptrdiff_t       __n = __l - __f;
3939720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const difference_type __len = __last - __first;
3949720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if (__len >= __n) {
3959720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    if (!__self_ref || __l < __first || __f >= __last)
3969720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      _M_copy(__f, __l, __first);
3979720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    else
3989720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block      _M_move(__f, __l, __first);
3999720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    erase(__first + __n, __last);
400e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  } else if (!__self_ref || (__f >= __last) || (__l <= __first)) { // no overlap
401e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    const_iterator __m = __f + __len;
402e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    _M_copy(__f, __m, __first);
403e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    _M_insert(__last, __m, __l, __self_ref );
404e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  } else if (__f < __first) { // we have to take care of overlaping
405e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    const_iterator __m = __f + __len;
406e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    // We have to deal with possible reallocation because we do insert first.
407e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    const difference_type __off_dest = __first - this->begin();
408e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    const difference_type __off_src = __f - this->begin();
409e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    _M_insert(__last, __m, __l, true);
410e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    _Traits::move(begin() + __off_dest, begin() + __off_src, __len);
411e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  } else {
412e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    const_iterator __m = __f + __len;
413e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    _Traits::move(__first, __f, __len);
414e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    _M_insert(__last, __m, __l, true);
4159720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
4169720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  return *this;
4179720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
4189720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
419e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scotttemplate <class _CharT, class _Traits, class _Alloc>
420e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott__size_type__ basic_string<_CharT,_Traits,_Alloc>::find( const _CharT* __s, size_type __pos,
421e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                                         size_type __n) const
422e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott{
4239720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const size_t __len = size();
424e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  if (__pos >= __len || __pos + __n > __len) {
425e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    if ( __n == 0 && __pos <= __len ) { // marginal case
426e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      return __pos;
427e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    }
4289720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return npos;
4299720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
430e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
431e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  const_pointer __result =
432e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    _STLP_STD::search(this->_M_Start() + __pos, this->_M_Finish(),
433e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                      __s, __s + __n, _STLP_PRIV _Eq_traits<_Traits>());
434e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  return __result != this->_M_Finish() ? __result - this->_M_Start() : npos;
4359720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
4369720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
437e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scotttemplate <class _CharT, class _Traits, class _Alloc>
438e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott__size_type__ basic_string<_CharT,_Traits,_Alloc>::find(_CharT __c, size_type __pos) const
439e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott{
440e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  if (__pos >= size()) { /*__pos + 1 > size()*/
4419720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return npos;
4429720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
443e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
444e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  const_pointer __result =
445e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    _STLP_STD::find_if(this->_M_Start() + __pos, this->_M_Finish(),
446e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                       _STLP_PRIV _Eq_char_bound<_Traits>(__c));
447e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  return __result != this->_M_Finish() ? __result - this->_M_Start() : npos;
4489720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
4499720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
4509720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
451e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott__size_type__ basic_string<_CharT,_Traits,_Alloc>::rfind(const _CharT* __s, size_type __pos,
452e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                                         size_type __n) const
4539720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block{
4549720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const size_type __len = size();
4559720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if ( __len < __n ) {
4569720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return npos;
4579720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
4589720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const_pointer __last = this->_M_Start() + (min)( __len - __n, __pos) + __n;
459e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  if ( __n == 0 ) { // marginal case
460e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    return __last - this->_M_Start();
461e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  }
462e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott  const_pointer __result = _STLP_STD::find_end(this->_M_Start(), __last,
463e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                               __s, __s + __n, _STLP_PRIV _Eq_traits<_Traits>());
4649720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  return __result != __last ? __result - this->_M_Start() : npos;
4659720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
4669720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
4679720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
468e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott__size_type__ basic_string<_CharT,_Traits,_Alloc>::rfind(_CharT __c, size_type __pos) const
4699720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block{
4709720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const size_type __len = size();
4719720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if ( __len < 1 ) {
4729720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return npos;
4739720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
4749720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;
4759720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const_reverse_iterator __rresult =
4769720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _STLP_STD::find_if(const_reverse_iterator(__last), rend(),
4779720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                       _STLP_PRIV _Eq_char_bound<_Traits>(__c));
4789720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
4799720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
4809720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
4819720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc> __size_type__
4829720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc> ::find_first_of(const _CharT* __s, size_type __pos,
4839720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                                    size_type __n) const {
4849720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if (__pos >= size()) /*__pos + 1 > size()*/
4859720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return npos;
4869720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  else {
487e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    const_iterator __result = _STLP_PRIV __str_find_first_of(begin() + __pos, end(),
488e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                                             __s, __s + __n,
489e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                                             __STATIC_CAST(_Traits*, 0));
4909720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return __result != end() ? __result - begin() : npos;
4919720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
4929720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
4939720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
4949720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
4959720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block __size_type__
4969720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc> ::find_last_of(const _CharT* __s, size_type __pos,
4979720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                                   size_type __n) const
4989720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block{
4999720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const size_type __len = size();
5009720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if ( __len < 1 ) {
5019720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return npos;
5029720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
5039720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;
5049720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const const_reverse_iterator __rresult =
505e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    _STLP_PRIV __str_find_first_of(const_reverse_iterator(__last), rend(),
506e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                   __s, __s + __n,
507e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                   __STATIC_CAST(_Traits*, 0));
5089720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
5099720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
5109720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
5119720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
5129720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc> __size_type__
5139720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc> ::find_first_not_of(const _CharT* __s, size_type __pos,
5149720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                                        size_type __n) const {
5159720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  typedef typename _Traits::char_type _CharType;
5169720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if (__pos >= size()) /*__pos + 1 >= size()*/
5179720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return npos;
5189720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  else {
519e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    const_pointer __result = _STLP_PRIV __str_find_first_not_of(this->_M_Start() + __pos, this->_M_Finish(),
520e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                                                __STATIC_CAST(const _CharType*, __s),
521e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                                                __STATIC_CAST(const _CharType*, __s) + __n,
522e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                                                __STATIC_CAST(_Traits*, 0));
5239720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return __result != this->_M_finish ? __result - this->_M_Start() : npos;
5249720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
5259720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
5269720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
5279720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc> __size_type__
5289720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc> ::find_first_not_of(_CharT __c, size_type __pos) const {
5299720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if (1 > size())
5309720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return npos;
5319720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  else {
5329720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    const_pointer __result = _STLP_STD::find_if(this->_M_Start() + __pos, this->_M_Finish(),
5339720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                                _STLP_PRIV _Neq_char_bound<_Traits>(__c));
5349720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return __result != this->_M_finish ? __result - this->_M_Start() : npos;
5359720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
5369720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
5379720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
5389720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
5399720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block__size_type__
5409720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT,_Traits,_Alloc>::find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
5419720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block{
5429720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  typedef typename _Traits::char_type _CharType;
5439720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const size_type __len = size();
5449720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if ( __len < 1 ) {
5459720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return npos;
5469720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
5479720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;
5489720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const_reverse_iterator __rlast = const_reverse_iterator(__last);
5499720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const_reverse_iterator __rresult =
550e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    _STLP_PRIV __str_find_first_not_of(__rlast, rend(),
551e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                       __STATIC_CAST(const _CharType*, __s),
552e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                       __STATIC_CAST(const _CharType*, __s) + __n,
553e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott                                       __STATIC_CAST(_Traits*, 0));
5549720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
5559720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
5569720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
5579720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
5589720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block__size_type__
5599720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT, _Traits, _Alloc>::find_last_not_of(_CharT __c, size_type __pos) const
5609720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block{
5619720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const size_type __len = size();
5629720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if ( __len < 1 ) {
5639720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    return npos;
5649720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
5659720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;
5669720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const_reverse_iterator __rlast = const_reverse_iterator(__last);
5679720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  const_reverse_iterator __rresult =
5689720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _STLP_STD::find_if(__rlast, rend(),
5699720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                       _STLP_PRIV _Neq_char_bound<_Traits>(__c));
5709720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
5719720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
5729720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
5739720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#if !defined (basic_string)
5749720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block_STLP_MOVE_TO_PRIV_NAMESPACE
5759720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#endif
5769720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
5779720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
5789720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockvoid _STLP_CALL _S_string_copy(const basic_string<_CharT,_Traits,_Alloc>& __s,
5799720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                               _CharT* __buf, size_t __n) {
5809720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if (__n > 0) {
5819720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    __n = (min) (__n - 1, __s.size());
5829720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    _STLP_STD::copy(__s.begin(), __s.begin() + __n, __buf);
5839720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    __buf[__n] = _CharT();
5849720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
5859720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
5869720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
5879720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block_STLP_MOVE_TO_STD_NAMESPACE
5889720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
5899720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block_STLP_END_NAMESPACE
5909720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
5919720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#include <stl/_range_errors.h>
5929720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
5939720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block_STLP_BEGIN_NAMESPACE
5949720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
5959720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block_STLP_MOVE_TO_PRIV_NAMESPACE
5969720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
5979720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// _String_base methods
5989720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _Tp, class _Alloc>
5999720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockvoid _String_base<_Tp,_Alloc>::_M_throw_length_error() const
6009720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block{ __stl_throw_length_error("basic_string"); }
6019720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
6029720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _Tp, class _Alloc>
6039720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockvoid _String_base<_Tp, _Alloc>::_M_throw_out_of_range() const
6049720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block{ __stl_throw_out_of_range("basic_string"); }
6059720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
6069720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _Tp, class _Alloc>
6079720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockvoid _String_base<_Tp, _Alloc>::_M_allocate_block(size_t __n) {
6089720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  if ((__n <= (max_size() + 1)) && (__n > 0)) {
6099720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#if defined (_STLP_USE_SHORT_STRING_OPTIM)
6109720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    if (__n > _DEFAULT_SIZE) {
611e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      this->_M_start_of_storage._M_data = _M_start_of_storage.allocate(__n, __n);
612e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      this->_M_finish = this->_M_start_of_storage._M_data;
613e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott      this->_M_buffers._M_end_of_storage = this->_M_start_of_storage._M_data + __n;
6149720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    }
6159720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#else
616e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    this->_M_start_of_storage._M_data = _M_start_of_storage.allocate(__n, __n);
617e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    this->_M_finish = this->_M_start_of_storage._M_data;
618e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott    this->_M_end_of_storage = this->_M_start_of_storage._M_data + __n;
619e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#endif
6209720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  } else {
6219720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block    this->_M_throw_length_error();
6229720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  }
6239720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
6249720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
6259720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#if !defined (basic_string)
6269720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block_STLP_MOVE_TO_STD_NAMESPACE
6279720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#endif
6289720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
6299720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#if defined (_STLP_DONT_SUP_DFLT_PARAM)
6309720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
6319720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT* __s)
6329720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  : _STLP_PRIV _String_base<_CharT,_Alloc>(allocator_type()) {
6339720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _STLP_FIX_LITERAL_BUG(__s)
6349720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _M_range_initialize(__s, __s + traits_type::length(__s));
6359720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
6369720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#endif
6379720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
6389720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
6399720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT* __s,
6409720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block                                                    const allocator_type& __a)
6419720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  : _STLP_PRIV _String_base<_CharT,_Alloc>(__a) {
6429720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _STLP_FIX_LITERAL_BUG(__s)
6439720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  _M_range_initialize(__s, __s + traits_type::length(__s));
6449720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block}
6459720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
6469720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
6479720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockbasic_string<_CharT, _Traits, _Alloc>::basic_string(const basic_string<_CharT, _Traits, _Alloc> & __s)
6489720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block  : _STLP_PRIV _String_base<_CharT,_Alloc>(__s.get_allocator())
6499720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block{ _M_range_initialize(__s._M_Start(), __s._M_Finish()); }
6509720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
6519720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#if defined (basic_string)
6529720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block_STLP_MOVE_TO_STD_NAMESPACE
6539720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#  undef basic_string
654e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#endif
655e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott
656e46c9386c4f79aa40185f79a19fc5b2a7ef528b3Patrick Scott#if !defined (_STLP_STATIC_CONST_INIT_BUG) && !defined (_STLP_NO_STATIC_CONST_DEFINITION)
6579720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blocktemplate <class _CharT, class _Traits, class _Alloc>
6589720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Blockconst size_t basic_string<_CharT, _Traits, _Alloc>::npos;
6599720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#endif
6609720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
6619720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block_STLP_END_NAMESPACE
6629720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
6639720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#undef __size_type__
6649720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#if defined (_STLP_NESTED_TYPE_PARAM_BUG)
6659720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#  undef size_type
6669720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#  undef iterator
6679720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#endif
6689720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
6699720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block#endif /*  _STLP_STRING_C */
6709720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block
6719720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// Local Variables:
6729720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// mode:C++
6739720d5f59b9c1f5d1b9ecbc9173dbcb71bd557beSteve Block// End:
674