_string.c

来自「stl的源码」· C语言 代码 · 共 674 行 · 第 1/2 页

C
674
字号
          else {            //The source buffer hasn't move, it has been duplicated            _M_move(__first, __last, __pos);          }        }      }      else {        const_iterator __mid = __first;        __mid += __elems_after + 1;        _STLP_PRIV __ucopy(__mid, __last, this->_M_finish + 1);        this->_M_finish += __n - __elems_after;        _STLP_PRIV __ucopy(__pos, __old_finish + 1, this->_M_finish);        this->_M_finish += __elems_after;        if (!__self_ref)          _M_copy(__first, __mid, __pos);        else          _M_move(__first, __mid, __pos);      }    }    else {      size_type __len = _M_compute_next_size(__n);      pointer __new_start = this->_M_start_of_storage.allocate(__len, __len);      pointer __new_finish = _STLP_PRIV __ucopy(this->_M_Start(), __pos, __new_start);      __new_finish = _STLP_PRIV __ucopy(__first, __last, __new_finish);      __new_finish = _STLP_PRIV __ucopy(__pos, this->_M_finish, __new_finish);      _M_construct_null(__new_finish);      this->_M_deallocate_block();      this->_M_reset(__new_start, __new_finish, __new_start + __len);    }  }}template <class _CharT, class _Traits, class _Alloc>basic_string<_CharT,_Traits,_Alloc>&basic_string<_CharT,_Traits,_Alloc> ::replace(iterator __first, iterator __last,                                              size_type __n, _CharT __c) {  size_type __len = (size_type)(__last - __first);  if (__len >= __n) {    _Traits::assign(__first, __n, __c);    erase(__first + __n, __last);  }  else {    _Traits::assign(__first, __len, __c);    insert(__last, __n - __len, __c);  }  return *this;}template <class _CharT, class _Traits, class _Alloc>basic_string<_CharT,_Traits,_Alloc>&basic_string<_CharT,_Traits,_Alloc> ::_M_replace(iterator __first, iterator __last,                                                 const _CharT* __f, const _CharT* __l,                                                 bool __self_ref) {  const ptrdiff_t       __n = __l - __f;  const difference_type __len = __last - __first;  if (__len >= __n) {    if (!__self_ref || __l < __first || __f >= __last)      _M_copy(__f, __l, __first);    else      _M_move(__f, __l, __first);    erase(__first + __n, __last);  } else if (!__self_ref || (__f >= __last) || (__l <= __first)) { // no overlap    const_iterator __m = __f + __len;    _M_copy(__f, __m, __first);    _M_insert(__last, __m, __l, __self_ref );  } else if (__f < __first) { // we have to take care of overlaping    const_iterator __m = __f + __len;    // We have to deal with possible reallocation because we do insert first.    const difference_type __off_dest = __first - this->begin();    const difference_type __off_src = __f - this->begin();    _M_insert(__last, __m, __l, true);    _Traits::move(begin() + __off_dest, begin() + __off_src, __len);  } else {    const_iterator __m = __f + __len;    _Traits::move(__first, __f, __len);    _M_insert(__last, __m, __l, true);  }  return *this;}template <class _CharT, class _Traits, class _Alloc>__size_type__ basic_string<_CharT,_Traits,_Alloc>::find( const _CharT* __s, size_type __pos,                                                         size_type __n) const{  const size_t __len = size();  if (__pos >= __len || __pos + __n > __len) {    if ( __n == 0 && __pos <= __len ) { // marginal case      return __pos;    }    return npos;  }  const_pointer __result =    _STLP_STD::search(this->_M_Start() + __pos, this->_M_Finish(),                      __s, __s + __n, _STLP_PRIV _Eq_traits<_Traits>());  return __result != this->_M_Finish() ? __result - this->_M_Start() : npos;}template <class _CharT, class _Traits, class _Alloc>__size_type__ basic_string<_CharT,_Traits,_Alloc>::find(_CharT __c, size_type __pos) const{  if (__pos >= size()) { /*__pos + 1 > size()*/    return npos;  }  const_pointer __result =    _STLP_STD::find_if(this->_M_Start() + __pos, this->_M_Finish(),                       _STLP_PRIV _Eq_char_bound<_Traits>(__c));  return __result != this->_M_Finish() ? __result - this->_M_Start() : npos;}template <class _CharT, class _Traits, class _Alloc>__size_type__ basic_string<_CharT,_Traits,_Alloc>::rfind(const _CharT* __s, size_type __pos,                                                         size_type __n) const{  const size_type __len = size();  if ( __len < __n ) {    return npos;  }  const_pointer __last = this->_M_Start() + (min)( __len - __n, __pos) + __n;  if ( __n == 0 ) { // marginal case    return __last - this->_M_Start();  }  const_pointer __result = _STLP_STD::find_end(this->_M_Start(), __last,                                               __s, __s + __n, _STLP_PRIV _Eq_traits<_Traits>());  return __result != __last ? __result - this->_M_Start() : npos;}template <class _CharT, class _Traits, class _Alloc>__size_type__ basic_string<_CharT,_Traits,_Alloc>::rfind(_CharT __c, size_type __pos) const{  const size_type __len = size();  if ( __len < 1 ) {    return npos;  }  const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;  const_reverse_iterator __rresult =    _STLP_STD::find_if(const_reverse_iterator(__last), rend(),                       _STLP_PRIV _Eq_char_bound<_Traits>(__c));  return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;}template <class _CharT, class _Traits, class _Alloc> __size_type__basic_string<_CharT,_Traits,_Alloc> ::find_first_of(const _CharT* __s, size_type __pos,                                                    size_type __n) const {  if (__pos >= size()) /*__pos + 1 > size()*/    return npos;  else {    const_iterator __result = _STLP_PRIV __str_find_first_of(begin() + __pos, end(),                                                             __s, __s + __n,                                                             __STATIC_CAST(_Traits*, 0));    return __result != end() ? __result - begin() : npos;  }}template <class _CharT, class _Traits, class _Alloc> __size_type__basic_string<_CharT,_Traits,_Alloc> ::find_last_of(const _CharT* __s, size_type __pos,                                                   size_type __n) const{  const size_type __len = size();  if ( __len < 1 ) {    return npos;  }  const const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;  const const_reverse_iterator __rresult =    _STLP_PRIV __str_find_first_of(const_reverse_iterator(__last), rend(),                                   __s, __s + __n,                                   __STATIC_CAST(_Traits*, 0));  return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;}template <class _CharT, class _Traits, class _Alloc> __size_type__basic_string<_CharT,_Traits,_Alloc> ::find_first_not_of(const _CharT* __s, size_type __pos,                                                        size_type __n) const {  typedef typename _Traits::char_type _CharType;  if (__pos >= size()) /*__pos + 1 >= size()*/    return npos;  else {    const_pointer __result = _STLP_PRIV __str_find_first_not_of(this->_M_Start() + __pos, this->_M_Finish(),                                                                __STATIC_CAST(const _CharType*, __s),                                                                __STATIC_CAST(const _CharType*, __s) + __n,                                                                __STATIC_CAST(_Traits*, 0));    return __result != this->_M_finish ? __result - this->_M_Start() : npos;  }}template <class _CharT, class _Traits, class _Alloc> __size_type__basic_string<_CharT,_Traits,_Alloc> ::find_first_not_of(_CharT __c, size_type __pos) const {  if (1 > size())    return npos;  else {    const_pointer __result = _STLP_STD::find_if(this->_M_Start() + __pos, this->_M_Finish(),                                                _STLP_PRIV _Neq_char_bound<_Traits>(__c));    return __result != this->_M_finish ? __result - this->_M_Start() : npos;  }}template <class _CharT, class _Traits, class _Alloc>__size_type__basic_string<_CharT,_Traits,_Alloc>::find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const{  typedef typename _Traits::char_type _CharType;  const size_type __len = size();  if ( __len < 1 ) {    return npos;  }  const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;  const_reverse_iterator __rlast = const_reverse_iterator(__last);  const_reverse_iterator __rresult =    _STLP_PRIV __str_find_first_not_of(__rlast, rend(),                                       __STATIC_CAST(const _CharType*, __s),                                       __STATIC_CAST(const _CharType*, __s) + __n,                                       __STATIC_CAST(_Traits*, 0));  return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;}template <class _CharT, class _Traits, class _Alloc>__size_type__basic_string<_CharT, _Traits, _Alloc>::find_last_not_of(_CharT __c, size_type __pos) const{  const size_type __len = size();  if ( __len < 1 ) {    return npos;  }  const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;  const_reverse_iterator __rlast = const_reverse_iterator(__last);  const_reverse_iterator __rresult =    _STLP_STD::find_if(__rlast, rend(),                       _STLP_PRIV _Neq_char_bound<_Traits>(__c));  return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;}#if !defined (basic_string)_STLP_MOVE_TO_PRIV_NAMESPACE#endiftemplate <class _CharT, class _Traits, class _Alloc>void _STLP_CALL _S_string_copy(const basic_string<_CharT,_Traits,_Alloc>& __s,                               _CharT* __buf, size_t __n) {  if (__n > 0) {    __n = (min) (__n - 1, __s.size());    _STLP_STD::copy(__s.begin(), __s.begin() + __n, __buf);    __buf[__n] = _CharT();  }}_STLP_MOVE_TO_STD_NAMESPACE_STLP_END_NAMESPACE#include <stl/_range_errors.h>_STLP_BEGIN_NAMESPACE_STLP_MOVE_TO_PRIV_NAMESPACE// _String_base methodstemplate <class _Tp, class _Alloc>void _String_base<_Tp,_Alloc>::_M_throw_length_error() const{ __stl_throw_length_error("basic_string"); }template <class _Tp, class _Alloc>void _String_base<_Tp, _Alloc>::_M_throw_out_of_range() const{ __stl_throw_out_of_range("basic_string"); }template <class _Tp, class _Alloc>void _String_base<_Tp, _Alloc>::_M_allocate_block(size_t __n) {  if ((__n <= (max_size() + 1)) && (__n > 0)) {#if defined (_STLP_USE_SHORT_STRING_OPTIM)    if (__n > _DEFAULT_SIZE) {      this->_M_start_of_storage._M_data = _M_start_of_storage.allocate(__n, __n);      this->_M_finish = this->_M_start_of_storage._M_data;      this->_M_buffers._M_end_of_storage = this->_M_start_of_storage._M_data + __n;    }#else    this->_M_start_of_storage._M_data = _M_start_of_storage.allocate(__n, __n);    this->_M_finish = this->_M_start_of_storage._M_data;    this->_M_end_of_storage = this->_M_start_of_storage._M_data + __n;#endif  } else {    this->_M_throw_length_error();  }}#if !defined (basic_string)_STLP_MOVE_TO_STD_NAMESPACE#endif#if defined (_STLP_DONT_SUP_DFLT_PARAM)template <class _CharT, class _Traits, class _Alloc>basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT* __s)  : _STLP_PRIV _String_base<_CharT,_Alloc>(allocator_type()) {  _STLP_FIX_LITERAL_BUG(__s)  _M_range_initialize(__s, __s + traits_type::length(__s));}#endiftemplate <class _CharT, class _Traits, class _Alloc>basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT* __s,                                                    const allocator_type& __a)  : _STLP_PRIV _String_base<_CharT,_Alloc>(__a) {  _STLP_FIX_LITERAL_BUG(__s)  _M_range_initialize(__s, __s + traits_type::length(__s));}template <class _CharT, class _Traits, class _Alloc>basic_string<_CharT, _Traits, _Alloc>::basic_string(const basic_string<_CharT, _Traits, _Alloc> & __s)  : _STLP_PRIV _String_base<_CharT,_Alloc>(__s.get_allocator()){ _M_range_initialize(__s._M_Start(), __s._M_Finish()); }#if defined (basic_string)_STLP_MOVE_TO_STD_NAMESPACE#  undef basic_string#endif#if !defined (_STLP_STATIC_CONST_INIT_BUG) && !defined (_STLP_NO_STATIC_CONST_DEFINITION)template <class _CharT, class _Traits, class _Alloc>const size_t basic_string<_CharT, _Traits, _Alloc>::npos;#endif_STLP_END_NAMESPACE#undef __size_type__#if defined (_STLP_NESTED_TYPE_PARAM_BUG)#  undef size_type#  undef iterator#endif#endif /*  _STLP_STRING_C */// Local Variables:// mode:C++// End:

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?