📄 _string.h
字号:
/* * 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 can always stores a null character * at the end (macro dependent); 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. * Additionally, the C++ standard imposes a major restriction: according * to the standard, the character type _CharT must be a POD type. This * implementation weakens that restriction, and allows _CharT to be a * a user-defined non-POD type. However, _CharT must still have a * default constructor. */#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.struct _String_reserve_t {};#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#endiftemplate <class _CharT, class _Traits, class _Alloc>class basic_string : protected _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{protected: // Protected members inherited from base. typedef _STLP_PRIV _String_base<_CharT,_Alloc> _Base; typedef basic_string<_CharT, _Traits, _Alloc> _Self; // fbp : used to optimize char/wchar_t cases, and to simplify // _STLP_DEF_CONST_PLCT_NEW_BUG problem workaround typedef typename _IsIntegral<_CharT>::_Ret _Char_Is_Integral; typedef typename _IsPOD<_CharT>::_Type _Char_Is_POD; typedef random_access_iterator_tag r_a_i_t;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 _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_end_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) {# if defined (_STLP_USE_SHORT_STRING_OPTIM) if (this->_M_using_static_buf()) { _Traits::assign(this->_M_Start(), __n, __c); this->_M_finish = this->_M_Start() + __n; } else# endif 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) {#if defined (_STLP_USE_SHORT_STRING_OPTIM) if (this->_M_using_static_buf()) { _Traits::assign(this->_M_Start(), __n, __c); this->_M_finish = this->_M_Start() + __n; } else#endif this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_Start(), __n, __c); _M_terminate_string(); } basic_string(__move_source<_Self> src) : _STLP_PRIV _String_base<_CharT,_Alloc>(__move_source<_Base>(src.get())) {} // Check to see if _InputIterator is an integer type. If so, then // it can't be an iterator.#if defined (_STLP_MEMBER_TEMPLATES) && !(defined (__MRC__) || (defined(__SC__) && !defined(__DMC__))) //*ty 04/30/2001 - mpw compilers choke on this ctor# if !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 /* 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) {}public:# endif /* _STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND */#endif /* !__MRC__ || (__SC__ && !__DMC__) */#if !(defined (_STLP_MEMBER_TEMPLATES) && !(defined (__MRC__) || (defined (__SC__) && !defined (__DMC__)))) || \ !defined (_STLP_NO_METHOD_SPECIALIZATION) && !defined (_STLP_NO_EXTENSIONS) || \ 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 /* _STLP_MEMBER_TEMPLATES */private: 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()); _STLP_TRY { _M_appendT(__f, __l, __tag); } _STLP_UNWIND(this->_M_destroy_range()) } template <class _ForwardIter> void _M_range_initialize(_ForwardIter __f, _ForwardIter __l, const forward_iterator_tag &) { difference_type __n = distance(__f, __l); this->_M_allocate_block(__n + 1);#if defined (_STLP_USE_SHORT_STRING_OPTIM) if (this->_M_using_static_buf()) { _M_copyT(__f, __l, this->_M_Start()); this->_M_finish = this->_M_Start() + __n; } else#endif /* _STLP_USE_SHORT_STRING_OPTIM */ 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);#if defined (_STLP_USE_SHORT_STRING_OPTIM)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -