⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _string.h

📁 stl的源码
💻 H
📖 第 1 页 / 共 3 页
字号:
/* * Copyright (c) 1997-1999 * Silicon Graphics Computer Systems, Inc. * * Copyright (c) 1999 * Boris Fomitchev * * This material is provided "as is", with absolutely no warranty expressed * or implied. Any use is at your own risk. * * Permission to use or copy this software for any purpose is hereby granted * without fee, provided the above notices are retained on all copies. * Permission to modify the code and to distribute modified code is granted, * provided the above notices are retained, and a notice that the code was * modified is included with the above copyright notice. * */#ifndef _STLP_INTERNAL_STRING_H#define _STLP_INTERNAL_STRING_H#ifndef _STLP_INTERNAL_ALLOC_H#  include <stl/_alloc.h>#endif#ifndef _STLP_STRING_FWD_H#  include <stl/_string_fwd.h>#endif#ifndef _STLP_INTERNAL_FUNCTION_BASE_H#  include <stl/_function_base.h>#endif#ifndef _STLP_INTERNAL_ALGOBASE_H#  include <stl/_algobase.h>#endif#ifndef _STLP_INTERNAL_ITERATOR_H#  include <stl/_iterator.h>#endif#ifndef _STLP_INTERNAL_UNINITIALIZED_H#  include <stl/_uninitialized.h>#endif#if defined (_STLP_USE_TEMPLATE_EXPRESSION)#  include <stl/_string_sum.h>#endif /* _STLP_USE_TEMPLATE_EXPRESSION */#if defined (__MWERKS__) && ! defined (_STLP_USE_OWN_NAMESPACE)// MSL implementation classes expect to see the definition of streampos// when this header is included. We expect this to be fixed in later MSL// implementations#  if !defined( __MSL_CPP__ ) || __MSL_CPP__ < 0x4105#    include <stl/msl_string.h>#  endif#endif // __MWERKS__/* * Standard C++ string class.  This class has performance * characteristics very much like vector<>, meaning, for example, that * it does not perform reference-count or copy-on-write, and that * concatenation of two strings is an O(N) operation. * There are three reasons why basic_string is not identical to * vector. * First, basic_string always stores a null character at the end; * this makes it possible for c_str to be a fast operation. * Second, the C++ standard requires basic_string to copy elements * using char_traits<>::assign, char_traits<>::copy, and * char_traits<>::move.  This means that all of vector<>'s low-level * operations must be rewritten.  Third, basic_string<> has a lot of * extra functions in its interface that are convenient but, strictly * speaking, redundant. */#include <stl/_string_base.h>_STLP_BEGIN_NAMESPACE// ------------------------------------------------------------// Class basic_string.// Class invariants:// (1) [start, finish) is a valid range.// (2) Each iterator in [start, finish) points to a valid object//     of type value_type.// (3) *finish is a valid object of type value_type; when//     value_type is not a POD it is value_type().// (4) [finish + 1, end_of_storage) is a valid range.// (5) Each iterator in [finish + 1, end_of_storage) points to//     unininitialized memory.// Note one important consequence: a string of length n must manage// a block of memory whose size is at least n + 1._STLP_MOVE_TO_PRIV_NAMESPACEstruct _String_reserve_t {};_STLP_MOVE_TO_STD_NAMESPACE#if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)#  define basic_string _STLP_NO_MEM_T_NAME(str)#elif defined (_STLP_DEBUG)#  define basic_string _STLP_NON_DBG_NAME(str)#endif#if defined (basic_string)_STLP_MOVE_TO_PRIV_NAMESPACE#endif#if defined (__DMC__)#  define _STLP_PRIVATE public#elif defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)#  define _STLP_PRIVATE protected#else#  define _STLP_PRIVATE private#endiftemplate <class _CharT, class _Traits, class _Alloc>class basic_string : _STLP_PRIVATE _STLP_PRIV _String_base<_CharT,_Alloc>#if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (basic_string)                   , public __stlport_class<basic_string<_CharT, _Traits, _Alloc> >#endif{_STLP_PRIVATE:                        // Private members inherited from base.  typedef _STLP_PRIV _String_base<_CharT,_Alloc> _Base;  typedef basic_string<_CharT, _Traits, _Alloc> _Self;public:  typedef _CharT value_type;  typedef _Traits traits_type;  typedef value_type* pointer;  typedef const value_type* const_pointer;  typedef value_type& reference;  typedef const value_type& const_reference;  typedef typename _Base::size_type size_type;  typedef ptrdiff_t difference_type;  typedef random_access_iterator_tag _Iterator_category;  typedef const value_type* const_iterator;  typedef value_type*       iterator;  _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;#include <stl/_string_npos.h>  typedef _STLP_PRIV _String_reserve_t _Reserve_t;public:                         // Constructor, destructor, assignment.  typedef typename _Base::allocator_type allocator_type;  allocator_type get_allocator() const  { return _STLP_CONVERT_ALLOCATOR((const allocator_type&)this->_M_start_of_storage, _CharT); }#if !defined (_STLP_DONT_SUP_DFLT_PARAM)  explicit basic_string(const allocator_type& __a = allocator_type())#else  basic_string()      : _STLP_PRIV _String_base<_CharT,_Alloc>(allocator_type(), _Base::_DEFAULT_SIZE)  { _M_terminate_string(); }  explicit basic_string(const allocator_type& __a)#endif      : _STLP_PRIV _String_base<_CharT,_Alloc>(__a, _Base::_DEFAULT_SIZE)  { _M_terminate_string(); }#if !defined (_STLP_DONT_SUP_DFLT_PARAM)  basic_string(_Reserve_t, size_t __n,               const allocator_type& __a = allocator_type())#else  basic_string(_Reserve_t, size_t __n)    : _STLP_PRIV _String_base<_CharT,_Alloc>(allocator_type(), __n + 1)  { _M_terminate_string(); }  basic_string(_Reserve_t, size_t __n, const allocator_type& __a)#endif    : _STLP_PRIV _String_base<_CharT,_Alloc>(__a, __n + 1)  { _M_terminate_string(); }  basic_string(const _Self&);#if !defined (_STLP_DONT_SUP_DFLT_PARAM)  basic_string(const _Self& __s, size_type __pos, size_type __n = npos,               const allocator_type& __a = allocator_type())#else  basic_string(const _Self& __s, size_type __pos)    : _STLP_PRIV _String_base<_CharT,_Alloc>(allocator_type()) {    if (__pos > __s.size())      this->_M_throw_out_of_range();    else      _M_range_initialize(__s._M_Start() + __pos, __s._M_Finish());  }  basic_string(const _Self& __s, size_type __pos, size_type __n)    : _STLP_PRIV _String_base<_CharT,_Alloc>(allocator_type()) {    if (__pos > __s.size())      this->_M_throw_out_of_range();    else      _M_range_initialize(__s._M_Start() + __pos,                          __s._M_Start() + __pos + (min) (__n, __s.size() - __pos));  }  basic_string(const _Self& __s, size_type __pos, size_type __n,               const allocator_type& __a)#endif    : _STLP_PRIV _String_base<_CharT,_Alloc>(__a) {    if (__pos > __s.size())      this->_M_throw_out_of_range();    else      _M_range_initialize(__s._M_Start() + __pos,                          __s._M_Start() + __pos + (min) (__n, __s.size() - __pos));  }#if !defined (_STLP_DONT_SUP_DFLT_PARAM)  basic_string(const _CharT* __s, size_type __n,               const allocator_type& __a = allocator_type())#else  basic_string(const _CharT* __s, size_type __n)    : _STLP_PRIV _String_base<_CharT,_Alloc>(allocator_type()) {      _STLP_FIX_LITERAL_BUG(__s)      _M_range_initialize(__s, __s + __n);    }  basic_string(const _CharT* __s, size_type __n, const allocator_type& __a)#endif    : _STLP_PRIV _String_base<_CharT,_Alloc>(__a) {      _STLP_FIX_LITERAL_BUG(__s)      _M_range_initialize(__s, __s + __n);    }#if !defined (_STLP_DONT_SUP_DFLT_PARAM)  basic_string(const _CharT* __s,               const allocator_type& __a = allocator_type());#else  basic_string(const _CharT* __s);  basic_string(const _CharT* __s, const allocator_type& __a);#endif#if !defined (_STLP_DONT_SUP_DFLT_PARAM)  basic_string(size_type __n, _CharT __c,               const allocator_type& __a = allocator_type())#else  basic_string(size_type __n, _CharT __c)    : _STLP_PRIV _String_base<_CharT,_Alloc>(allocator_type(), __n + 1) {    this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_Start(), __n, __c);    _M_terminate_string();  }  basic_string(size_type __n, _CharT __c, const allocator_type& __a)#endif    : _STLP_PRIV _String_base<_CharT,_Alloc>(__a, __n + 1) {    this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_Start(), __n, __c);    _M_terminate_string();  }#if !defined (_STLP_NO_MOVE_SEMANTIC)  basic_string(__move_source<_Self> src)    : _STLP_PRIV _String_base<_CharT,_Alloc>(__move_source<_Base>(src.get())) {}#endif  // Check to see if _InputIterator is an integer type.  If so, then  // it can't be an iterator.#if defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)  template <class _InputIterator>  basic_string(_InputIterator __f, _InputIterator __l,               const allocator_type & __a _STLP_ALLOCATOR_TYPE_DFL)    : _STLP_PRIV _String_base<_CharT,_Alloc>(__a) {    typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;    _M_initialize_dispatch(__f, __l, _Integral());  }#  if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)  template <class _InputIterator>  basic_string(_InputIterator __f, _InputIterator __l)    : _STLP_PRIV _String_base<_CharT,_Alloc>(allocator_type()) {    typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;    _M_initialize_dispatch(__f, __l, _Integral());  }#  endif#else#  if !defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)  basic_string(const _CharT* __f, const _CharT* __l,               const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)    : _STLP_PRIV _String_base<_CharT,_Alloc>(__a) {    _STLP_FIX_LITERAL_BUG(__f)  _STLP_FIX_LITERAL_BUG(__l)    _M_range_initialize(__f, __l);  }#    if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)  basic_string(const _CharT* __f, const _CharT* __l)    : _STLP_PRIV _String_base<_CharT,_Alloc>(allocator_type()) {    _STLP_FIX_LITERAL_BUG(__f)  _STLP_FIX_LITERAL_BUG(__l)    _M_range_initialize(__f, __l);  }#    endif#  endif#  if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)  /* We need an additionnal constructor to build an empty string without   * any allocation or termination char*/protected:  struct _CalledFromWorkaround_t {};  basic_string(_CalledFromWorkaround_t, const allocator_type &__a)    : _String_base<_CharT,_Alloc>(__a) {}#  endif#endif_STLP_PRIVATE:  size_type _M_compute_next_size(size_type __n) {    const size_type __size = size();    if (__n > max_size() - __size)      this->_M_throw_length_error();    size_type __len = __size + (max)(__n, __size) + 1;    if (__len > max_size() || __len < __size)      __len = max_size(); // overflow    return __len;  }  template <class _InputIter>  void _M_range_initialize(_InputIter __f, _InputIter __l,                           const input_iterator_tag &__tag) {    this->_M_allocate_block();    _M_construct_null(this->_M_Finish());    _M_appendT(__f, __l, __tag);  }  template <class _ForwardIter>  void _M_range_initialize(_ForwardIter __f, _ForwardIter __l,                           const forward_iterator_tag &) {    difference_type __n = _STLP_STD::distance(__f, __l);    this->_M_allocate_block(__n + 1);    this->_M_finish = uninitialized_copy(__f, __l, this->_M_Start());    this->_M_terminate_string();  }  template <class _InputIter>  void _M_range_initializeT(_InputIter __f, _InputIter __l) {    _M_range_initialize(__f, __l, _STLP_ITERATOR_CATEGORY(__f, _InputIter));  }  template <class _Integer>  void _M_initialize_dispatch(_Integer __n, _Integer __x, const __true_type& /*_Integral*/) {    this->_M_allocate_block(__n + 1);    this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_Start(), __n, __x);    this->_M_terminate_string();  }  template <class _InputIter>  void _M_initialize_dispatch(_InputIter __f, _InputIter __l, const __false_type& /*_Integral*/) {    _M_range_initializeT(__f, __l);  }public:  _Self& operator=(const _Self& __s) {    if (&__s != this)      _M_assign(__s._M_Start(), __s._M_Finish());    return *this;  }  _Self& operator=(const _CharT* __s) {    _STLP_FIX_LITERAL_BUG(__s)    return _M_assign(__s, __s + traits_type::length(__s));  }  _Self& operator=(_CharT __c)  { return assign(__STATIC_CAST(size_type,1), __c); }private:  static _CharT _STLP_CALL _M_null()  { return _STLP_DEFAULT_CONSTRUCTED(_CharT); }_STLP_PRIVATE:                     // Helper functions used by constructors                                   // and elsewhere.  void _M_construct_null(_CharT* __p) const  { _STLP_STD::_Construct(__p); }  void _M_terminate_string()  { _M_construct_null(this->_M_Finish()); }  bool _M_inside(const _CharT* __s) const {    _STLP_FIX_LITERAL_BUG(__s)    return (__s >= this->_M_Start()) && (__s < this->_M_Finish());  }  void _M_range_initialize(const _CharT* __f, const _CharT* __l) {    _STLP_FIX_LITERAL_BUG(__f) _STLP_FIX_LITERAL_BUG(__l)    ptrdiff_t __n = __l - __f;    this->_M_allocate_block(__n + 1);    this->_M_finish = uninitialized_copy(__f, __l, this->_M_Start());    _M_terminate_string();  }public:                         // Iterators.  iterator begin()             { return this->_M_Start(); }  iterator end()               { return this->_M_Finish(); }  const_iterator begin() const { return this->_M_Start(); }  const_iterator end()   const { return this->_M_Finish(); }  reverse_iterator rbegin()  { return reverse_iterator(this->_M_Finish()); }  reverse_iterator rend()  { return reverse_iterator(this->_M_Start()); }  const_reverse_iterator rbegin() const  { return const_reverse_iterator(this->_M_Finish()); }

⌨️ 快捷键说明

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