epoc32/include/stdapis/stlportv5/stl/_string.c
branchSymbian3
changeset 4 837f303aceeb
parent 3 e1b950c65cb4
     1.1 --- a/epoc32/include/stdapis/stlportv5/stl/_string.c	Wed Mar 31 12:27:01 2010 +0100
     1.2 +++ b/epoc32/include/stdapis/stlportv5/stl/_string.c	Wed Mar 31 12:33:34 2010 +0100
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - *  © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
     1.6 + *
     1.7   *
     1.8   * Copyright (c) 1994
     1.9   * Hewlett-Packard Company
    1.10 @@ -10,13 +10,13 @@
    1.11   * Copyright (c) 1997
    1.12   * Moscow Center for SPARC Technology
    1.13   *
    1.14 - * Copyright (c) 1999 
    1.15 + * Copyright (c) 1999
    1.16   * Boris Fomitchev
    1.17   *
    1.18   * This material is provided "as is", with absolutely no warranty expressed
    1.19   * or implied. Any use is at your own risk.
    1.20   *
    1.21 - * Permission to use or copy this software for any purpose is hereby granted 
    1.22 + * Permission to use or copy this software for any purpose is hereby granted
    1.23   * without fee, provided the above notices are retained on all copies.
    1.24   * Permission to modify the code and to distribute modified code is granted,
    1.25   * provided the above notices are retained, and a notice that the code was
    1.26 @@ -26,145 +26,188 @@
    1.27  #ifndef _STLP_STRING_C
    1.28  #define _STLP_STRING_C
    1.29  
    1.30 -#ifndef _STLP_STRING_H
    1.31 -# include <stl/_string.h>
    1.32 +#ifndef _STLP_INTERNAL_STRING_H
    1.33 +#  include <stl/_string.h>
    1.34  #endif
    1.35  
    1.36 -# ifdef _STLP_DEBUG
    1.37 -#  define basic_string _Nondebug_string
    1.38 -# endif
    1.39 +#ifndef _STLP_INTERNAL_CTRAITS_FUNCTIONS_H
    1.40 +#  include <stl/_ctraits_fns.h>
    1.41 +#endif
    1.42  
    1.43 -# if defined (_STLP_USE_OWN_NAMESPACE) || !defined (_STLP_USE_NATIVE_STRING)
    1.44 +#if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
    1.45 +#  define basic_string _STLP_NO_MEM_T_NAME(str)
    1.46 +#elif defined (_STLP_DEBUG)
    1.47 +#  define basic_string _STLP_NON_DBG_NAME(str)
    1.48 +#endif
    1.49  
    1.50 -# if defined (_STLP_NESTED_TYPE_PARAM_BUG)
    1.51 +#if defined (_STLP_NESTED_TYPE_PARAM_BUG)
    1.52  #  define __size_type__ size_t
    1.53  #  define size_type size_t
    1.54 -#  define iterator   _CharT*
    1.55 -# else
    1.56 +#  define iterator _CharT*
    1.57 +#else
    1.58  #  define __size_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_string<_CharT,_Traits,_Alloc>::size_type
    1.59 -# endif
    1.60 +#endif
    1.61  
    1.62  _STLP_BEGIN_NAMESPACE
    1.63  
    1.64 +_STLP_MOVE_TO_PRIV_NAMESPACE
    1.65 +
    1.66 +// A helper class to use a char_traits as a function object.
    1.67 +template <class _Traits>
    1.68 +struct _Not_within_traits : public unary_function<typename _Traits::char_type, bool> {
    1.69 +  typedef typename _Traits::char_type _CharT;
    1.70 +  const _CharT* _M_first;
    1.71 +  const _CharT* _M_last;
    1.72 +
    1.73 +  _Not_within_traits(const _CharT* __f, const _CharT* __l)
    1.74 +    : _M_first(__f), _M_last(__l) {}
    1.75 +
    1.76 +  bool operator()(const _CharT& __x) const {
    1.77 +    return find_if(_M_first, _M_last,
    1.78 +                   _STLP_PRIV _Eq_char_bound<_Traits>(__x)) == _M_last;
    1.79 +  }
    1.80 +};
    1.81 +
    1.82  // ------------------------------------------------------------
    1.83  // Non-inline declarations.
    1.84  
    1.85 +#if !defined (basic_string)
    1.86 +_STLP_MOVE_TO_STD_NAMESPACE
    1.87 +#endif
    1.88  
    1.89  // Change the string's capacity so that it is large enough to hold
    1.90  //  at least __res_arg elements, plus the terminating _CharT().  Note that,
    1.91  //  if __res_arg < capacity(), this member function may actually decrease
    1.92  //  the string's capacity.
    1.93 -template <class _CharT, class _Traits, class _Alloc> 
    1.94 -_STLP_EXP_DECLSPEC void basic_string<_CharT,_Traits,_Alloc>::reserve(size_type __res_arg) {
    1.95 +template <class _CharT, class _Traits, class _Alloc>
    1.96 +void basic_string<_CharT,_Traits,_Alloc>::reserve(size_type __res_arg) {
    1.97 +  if (__res_arg > max_size())
    1.98 +    this->_M_throw_length_error();
    1.99  
   1.100 -  if (__res_arg >= capacity())
   1.101 -    {      
   1.102 -      if (__res_arg > max_size())
   1.103 -	this->_M_throw_length_error();
   1.104 +  size_type __n = (max)(__res_arg, size()) + 1;
   1.105 +  if (__n <= capacity() + 1)
   1.106 +    return;
   1.107  
   1.108 -      size_type __n = __res_arg + 1;
   1.109 -      _STLP_LEAVE_VOLATILE pointer __new_start = this->_M_end_of_storage.allocate(__n);
   1.110 -      _STLP_LEAVE_VOLATILE pointer __new_finish = __new_start;
   1.111 -      
   1.112 -      _STLP_TRY {
   1.113 -	__new_finish = uninitialized_copy(this->_M_start, this->_M_finish, __new_start);
   1.114 -	_M_construct_null(__new_finish);
   1.115 -      }
   1.116 -      _STLP_UNWIND((_STLP_STD::_Destroy(__new_start, __new_finish), 
   1.117 -		    this->_M_end_of_storage.deallocate(__new_start, __n)));
   1.118 -      
   1.119 -      _STLP_STD::_Destroy(this->_M_start, this->_M_finish + 1);
   1.120 -      this->_M_deallocate_block();
   1.121 -      this->_M_start = __new_start;
   1.122 -      this->_M_finish = __new_finish;
   1.123 -      this->_M_end_of_storage._M_data = __new_start + __n;
   1.124 -    }
   1.125 +  pointer __new_start = this->_M_end_of_storage.allocate(__n, __n);
   1.126 +  pointer __new_finish = __new_start;
   1.127 +
   1.128 +  _STLP_TRY {
   1.129 +    __new_finish = _STLP_PRIV __ucopy(this->_M_Start(), this->_M_Finish(), __new_start);
   1.130 +    _M_construct_null(__new_finish);
   1.131 +  }
   1.132 +  _STLP_UNWIND((_STLP_STD::_Destroy_Range(__new_start, __new_finish),
   1.133 +                this->_M_end_of_storage.deallocate(__new_start, __n)))
   1.134 +
   1.135 +  this->_M_destroy_range();
   1.136 +  this->_M_deallocate_block();
   1.137 +  this->_M_reset(__new_start, __new_finish, __new_start + __n);
   1.138  }
   1.139  
   1.140 -template <class _CharT, class _Traits, class _Alloc> 
   1.141 -_STLP_EXP_DECLSPEC basic_string<_CharT,_Traits,_Alloc>& 
   1.142 -basic_string<_CharT,_Traits,_Alloc>::append(size_type __n, _CharT __c) 
   1.143 -{
   1.144 +template <class _CharT, class _Traits, class _Alloc>
   1.145 +basic_string<_CharT,_Traits,_Alloc>&
   1.146 +basic_string<_CharT,_Traits,_Alloc>::append(size_type __n, _CharT __c) {
   1.147    if (__n > max_size() || size() > max_size() - __n)
   1.148      this->_M_throw_length_error();
   1.149    if (size() + __n > capacity())
   1.150      reserve(size() + (max)(size(), __n));
   1.151    if (__n > 0) {
   1.152 -    uninitialized_fill_n(this->_M_finish + 1, __n - 1, __c);
   1.153 +#if defined (_STLP_USE_SHORT_STRING_OPTIM)
   1.154 +    if (this->_M_using_static_buf())
   1.155 +      _Traits::assign(this->_M_finish + 1, __n - 1, __c);
   1.156 +    else
   1.157 +#endif /* _STLP_USE_SHORT_STRING_OPTIM */
   1.158 +    _STLP_PRIV __uninitialized_fill_n(this->_M_finish + 1, __n - 1, __c);
   1.159      _STLP_TRY {
   1.160        _M_construct_null(this->_M_finish + __n);
   1.161      }
   1.162 -    _STLP_UNWIND(_STLP_STD::_Destroy(this->_M_finish + 1, this->_M_finish + __n));
   1.163 +    _STLP_UNWIND(this->_M_destroy_ptr_range(this->_M_finish + 1, this->_M_finish + __n))
   1.164      _Traits::assign(*end(), __c);
   1.165      this->_M_finish += __n;
   1.166    }
   1.167    return *this;
   1.168  }
   1.169  
   1.170 -#ifndef _STLP_MEMBER_TEMPLATES
   1.171 -
   1.172 -template <class _CharT, class _Traits, class _Alloc> 
   1.173 -_STLP_EXP_DECLSPEC basic_string<_CharT, _Traits, _Alloc>& 
   1.174 -basic_string<_CharT, _Traits, _Alloc>::append(const _CharT* __first,
   1.175 -					      const _CharT* __last)
   1.176 -{
   1.177 +template <class _CharT, class _Traits, class _Alloc>
   1.178 +basic_string<_CharT, _Traits, _Alloc>&
   1.179 +basic_string<_CharT, _Traits, _Alloc>::_M_append(const _CharT* __first, const _CharT* __last) {
   1.180    if (__first != __last) {
   1.181      const size_type __old_size = size();
   1.182      ptrdiff_t __n = __last - __first;
   1.183      if ((size_type)__n > max_size() || __old_size > max_size() - __n)
   1.184        this->_M_throw_length_error();
   1.185      if (__old_size + __n > capacity()) {
   1.186 -      const size_type __len = __old_size + (max)(__old_size, (size_t) __n) + 1;
   1.187 -      pointer __new_start = this->_M_end_of_storage.allocate(__len);
   1.188 -      _STLP_LEAVE_VOLATILE pointer __new_finish = __new_start;
   1.189 +      size_type __len = __old_size + (max)(__old_size, (size_t) __n) + 1;
   1.190 +      pointer __new_start = this->_M_end_of_storage.allocate(__len, __len);
   1.191 +      pointer __new_finish = __new_start;
   1.192        _STLP_TRY {
   1.193 -        __new_finish = uninitialized_copy(this->_M_start, this->_M_finish, __new_start);
   1.194 -        __new_finish = uninitialized_copy(__first, __last, __new_finish);
   1.195 +        __new_finish = _STLP_PRIV __ucopy(this->_M_Start(), this->_M_Finish(), __new_start);
   1.196 +        __new_finish = _STLP_PRIV __ucopy(__first, __last, __new_finish);
   1.197          _M_construct_null(__new_finish);
   1.198        }
   1.199 -      _STLP_UNWIND((_STLP_STD::_Destroy(__new_start,__new_finish),
   1.200 -                    this->_M_end_of_storage.deallocate(__new_start,__len)));
   1.201 -      _STLP_STD::_Destroy(this->_M_start, this->_M_finish + 1);
   1.202 +      _STLP_UNWIND((_STLP_STD::_Destroy_Range(__new_start,__new_finish),
   1.203 +                    this->_M_end_of_storage.deallocate(__new_start,__len)))
   1.204 +      this->_M_destroy_range();
   1.205        this->_M_deallocate_block();
   1.206 -      this->_M_start = __new_start;
   1.207 -      this->_M_finish = __new_finish;
   1.208 -      this->_M_end_of_storage._M_data = __new_start + __len; 
   1.209 +      this->_M_reset(__new_start, __new_finish, __new_start + __len);
   1.210      }
   1.211      else {
   1.212        const _CharT* __f1 = __first;
   1.213        ++__f1;
   1.214 -      uninitialized_copy(__f1, __last, this->_M_finish + 1);
   1.215 +#if defined (_STLP_USE_SHORT_STRING_OPTIM)
   1.216 +      if (this->_M_using_static_buf())
   1.217 +        _M_copy(__f1, __last, this->_M_Finish() + 1);
   1.218 +      else
   1.219 +#endif /* _STLP_USE_SHORT_STRING_OPTIM */
   1.220 +      _STLP_PRIV __ucopy(__f1, __last, this->_M_finish + 1);
   1.221        _STLP_TRY {
   1.222          _M_construct_null(this->_M_finish + __n);
   1.223        }
   1.224 -      _STLP_UNWIND(_STLP_STD::_Destroy(this->_M_finish + 1, this->_M_finish + __n));
   1.225 +      _STLP_UNWIND(this->_M_destroy_ptr_range(this->_M_finish + 1, this->_M_finish + __n))
   1.226        _Traits::assign(*end(), *__first);
   1.227        this->_M_finish += __n;
   1.228      }
   1.229    }
   1.230 -  return *this;  
   1.231 +  return *this;
   1.232  }
   1.233  
   1.234 -#endif /* _STLP_MEMBER_TEMPLATES */
   1.235 -
   1.236 -template <class _CharT, class _Traits, class _Alloc> 
   1.237 -_STLP_EXP_DECLSPEC basic_string<_CharT,_Traits,_Alloc>& 
   1.238 +template <class _CharT, class _Traits, class _Alloc>
   1.239 +basic_string<_CharT,_Traits,_Alloc>&
   1.240  basic_string<_CharT,_Traits,_Alloc>::assign(size_type __n, _CharT __c) {
   1.241    if (__n <= size()) {
   1.242 -    _Traits::assign(this->_M_start, __n, __c);
   1.243 +    _Traits::assign(this->_M_Start(), __n, __c);
   1.244      erase(begin() + __n, end());
   1.245    }
   1.246    else {
   1.247 -    _Traits::assign(this->_M_start, size(), __c);
   1.248 -    append(__n - size(), __c);
   1.249 +    if (__n < capacity()) {
   1.250 +      _Traits::assign(this->_M_Start(), size(), __c);
   1.251 +      append(__n - size(), __c);
   1.252 +    }
   1.253 +    else {
   1.254 +      _Self __str(__n, __c);
   1.255 +      this->swap(__str);
   1.256 +    }
   1.257    }
   1.258    return *this;
   1.259  }
   1.260  
   1.261 -template <class _CharT, class _Traits, class _Alloc> 
   1.262 +template <class _CharT, class _Traits, class _Alloc>
   1.263 +basic_string<_CharT,_Traits,_Alloc>&
   1.264 +basic_string<_CharT,_Traits,_Alloc>::_M_assign(const _CharT* __f, const _CharT* __l) {
   1.265 +  ptrdiff_t __n = __l - __f;
   1.266 +  if (__STATIC_CAST(size_type, __n) <= size()) {
   1.267 +    _Traits::copy(this->_M_Start(), __f, __n);
   1.268 +    erase(begin() + __n, end());
   1.269 +  }
   1.270 +  else {
   1.271 +    _Traits::copy(this->_M_Start(), __f, size());
   1.272 +    _M_append(__f + size(), __l);
   1.273 +  }
   1.274 +  return *this;
   1.275 +}
   1.276 +
   1.277 +template <class _CharT, class _Traits, class _Alloc>
   1.278  _CharT* basic_string<_CharT,_Traits,_Alloc> ::_M_insert_aux(_CharT* __p,
   1.279 -                  _CharT __c)
   1.280 -{
   1.281 +                                                            _CharT __c) {
   1.282    pointer __new_pos = __p;
   1.283    if (this->_M_finish + 1 < this->_M_end_of_storage._M_data) {
   1.284      _M_construct_null(this->_M_finish + 1);
   1.285 @@ -174,146 +217,175 @@
   1.286    }
   1.287    else {
   1.288      const size_type __old_len = size();
   1.289 -    const size_type __len = __old_len +
   1.290 -                            (max)(__old_len, __STATIC_CAST(size_type,1)) + 1;
   1.291 -    pointer __new_start = this->_M_end_of_storage.allocate(__len);
   1.292 -    _STLP_LEAVE_VOLATILE pointer __new_finish = __new_start;
   1.293 +    size_type __len = __old_len + (max)(__old_len, __STATIC_CAST(size_type,1)) + 1;
   1.294 +    pointer __new_start = this->_M_end_of_storage.allocate(__len, __len);
   1.295 +    pointer __new_finish = __new_start;
   1.296      _STLP_TRY {
   1.297 -      __new_pos = uninitialized_copy(this->_M_start, __p, __new_start);
   1.298 -      _Construct(__new_pos, __c);
   1.299 +      __new_pos = _STLP_PRIV __ucopy(this->_M_Start(), __p, __new_start);
   1.300 +      _Copy_Construct(__new_pos, __c);
   1.301        __new_finish = __new_pos + 1;
   1.302 -      __new_finish = uninitialized_copy(__p, this->_M_finish, __new_finish);
   1.303 +      __new_finish = _STLP_PRIV __ucopy(__p, this->_M_finish, __new_finish);
   1.304        _M_construct_null(__new_finish);
   1.305      }
   1.306 -    _STLP_UNWIND((_STLP_STD::_Destroy(__new_start,__new_finish), 
   1.307 -                  this->_M_end_of_storage.deallocate(__new_start,__len)));
   1.308 -    _STLP_STD::_Destroy(this->_M_start, this->_M_finish + 1);
   1.309 +    _STLP_UNWIND((_STLP_STD::_Destroy_Range(__new_start,__new_finish),
   1.310 +                  this->_M_end_of_storage.deallocate(__new_start,__len)))
   1.311 +    this->_M_destroy_range();
   1.312      this->_M_deallocate_block();
   1.313 -    this->_M_start = __new_start;
   1.314 -    this->_M_finish = __new_finish;
   1.315 -    this->_M_end_of_storage._M_data = __new_start + __len;
   1.316 +    this->_M_reset(__new_start, __new_finish, __new_start + __len);
   1.317    }
   1.318    return __new_pos;
   1.319  }
   1.320  
   1.321 -template <class _CharT, class _Traits, class _Alloc> 
   1.322 -_STLP_EXP_DECLSPEC void basic_string<_CharT,_Traits,_Alloc>::insert(iterator __position,
   1.323 -           size_t __n, _CharT __c)
   1.324 -{
   1.325 +template <class _CharT, class _Traits, class _Alloc>
   1.326 +void basic_string<_CharT,_Traits,_Alloc>::insert(iterator __pos,
   1.327 +                                                 size_t __n, _CharT __c) {
   1.328    if (__n != 0) {
   1.329      if (size_type(this->_M_end_of_storage._M_data - this->_M_finish) >= __n + 1) {
   1.330 -      const size_type __elems_after = this->_M_finish - __position;
   1.331 +      const size_type __elems_after = this->_M_finish - __pos;
   1.332        pointer __old_finish = this->_M_finish;
   1.333        if (__elems_after >= __n) {
   1.334 -        uninitialized_copy((this->_M_finish - __n) + 1, this->_M_finish + 1,
   1.335 +#if defined (_STLP_USE_SHORT_STRING_OPTIM)
   1.336 +        if (this->_M_using_static_buf())
   1.337 +          _M_copy((this->_M_finish - __n) + 1, this->_M_finish + 1, this->_M_finish + 1);
   1.338 +        else
   1.339 +#endif /* _STLP_USE_SHORT_STRING_OPTIM */
   1.340 +        _STLP_PRIV __ucopy((this->_M_finish - __n) + 1, this->_M_finish + 1,
   1.341                             this->_M_finish + 1);
   1.342          this->_M_finish += __n;
   1.343 -        _Traits::move(__position + __n,
   1.344 -                      __position, (__elems_after - __n) + 1);
   1.345 -        _Traits::assign(__position, __n, __c);
   1.346 +        _Traits::move(__pos + __n, __pos, (__elems_after - __n) + 1);
   1.347 +        _Traits::assign(__pos, __n, __c);
   1.348        }
   1.349        else {
   1.350 -        uninitialized_fill_n(this->_M_finish + 1, __n - __elems_after - 1, __c);
   1.351 +#if defined (_STLP_USE_SHORT_STRING_OPTIM)
   1.352 +        if (this->_M_using_static_buf())
   1.353 +          _Traits::assign(this->_M_finish + 1, __n - __elems_after - 1, __c);
   1.354 +        else
   1.355 +#endif /* _STLP_USE_SHORT_STRING_OPTIM */
   1.356 +        _STLP_PRIV __uninitialized_fill_n(this->_M_finish + 1, __n - __elems_after - 1, __c);
   1.357          this->_M_finish += __n - __elems_after;
   1.358          _STLP_TRY {
   1.359 -          uninitialized_copy(__position, __old_finish + 1, this->_M_finish);
   1.360 +#if defined (_STLP_USE_SHORT_STRING_OPTIM)
   1.361 +          if (this->_M_using_static_buf())
   1.362 +            _M_copy(__pos, __old_finish + 1, this->_M_finish);
   1.363 +          else
   1.364 +#endif /* _STLP_USE_SHORT_STRING_OPTIM */
   1.365 +          _STLP_PRIV __ucopy(__pos, __old_finish + 1, this->_M_finish);
   1.366            this->_M_finish += __elems_after;
   1.367          }
   1.368 -        _STLP_UNWIND((_STLP_STD::_Destroy(__old_finish + 1, this->_M_finish), 
   1.369 -                      this->_M_finish = __old_finish));
   1.370 -        _Traits::assign(__position, __elems_after + 1, __c);
   1.371 +        _STLP_UNWIND((_STLP_STD::_Destroy_Range(__old_finish + 1, this->_M_finish),
   1.372 +                      this->_M_finish = __old_finish))
   1.373 +        _Traits::assign(__pos, __elems_after + 1, __c);
   1.374        }
   1.375      }
   1.376      else {
   1.377 -      const size_type __old_size = size();        
   1.378 -      const size_type __len = __old_size + (max)(__old_size, __n) + 1;
   1.379 -      pointer __new_start = this->_M_end_of_storage.allocate(__len);
   1.380 -      _STLP_LEAVE_VOLATILE pointer __new_finish = __new_start;
   1.381 +      const size_type __old_size = size();
   1.382 +      size_type __len = __old_size + (max)(__old_size, __n) + 1;
   1.383 +      pointer __new_start = this->_M_end_of_storage.allocate(__len, __len);
   1.384 +      pointer __new_finish = __new_start;
   1.385        _STLP_TRY {
   1.386 -        __new_finish = uninitialized_copy(this->_M_start, __position, __new_start);
   1.387 -        __new_finish = uninitialized_fill_n(__new_finish, __n, __c);
   1.388 -        __new_finish = uninitialized_copy(__position, this->_M_finish,
   1.389 -                                          __new_finish);
   1.390 +        __new_finish = _STLP_PRIV __ucopy(this->_M_Start(), __pos, __new_start);
   1.391 +        __new_finish = _STLP_PRIV __uninitialized_fill_n(__new_finish, __n, __c);
   1.392 +        __new_finish = _STLP_PRIV __ucopy(__pos, this->_M_finish, __new_finish);
   1.393          _M_construct_null(__new_finish);
   1.394        }
   1.395 -      _STLP_UNWIND((_STLP_STD::_Destroy(__new_start,__new_finish),
   1.396 -                    this->_M_end_of_storage.deallocate(__new_start,__len)));
   1.397 -      _STLP_STD::_Destroy(this->_M_start, this->_M_finish + 1);
   1.398 +      _STLP_UNWIND((_STLP_STD::_Destroy_Range(__new_start,__new_finish),
   1.399 +                    this->_M_end_of_storage.deallocate(__new_start,__len)))
   1.400 +      this->_M_destroy_range();
   1.401        this->_M_deallocate_block();
   1.402 -      this->_M_start = __new_start;
   1.403 -      this->_M_finish = __new_finish;
   1.404 -      this->_M_end_of_storage._M_data = __new_start + __len;    
   1.405 +      this->_M_reset(__new_start, __new_finish, __new_start + __len);
   1.406      }
   1.407    }
   1.408  }
   1.409  
   1.410 -#ifndef _STLP_MEMBER_TEMPLATES
   1.411 -
   1.412 -template <class _CharT, class _Traits, class _Alloc> 
   1.413 -_STLP_EXP_DECLSPEC void 
   1.414 -basic_string<_CharT,_Traits,_Alloc>::insert(iterator __position,
   1.415 -                                            const _CharT* __first, 
   1.416 -                                            const _CharT* __last)
   1.417 -{
   1.418 +template <class _CharT, class _Traits, class _Alloc>
   1.419 +void basic_string<_CharT,_Traits,_Alloc>::_M_insert(iterator __pos,
   1.420 +                                                    const _CharT* __first, const _CharT* __last,
   1.421 +                                                    bool __self_ref) {
   1.422 +  //this version has to take care about the auto referencing
   1.423    if (__first != __last) {
   1.424      const ptrdiff_t __n = __last - __first;
   1.425      if (this->_M_end_of_storage._M_data - this->_M_finish >= __n + 1) {
   1.426 -      const ptrdiff_t __elems_after = this->_M_finish - __position;
   1.427 +      const ptrdiff_t __elems_after = this->_M_finish - __pos;
   1.428        pointer __old_finish = this->_M_finish;
   1.429        if (__elems_after >= __n) {
   1.430 -        uninitialized_copy((this->_M_finish - __n) + 1, this->_M_finish + 1,
   1.431 -                           this->_M_finish + 1);
   1.432 +#if defined (_STLP_USE_SHORT_STRING_OPTIM)
   1.433 +        if (this->_M_using_static_buf())
   1.434 +          _M_copy((this->_M_finish - __n) + 1, this->_M_finish + 1, this->_M_finish + 1);
   1.435 +        else
   1.436 +#endif /* _STLP_USE_SHORT_STRING_OPTIM */
   1.437 +        _STLP_PRIV __ucopy((this->_M_finish - __n) + 1, this->_M_finish + 1, this->_M_finish + 1);
   1.438          this->_M_finish += __n;
   1.439 -        _Traits::move(__position + __n,
   1.440 -                      __position, (__elems_after - __n) + 1);
   1.441 -        _M_copy(__first, __last, __position);
   1.442 +        _Traits::move(__pos + __n, __pos, (__elems_after - __n) + 1);
   1.443 +        if (!__self_ref || __last < __pos) {
   1.444 +          _M_copy(__first, __last, __pos);
   1.445 +        }
   1.446 +        else {
   1.447 +          //We have to check that the source buffer hasn't move
   1.448 +          if (__first >= __pos) {
   1.449 +            //The source buffer has move
   1.450 +            __first += __n;
   1.451 +            __last += __n;
   1.452 +            _M_copy(__first, __last, __pos);
   1.453 +          }
   1.454 +          else {
   1.455 +            //The source buffer hasn't move, it has been duplicated
   1.456 +            _M_move(__first, __last, __pos);
   1.457 +          }
   1.458 +        }
   1.459        }
   1.460        else {
   1.461 -        const _CharT* __mid = __first;
   1.462 -        advance(__mid, __elems_after + 1);
   1.463 -        uninitialized_copy(__mid, __last, this->_M_finish + 1);
   1.464 +        const_iterator __mid = __first;
   1.465 +        __mid += __elems_after + 1;
   1.466 +#if defined (_STLP_USE_SHORT_STRING_OPTIM)
   1.467 +        if (this->_M_using_static_buf())
   1.468 +          _M_copy(__mid, __last, this->_M_finish + 1);
   1.469 +        else
   1.470 +#endif /* _STLP_USE_SHORT_STRING_OPTIM */
   1.471 +        _STLP_PRIV __ucopy(__mid, __last, this->_M_finish + 1);
   1.472          this->_M_finish += __n - __elems_after;
   1.473          _STLP_TRY {
   1.474 -          uninitialized_copy(__position, __old_finish + 1, this->_M_finish);
   1.475 +#if defined (_STLP_USE_SHORT_STRING_OPTIM)
   1.476 +          if (this->_M_using_static_buf())
   1.477 +            _M_copy(__pos, __old_finish + 1, this->_M_finish);
   1.478 +          else
   1.479 +#endif /* _STLP_USE_SHORT_STRING_OPTIM */
   1.480 +          _STLP_PRIV __ucopy(__pos, __old_finish + 1, this->_M_finish);
   1.481            this->_M_finish += __elems_after;
   1.482          }
   1.483 -        _STLP_UNWIND((_STLP_STD::_Destroy(__old_finish + 1, this->_M_finish), 
   1.484 -                      this->_M_finish = __old_finish));
   1.485 -        _M_copy(__first, __mid, __position);
   1.486 +        _STLP_UNWIND((_STLP_STD::_Destroy_Range(__old_finish + 1, this->_M_finish),
   1.487 +                      this->_M_finish = __old_finish))
   1.488 +        if (!__self_ref)
   1.489 +          _M_copy(__first, __mid, __pos);
   1.490 +        else
   1.491 +          _M_move(__first, __mid, __pos);
   1.492        }
   1.493      }
   1.494      else {
   1.495 -      size_type __old_size = size();        
   1.496 -      size_type __len
   1.497 -        = __old_size + (max)(__old_size, __STATIC_CAST(const size_type,__n)) + 1;
   1.498 -      pointer __new_start = this->_M_end_of_storage.allocate(__len);
   1.499 -      _STLP_LEAVE_VOLATILE pointer __new_finish = __new_start;
   1.500 +      const size_type __old_size = size();
   1.501 +      size_type __len = __old_size + (max)(__old_size, __STATIC_CAST(const size_type,__n)) + 1;
   1.502 +      pointer __new_start = this->_M_end_of_storage.allocate(__len, __len);
   1.503 +      pointer __new_finish = __new_start;
   1.504        _STLP_TRY {
   1.505 -        __new_finish = uninitialized_copy(this->_M_start, __position, __new_start);
   1.506 -        __new_finish = uninitialized_copy(__first, __last, __new_finish);
   1.507 -        __new_finish
   1.508 -          = uninitialized_copy(__position, this->_M_finish, __new_finish);
   1.509 +        __new_finish = _STLP_PRIV __ucopy(this->_M_Start(), __pos, __new_start);
   1.510 +        __new_finish = _STLP_PRIV __ucopy(__first, __last, __new_finish);
   1.511 +        __new_finish = _STLP_PRIV __ucopy(__pos, this->_M_finish, __new_finish);
   1.512          _M_construct_null(__new_finish);
   1.513        }
   1.514 -      _STLP_UNWIND((_STLP_STD::_Destroy(__new_start,__new_finish),
   1.515 -                    this->_M_end_of_storage.deallocate(__new_start,__len)));
   1.516 -      _STLP_STD::_Destroy(this->_M_start, this->_M_finish + 1);
   1.517 +      _STLP_UNWIND((_STLP_STD::_Destroy_Range(__new_start,__new_finish),
   1.518 +                    this->_M_end_of_storage.deallocate(__new_start,__len)))
   1.519 +      this->_M_destroy_range();
   1.520        this->_M_deallocate_block();
   1.521 -      this->_M_start = __new_start;
   1.522 -      this->_M_finish = __new_finish;
   1.523 -      this->_M_end_of_storage._M_data = __new_start + __len; 
   1.524 +      this->_M_reset(__new_start, __new_finish, __new_start + __len);
   1.525      }
   1.526    }
   1.527  }
   1.528  
   1.529 -#endif /* _STLP_MEMBER_TEMPLATES */
   1.530 +template <class _CharT, class _Traits, class _Alloc>
   1.531 +basic_string<_CharT,_Traits,_Alloc>&
   1.532 +basic_string<_CharT,_Traits,_Alloc> ::replace(iterator __first, iterator __last,
   1.533 +                                              size_type __n, _CharT __c) {
   1.534 +  size_type __len = (size_type)(__last - __first);
   1.535  
   1.536 -template <class _CharT, class _Traits, class _Alloc> 
   1.537 -_STLP_EXP_DECLSPEC basic_string<_CharT,_Traits,_Alloc>& 
   1.538 -basic_string<_CharT,_Traits,_Alloc>::replace(iterator __first, iterator __last, size_type __n, _CharT __c)
   1.539 -{
   1.540 -  size_type __len = (size_type)(__last - __first);
   1.541 -  
   1.542    if (__len >= __n) {
   1.543      _Traits::assign(__first, __n, __c);
   1.544      erase(__first + __n, __last);
   1.545 @@ -325,300 +397,292 @@
   1.546    return *this;
   1.547  }
   1.548  
   1.549 -#ifndef _STLP_MEMBER_TEMPLATES
   1.550 -
   1.551 -
   1.552 -template <class _CharT, class _Traits, class _Alloc> 
   1.553 -_STLP_EXP_DECLSPEC basic_string<_CharT,_Traits,_Alloc>& 
   1.554 -basic_string<_CharT,_Traits,_Alloc> ::replace(iterator __first, iterator __last,
   1.555 -            const _CharT* __f, const _CharT* __l)
   1.556 -{
   1.557 -  const ptrdiff_t         __n = __l - __f;
   1.558 +template <class _CharT, class _Traits, class _Alloc>
   1.559 +basic_string<_CharT,_Traits,_Alloc>&
   1.560 +basic_string<_CharT,_Traits,_Alloc> ::_M_replace(iterator __first, iterator __last,
   1.561 +                                                 const _CharT* __f, const _CharT* __l,
   1.562 +                                                 bool __self_ref) {
   1.563 +  const ptrdiff_t       __n = __l - __f;
   1.564    const difference_type __len = __last - __first;
   1.565    if (__len >= __n) {
   1.566 -    _M_copy(__f, __l, __first);
   1.567 +    if (!__self_ref || __l < __first || __f >= __last)
   1.568 +      _M_copy(__f, __l, __first);
   1.569 +    else
   1.570 +      _M_move(__f, __l, __first);
   1.571      erase(__first + __n, __last);
   1.572    }
   1.573    else {
   1.574 -    const _CharT* __m = __f + __len;
   1.575 -    _M_copy(__f, __m, __first);
   1.576 -    insert(__last, __m, __l);
   1.577 +    if (!__self_ref || (__f >= __last) || (__l <= __first)) {
   1.578 +      //no overlap:
   1.579 +      const_iterator __m = __f + __len;
   1.580 +      _M_copy(__f, __m, __first);
   1.581 +      _M_insert(__last, __m, __l, false );
   1.582 +    }
   1.583 +    else {
   1.584 +      //we have to take care of overlaping
   1.585 +      if (__f < __first) {
   1.586 +        const_iterator __m = __f + __len;
   1.587 +        //We have to deal with possible reallocation because we do insert first.
   1.588 +        const difference_type __off_dest = __first - this->begin();
   1.589 +        const difference_type __off_src = __f - this->begin();
   1.590 +        _M_insert(__last, __m, __l, true);
   1.591 +        _Traits::move(begin() + __off_dest, begin() + __off_src, __len);
   1.592 +      }
   1.593 +      else {
   1.594 +        const_iterator __m = __f + __len;
   1.595 +        _Traits::move(__first, __f, __len);
   1.596 +        _M_insert(__last, __m, __l, true);
   1.597 +      }
   1.598 +    }
   1.599    }
   1.600    return *this;
   1.601  }
   1.602  
   1.603 -#endif /* _STLP_MEMBER_TEMPLATES */
   1.604 -
   1.605 -template <class _CharT, class _Traits, class _Alloc> 
   1.606 -_STLP_EXP_DECLSPEC __size_type__
   1.607 -basic_string<_CharT,_Traits,_Alloc> ::find(const _CharT* __s, size_type __pos, size_type __n) const 
   1.608 -{
   1.609 -#ifndef __SYMBIAN32__ // A different implementation without using search
   1.610 -  if (__pos + __n > size())
   1.611 +template <class _CharT, class _Traits, class _Alloc> __size_type__
   1.612 +basic_string<_CharT,_Traits,_Alloc> ::find(const _CharT* __s, size_type __pos,
   1.613 +                                           size_type __n) const {
   1.614 +  const size_t __len = size();
   1.615 +  if (__pos >= __len || __pos + __n > __len)
   1.616      return npos;
   1.617    else {
   1.618 -    const const_pointer __result =
   1.619 -      _STLP_STD::search((const _CharT*)this->_M_start + __pos, (const _CharT*)this->_M_finish, 
   1.620 -			__s, __s + __n, _Eq_traits<_Traits>());
   1.621 -    return __result != this->_M_finish ? __result - this->_M_start : npos;
   1.622 -  }
   1.623 -#else
   1.624 -  const size_type __len = this->size();
   1.625 -  size_t __tpos = __pos;
   1.626 -  const _CharT* __data = this->_M_start;
   1.627 -  while (__tpos + __n <= __len) {
   1.628 -    if (traits_type::compare(__data + __tpos, __s, __n) == 0)
   1.629 -      return __tpos;
   1.630 -    ++__tpos;
   1.631 -  }
   1.632 -  return npos;  
   1.633 -#endif //__SYMBIAN32__  
   1.634 -}
   1.635 -
   1.636 -template <class _CharT, class _Traits, class _Alloc> 
   1.637 -_STLP_EXP_DECLSPEC __size_type__
   1.638 -basic_string<_CharT,_Traits,_Alloc> ::find(_CharT __c, size_type __pos) const 
   1.639 -{
   1.640 -  if (__pos >= size())
   1.641 -    return npos;
   1.642 -  else {
   1.643 -    const const_pointer __result =
   1.644 -      _STLP_STD::find_if((const _CharT*)this->_M_start + __pos, (const _CharT*)this->_M_finish,
   1.645 -			 _Eq_char_bound<_Traits>(__c));
   1.646 -    return __result != this->_M_finish ? __result - this->_M_start : npos;
   1.647 -  }
   1.648 -}    
   1.649 -
   1.650 -template <class _CharT, class _Traits, class _Alloc> 
   1.651 -_STLP_EXP_DECLSPEC __size_type__
   1.652 -basic_string<_CharT,_Traits,_Alloc> ::rfind(const _CharT* __s, size_type __pos, size_type __n) const 
   1.653 -{
   1.654 -  const size_t __len = size();
   1.655 -
   1.656 -  if (__n > __len)
   1.657 -    return npos;
   1.658 -  else if (__n == 0)
   1.659 -    return (min) (__len, __pos);
   1.660 -  else {
   1.661 -    const_pointer __last = this->_M_start + (min) (__len - __n, __pos) + __n;
   1.662 -    const_pointer __result = _STLP_STD::find_end((const_pointer)this->_M_start, __last,
   1.663 -						 __s, __s + __n,
   1.664 -						 _Eq_traits<_Traits>());
   1.665 -    return __result != __last ? __result - this->_M_start : npos;
   1.666 +    const_pointer __result =
   1.667 +      _STLP_STD::search(this->_M_Start() + __pos, this->_M_Finish(),
   1.668 +                        __s, __s + __n, _STLP_PRIV _Eq_traits<_Traits>());
   1.669 +    return __result != this->_M_Finish() ? __result - this->_M_Start() : npos;
   1.670    }
   1.671  }
   1.672  
   1.673 -template <class _CharT, class _Traits, class _Alloc> 
   1.674 -_STLP_EXP_DECLSPEC __size_type__
   1.675 -basic_string<_CharT,_Traits,_Alloc> ::rfind(_CharT __c, size_type __pos) const 
   1.676 -{
   1.677 -  const size_type __len = size();
   1.678 -
   1.679 -  if (__len < 1)
   1.680 +template <class _CharT, class _Traits, class _Alloc> __size_type__
   1.681 +basic_string<_CharT,_Traits,_Alloc> ::find(_CharT __c, size_type __pos) const {
   1.682 +  if (__pos >= size()) /*__pos + 1 > size()*/
   1.683      return npos;
   1.684    else {
   1.685 -    const const_iterator __last = begin() + (min) (__len - 1, __pos) + 1;
   1.686 -    const_reverse_iterator __rresult =
   1.687 -      _STLP_STD::find_if(const_reverse_iterator(__last), rend(),
   1.688 -              _Eq_char_bound<_Traits>(__c));
   1.689 -    return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
   1.690 +    const_pointer __result =
   1.691 +      _STLP_STD::find_if(this->_M_Start() + __pos, this->_M_Finish(),
   1.692 +                         _STLP_PRIV _Eq_char_bound<_Traits>(__c));
   1.693 +    return __result != this->_M_Finish() ? __result - this->_M_Start() : npos;
   1.694    }
   1.695  }
   1.696  
   1.697 -template <class _CharT, class _Traits, class _Alloc> 
   1.698 -_STLP_EXP_DECLSPEC __size_type__
   1.699 -basic_string<_CharT,_Traits,_Alloc> 
   1.700 -    ::find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
   1.701 +template <class _CharT, class _Traits, class _Alloc>
   1.702 +__size_type__
   1.703 +basic_string<_CharT,_Traits,_Alloc>::rfind(const _CharT* __s, size_type __pos, size_type __n) const
   1.704  {
   1.705 -  if (__pos >= size())
   1.706 +  const size_type __len = size();
   1.707 +  if ( __len < __n ) {
   1.708 +    return npos;
   1.709 +  }
   1.710 +  const_pointer __last = this->_M_Start() + (min)( __len - __n, __pos) + __n;
   1.711 +  const_pointer __result = find_end(this->_M_Start(), __last,
   1.712 +                                    __s, __s + __n, _STLP_PRIV _Eq_traits<_Traits>());
   1.713 +  return __result != __last ? __result - this->_M_Start() : npos;
   1.714 +}
   1.715 +
   1.716 +template <class _CharT, class _Traits, class _Alloc>
   1.717 +__size_type__
   1.718 +basic_string<_CharT,_Traits,_Alloc>::rfind(_CharT __c, size_type __pos) const
   1.719 +{
   1.720 +  const size_type __len = size();
   1.721 +  if ( __len < 1 ) {
   1.722 +    return npos;
   1.723 +  }
   1.724 +  const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;
   1.725 +  const_reverse_iterator __rresult =
   1.726 +    _STLP_STD::find_if(const_reverse_iterator(__last), rend(),
   1.727 +                       _STLP_PRIV _Eq_char_bound<_Traits>(__c));
   1.728 +  return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
   1.729 +}
   1.730 +
   1.731 +template <class _CharT, class _Traits, class _Alloc> __size_type__
   1.732 +basic_string<_CharT,_Traits,_Alloc> ::find_first_of(const _CharT* __s, size_type __pos,
   1.733 +                                                    size_type __n) const {
   1.734 +  if (__pos >= size()) /*__pos + 1 > size()*/
   1.735      return npos;
   1.736    else {
   1.737 -    const_iterator __result = __find_first_of(begin() + __pos, end(),
   1.738 -                                              __s, __s + __n,
   1.739 -                                              _Eq_traits<_Traits>());
   1.740 +    const_iterator __result = _STLP_PRIV __find_first_of(begin() + __pos, end(),
   1.741 +                                                         __s, __s + __n,
   1.742 +                                                         _STLP_PRIV _Eq_traits<_Traits>());
   1.743      return __result != end() ? __result - begin() : npos;
   1.744    }
   1.745  }
   1.746  
   1.747 -
   1.748 -template <class _CharT, class _Traits, class _Alloc> 
   1.749 -_STLP_EXP_DECLSPEC __size_type__
   1.750 -basic_string<_CharT,_Traits,_Alloc> 
   1.751 -    ::find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
   1.752 +template <class _CharT, class _Traits, class _Alloc>
   1.753 + __size_type__
   1.754 +basic_string<_CharT,_Traits,_Alloc> ::find_last_of(const _CharT* __s, size_type __pos,
   1.755 +                                                   size_type __n) const
   1.756  {
   1.757    const size_type __len = size();
   1.758 +  if ( __len < 1 ) {
   1.759 +    return npos;
   1.760 +  }
   1.761 +  const const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;
   1.762 +  const const_reverse_iterator __rresult =
   1.763 +    _STLP_PRIV __find_first_of(const_reverse_iterator(__last), rend(),
   1.764 +                               __s, __s + __n,
   1.765 +                              _STLP_PRIV _Eq_traits<_Traits>());
   1.766 +  return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
   1.767 +}
   1.768  
   1.769 -  if (__len < 1)
   1.770 +
   1.771 +template <class _CharT, class _Traits, class _Alloc> __size_type__
   1.772 +basic_string<_CharT,_Traits,_Alloc> ::find_first_not_of(const _CharT* __s, size_type __pos,
   1.773 +                                                        size_type __n) const {
   1.774 +  typedef typename _Traits::char_type _CharType;
   1.775 +  if (__pos >= size()) /*__pos + 1 >= size()*/
   1.776      return npos;
   1.777    else {
   1.778 -    const const_iterator __last = begin() + (min) (__len - 1, __pos) + 1;
   1.779 -    const const_reverse_iterator __rresult =
   1.780 -      __find_first_of(const_reverse_iterator(__last), rend(),
   1.781 -                      __s, __s + __n,
   1.782 -                      _Eq_traits<_Traits>());
   1.783 -    return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
   1.784 +    const_pointer __result = _STLP_STD::find_if(this->_M_Start() + __pos, this->_M_Finish(),
   1.785 +                                                _STLP_PRIV _Not_within_traits<_Traits>(__CONST_CAST(const _CharType*, __s),
   1.786 +                                                                                        __CONST_CAST(const _CharType*, __s) + __n));
   1.787 +    return __result != this->_M_finish ? __result - this->_M_Start() : npos;
   1.788    }
   1.789  }
   1.790  
   1.791 -
   1.792 -template <class _CharT, class _Traits, class _Alloc> 
   1.793 -_STLP_EXP_DECLSPEC __size_type__
   1.794 -basic_string<_CharT,_Traits,_Alloc> ::find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
   1.795 -{
   1.796 -  typedef typename _Traits::char_type _CharType;
   1.797 -  if (__pos > size())
   1.798 +template <class _CharT, class _Traits, class _Alloc> __size_type__
   1.799 +basic_string<_CharT,_Traits,_Alloc> ::find_first_not_of(_CharT __c, size_type __pos) const {
   1.800 +  if (1 > size())
   1.801      return npos;
   1.802    else {
   1.803 -    const_pointer __result = _STLP_STD::find_if((const _CharT*)this->_M_start + __pos, 
   1.804 -				      (const _CharT*)this->_M_finish,
   1.805 -                                _Not_within_traits<_Traits>((const _CharType*)__s, 
   1.806 -							    (const _CharType*)__s + __n));
   1.807 -    return __result != this->_M_finish ? __result - this->_M_start : npos;
   1.808 +    const_pointer __result = _STLP_STD::find_if(this->_M_Start() + __pos, this->_M_Finish(),
   1.809 +                                                _STLP_PRIV _Neq_char_bound<_Traits>(__c));
   1.810 +    return __result != this->_M_finish ? __result - this->_M_Start() : npos;
   1.811    }
   1.812  }
   1.813  
   1.814 -template <class _CharT, class _Traits, class _Alloc> 
   1.815 -_STLP_EXP_DECLSPEC __size_type__
   1.816 -basic_string<_CharT,_Traits,_Alloc> ::find_first_not_of(_CharT __c, size_type __pos) const
   1.817 -{
   1.818 -  if (__pos > size())
   1.819 -    return npos;
   1.820 -  else {
   1.821 -    const_pointer __result = _STLP_STD::find_if((const _CharT*)this->_M_start + __pos, (const _CharT*)this->_M_finish,
   1.822 -						_Neq_char_bound<_Traits>(__c));
   1.823 -    return __result != this->_M_finish ? __result - this->_M_start : npos;
   1.824 -  }
   1.825 -}    
   1.826 -
   1.827 -template <class _CharT, class _Traits, class _Alloc> 
   1.828 -_STLP_EXP_DECLSPEC __size_type__
   1.829 -basic_string<_CharT,_Traits,_Alloc> ::find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const 
   1.830 +template <class _CharT, class _Traits, class _Alloc>
   1.831 +__size_type__
   1.832 +basic_string<_CharT,_Traits,_Alloc>::find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
   1.833  {
   1.834    typedef typename _Traits::char_type _CharType;
   1.835    const size_type __len = size();
   1.836 -
   1.837 -  if (__len < 1)
   1.838 +  if ( __len < 1 ) {
   1.839      return npos;
   1.840 -  else {
   1.841 -    const_iterator __last = begin() + (min) (__len - 1, __pos) + 1;
   1.842 -    const_reverse_iterator __rlast = const_reverse_iterator(__last);
   1.843 -    const_reverse_iterator __rresult =
   1.844 -      _STLP_STD::find_if(__rlast, rend(),
   1.845 -			 _Not_within_traits<_Traits>((const _CharType*)__s, 
   1.846 -						     (const _CharType*)__s + __n));
   1.847 -    return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
   1.848    }
   1.849 +  const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;
   1.850 +  const_reverse_iterator __rlast = const_reverse_iterator(__last);
   1.851 +  const_reverse_iterator __rresult =
   1.852 +    _STLP_STD::find_if(__rlast, rend(),
   1.853 +                       _STLP_PRIV _Not_within_traits<_Traits>((const _CharType*)__s,
   1.854 +                                                              (const _CharType*)__s + __n));
   1.855 +  return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
   1.856  }
   1.857  
   1.858 -template <class _CharT, class _Traits, class _Alloc> 
   1.859 -_STLP_EXP_DECLSPEC __size_type__
   1.860 -basic_string<_CharT, _Traits, _Alloc> ::find_last_not_of(_CharT __c, size_type __pos) const 
   1.861 +template <class _CharT, class _Traits, class _Alloc>
   1.862 +__size_type__
   1.863 +basic_string<_CharT, _Traits, _Alloc>::find_last_not_of(_CharT __c, size_type __pos) const
   1.864  {
   1.865    const size_type __len = size();
   1.866 -
   1.867 -  if (__len < 1)
   1.868 +  if ( __len < 1 ) {
   1.869      return npos;
   1.870 -  else {
   1.871 -    const_iterator __last = begin() + (min) (__len - 1, __pos) + 1;
   1.872 -    const_reverse_iterator __rlast = const_reverse_iterator(__last);
   1.873 -    const_reverse_iterator __rresult =
   1.874 -      _STLP_STD::find_if(__rlast, rend(),
   1.875 -			 _Neq_char_bound<_Traits>(__c));
   1.876 -    return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
   1.877    }
   1.878 +  const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;
   1.879 +  const_reverse_iterator __rlast = const_reverse_iterator(__last);
   1.880 +  const_reverse_iterator __rresult =
   1.881 +    _STLP_STD::find_if(__rlast, rend(),
   1.882 +                       _STLP_PRIV _Neq_char_bound<_Traits>(__c));
   1.883 +  return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
   1.884  }
   1.885  
   1.886 -template <class _CharT, class _Traits, class _Alloc> 
   1.887 +#if !defined (basic_string)
   1.888 +_STLP_MOVE_TO_PRIV_NAMESPACE
   1.889 +#endif
   1.890 +
   1.891 +template <class _CharT, class _Traits, class _Alloc>
   1.892  void _STLP_CALL _S_string_copy(const basic_string<_CharT,_Traits,_Alloc>& __s,
   1.893 -                    _CharT* __buf,
   1.894 -                    size_t __n)
   1.895 -{
   1.896 +                               _CharT* __buf, size_t __n) {
   1.897    if (__n > 0) {
   1.898      __n = (min) (__n - 1, __s.size());
   1.899      _STLP_STD::copy(__s.begin(), __s.begin() + __n, __buf);
   1.900      __buf[__n] = _CharT();
   1.901    }
   1.902  }
   1.903 -_STLP_END_NAMESPACE
   1.904  
   1.905 -// _string_fwd has to see clean basic_string
   1.906 -# undef basic_string
   1.907 -
   1.908 -# if !defined (_STLP_LINK_TIME_INSTANTIATION)
   1.909 -#  include <stl/_string_fwd.c> 
   1.910 -# endif
   1.911 -
   1.912 -# ifdef _STLP_DEBUG
   1.913 -#  define basic_string _Nondebug_string
   1.914 -# endif
   1.915 -
   1.916 -# include <stl/_range_errors.h>  
   1.917 -_STLP_BEGIN_NAMESPACE
   1.918 -
   1.919 -// _String_base methods
   1.920 -template <class _Tp, class _Alloc> 
   1.921 -void _String_base<_Tp,_Alloc>::_M_throw_length_error() const {
   1.922 -    __stl_throw_length_error("basic_string");
   1.923 -}
   1.924 -
   1.925 -template <class _Tp, class _Alloc> 
   1.926 -void _String_base<_Tp, _Alloc>::_M_throw_out_of_range() const {
   1.927 -    __stl_throw_out_of_range("basic_string");
   1.928 -}
   1.929 -
   1.930 -template <class _Tp, class _Alloc> 
   1.931 -void _String_base<_Tp, _Alloc>::_M_allocate_block(size_t __n) {  
   1.932 -  if ((__n <= (max_size()+1)) && (__n>0)){ 
   1.933 -    _M_start  = _M_end_of_storage.allocate(__n); 
   1.934 -    _M_finish = _M_start; 
   1.935 -    _M_end_of_storage._M_data = _M_start + __n; 
   1.936 -  } 
   1.937 -    else 
   1.938 -      _M_throw_length_error(); 
   1.939 -} 
   1.940 - 
   1.941 -template <class _CharT, class _Traits, class _Alloc> 
   1.942 -_STLP_EXP_DECLSPEC basic_string<_CharT, _Traits, _Alloc>::basic_string()
   1.943 -  : _String_base<_CharT,_Alloc>(allocator_type()) 
   1.944 -{  
   1.945 -  this->_M_start = this->_M_end_of_storage.allocate(8); 
   1.946 -  this->_M_finish = this->_M_start; 
   1.947 -  this->_M_end_of_storage._M_data = this->_M_start + 8; 
   1.948 -  _M_terminate_string();  
   1.949 -  _STLP_POP_CLEANUP_ITEM
   1.950 -} 
   1.951 -
   1.952 -
   1.953 -template <class _CharT, class _Traits, class _Alloc> 
   1.954 -_STLP_EXP_DECLSPEC basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT* __s, 
   1.955 -						    const allocator_type& __a) 
   1.956 -  : _String_base<_CharT,_Alloc>(__a)  
   1.957 -{ 
   1.958 -  _STLP_FIX_LITERAL_BUG(__s) 
   1.959 -    _M_range_initialize(__s, __s + traits_type::length(__s));  
   1.960 -  _STLP_POP_CLEANUP_ITEM
   1.961 -} 
   1.962 -
   1.963 -
   1.964 -template <class _CharT, class _Traits, class _Alloc> 
   1.965 -_STLP_EXP_DECLSPEC basic_string<_CharT, _Traits, _Alloc>::basic_string(const basic_string<_CharT, _Traits, _Alloc> & __s)  
   1.966 -  : _String_base<_CharT,_Alloc>(__s.get_allocator())  
   1.967 -{  
   1.968 -  _M_range_initialize(__s._M_start, __s._M_finish);  
   1.969 -  _STLP_POP_CLEANUP_ITEM
   1.970 -} 
   1.971 -  
   1.972 -# if defined ( __SUNPRO_CC) && ! defined(_STLP_STATIC_CONST_INIT_BUG)
   1.973 -template <class _CharT, class _Traits, class _Alloc> const size_t basic_string<_CharT, _Traits, _Alloc>::npos;
   1.974 -# endif
   1.975 +_STLP_MOVE_TO_STD_NAMESPACE
   1.976  
   1.977  _STLP_END_NAMESPACE
   1.978  
   1.979 -# undef basic_string
   1.980 -# undef __size_type__
   1.981 -# undef size_type
   1.982 -# undef iterator
   1.983 -# endif /* NATIVE */
   1.984 +#include <stl/_range_errors.h>
   1.985 +
   1.986 +_STLP_BEGIN_NAMESPACE
   1.987 +
   1.988 +_STLP_MOVE_TO_PRIV_NAMESPACE
   1.989 +
   1.990 +// _String_base methods
   1.991 +template <class _Tp, class _Alloc>
   1.992 +void _String_base<_Tp,_Alloc>::_M_throw_length_error() const
   1.993 +{ __stl_throw_length_error("basic_string"); }
   1.994 +
   1.995 +template <class _Tp, class _Alloc>
   1.996 +void _String_base<_Tp, _Alloc>::_M_throw_out_of_range() const
   1.997 +{ __stl_throw_out_of_range("basic_string"); }
   1.998 +
   1.999 +template <class _Tp, class _Alloc>
  1.1000 +void _String_base<_Tp, _Alloc>::_M_allocate_block(size_t __n) {
  1.1001 +  if ((__n <= (max_size() + 1)) && (__n > 0)) {
  1.1002 +#if defined (_STLP_USE_SHORT_STRING_OPTIM)
  1.1003 +    if (__n > _DEFAULT_SIZE) {
  1.1004 +      this->_M_buffers._M_dynamic_buf = _M_end_of_storage.allocate(__n, __n);
  1.1005 +      this->_M_finish = this->_M_buffers._M_dynamic_buf;
  1.1006 +      this->_M_end_of_storage._M_data = this->_M_finish + __n;
  1.1007 +    }
  1.1008 +#else
  1.1009 +    this->_M_start  = _M_end_of_storage.allocate(__n, __n);
  1.1010 +    this->_M_finish = this->_M_start;
  1.1011 +    this->_M_end_of_storage._M_data = this->_M_finish + __n;
  1.1012 +#endif /*_STLP_USE_SHORT_STRING_OPTIM  */
  1.1013 +  } else {
  1.1014 +    this->_M_throw_length_error();
  1.1015 +  }
  1.1016 +}
  1.1017 +
  1.1018 +#if !defined (basic_string)
  1.1019 +_STLP_MOVE_TO_STD_NAMESPACE
  1.1020 +#endif
  1.1021 +
  1.1022 +#if defined (_STLP_DONT_SUP_DFLT_PARAM)
  1.1023 +template <class _CharT, class _Traits, class _Alloc>
  1.1024 +basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT* __s)
  1.1025 +  : _STLP_PRIV _String_base<_CharT,_Alloc>(allocator_type()) {
  1.1026 +  _STLP_FIX_LITERAL_BUG(__s)
  1.1027 +  _M_range_initialize(__s, __s + traits_type::length(__s));
  1.1028 +}
  1.1029 +#endif
  1.1030 +
  1.1031 +template <class _CharT, class _Traits, class _Alloc>
  1.1032 +basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT* __s,
  1.1033 +                                                    const allocator_type& __a)
  1.1034 +  : _STLP_PRIV _String_base<_CharT,_Alloc>(__a) {
  1.1035 +  _STLP_FIX_LITERAL_BUG(__s)
  1.1036 +  _M_range_initialize(__s, __s + traits_type::length(__s));
  1.1037 +}
  1.1038 +
  1.1039 +template <class _CharT, class _Traits, class _Alloc>
  1.1040 +basic_string<_CharT, _Traits, _Alloc>::basic_string(const basic_string<_CharT, _Traits, _Alloc> & __s)
  1.1041 +  : _STLP_PRIV _String_base<_CharT,_Alloc>(__s.get_allocator())
  1.1042 +{ _M_range_initialize(__s._M_Start(), __s._M_Finish()); }
  1.1043 +
  1.1044 +#if defined (basic_string)
  1.1045 +_STLP_MOVE_TO_STD_NAMESPACE
  1.1046 +#  undef basic_string
  1.1047 +#else
  1.1048 +/* If basic_string is defined it means that it won't be the basic_string class
  1.1049 + * exposed to STLport users so npos do not need external linkage.
  1.1050 + */
  1.1051 +#  if !defined (_STLP_STATIC_CONST_INIT_BUG)
  1.1052 +#    if !defined (__GNUC__) || (__GNUC__ != 2) || (__GNUC_MINOR__ != 96)
  1.1053 +template <class _CharT, class _Traits, class _Alloc>
  1.1054 +const size_t basic_string<_CharT, _Traits, _Alloc>::npos;
  1.1055 +#    endif
  1.1056 +#  endif
  1.1057 +#endif
  1.1058 +
  1.1059 +_STLP_END_NAMESPACE
  1.1060 +
  1.1061 +#undef __size_type__
  1.1062 +#if defined (_STLP_NESTED_TYPE_PARAM_BUG)
  1.1063 +#  undef size_type
  1.1064 +#  undef iterator
  1.1065 +#endif
  1.1066  
  1.1067  #endif /*  _STLP_STRING_C */
  1.1068