📄 stl_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 __SGI_STL_STRING_H
#define __SGI_STL_STRING_H
# ifndef __STL_CONFIG_H
# include <stl_config.h>
# endif
#if !defined (__STLPORT_DEBUG_H) && (defined (__STL_DEBUG) || defined (__STL_ASSERTIONS))
# include <stldebug.h>
#endif
# if defined (__STL_DEBUG)
# define _Make_iterator(__i) iterator(&_M_iter_list, __i)
# define _Make_const_iterator(__i) const_iterator(&_M_iter_list, __i)
# define _Make_ptr(__i) (__i)._M_iterator
# else
# define _Make_iterator(__i) __i
# define _Make_const_iterator(__i) __i
# define _Make_ptr(__i) __i
# endif
# ifndef __STLPORT_CCTYPE
# include <cctype>
# endif
#ifndef __SGI_STL_STRING_FWD_H
# include <stl_string_fwd.h>
#endif
#ifndef __SGI_STL_INTERNAL_FUNCTION_H
# include <stl_function.h>
#endif
# include <stl_ctraits_fns.h>
#ifndef __SGI_STDEXCEPT
# include <stdexcept>
#endif
#ifndef __SGI_STL_MEMORY
# include <memory>
#endif
#ifndef __SGI_STL_ALGORITHM
# include <algorithm>
#endif
# ifndef __STLPORT_IOSFWD
# include <iosfwd>
# endif
#if defined (__STL_DEBUG) && ! defined (__STLPORT_VEC_ITERATOR_H)
// string uses the same debug iterator as vector
# include <stl_vec_iterator.h>
#endif
#if defined( __MWERKS__ ) && ! defined (__STL_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
# include <mcompile.h>
# if !defined( __MSL_CPP__ ) || __MSL_CPP__ < 0x4105
# include <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.
// 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.
__STL_BEGIN_NAMESPACE
#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
#pragma set woff 1174
#pragma set woff 1375
#endif
// A helper class to use a char_traits as a function object.
template <class _Traits>
struct _Not_within_traits
: public unary_function<typename _Traits::char_type, bool>
{
typedef typename _Traits::char_type _CharT;
const _CharT* _M_first;
const _CharT* _M_last;
_Not_within_traits(const typename _Traits::char_type* __f,
const typename _Traits::char_type* __l)
: _M_first(__f), _M_last(__l) {}
bool operator()(const typename _Traits::char_type& __x) const {
return find_if((_CharT*)_M_first, (_CharT*)_M_last,
bind1st(_Eq_traits<_Traits>(), __x)) == (_CharT*)_M_last;
}
};
// ------------------------------------------------------------
// Class _String_base.
// _String_base is a helper class that makes it it easier to write an
// exception-safe version of basic_string. The constructor allocates,
// but does not initialize, a block of memory. The destructor
// deallocates, but does not destroy elements within, a block of
// memory. The destructor assumes that _M_start either is null, or else
// points to a block of memory that was allocated using _String_base's
// allocator and whose size is _M_end_of_storage._M_data - _M_start.
// Additionally, _String_base encapsulates the difference between
// old SGI-style allocators and standard-conforming allocators.
template <class _Tp, class _Alloc> class _String_base {
public:
typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type allocator_type;
_Tp* _M_start;
_Tp* _M_finish;
_STL_alloc_proxy<_Tp*, _Tp, allocator_type> _M_end_of_storage;
// Precondition: 0 < __n <= max_size().
void _M_allocate_block(size_t __n) {
if (__n <= max_size()) {
_M_start = _M_end_of_storage.allocate(__n);
_M_finish = _M_start;
_M_end_of_storage._M_data = _M_start + __n;
}
else
_M_throw_length_error();
}
void _M_deallocate_block()
{ _M_end_of_storage.deallocate(_M_start, _M_end_of_storage._M_data - _M_start); }
size_t max_size() const { return (size_t(-1) / sizeof(_Tp)) - 1; }
_String_base(const allocator_type& __a)
: _M_start(0), _M_finish(0), _M_end_of_storage(__a, (_Tp*)0) { }
_String_base(const allocator_type& __a, size_t __n)
: _M_start(0), _M_finish(0), _M_end_of_storage(__a, (_Tp*)0)
{ _M_allocate_block(__n); }
~_String_base() { _M_deallocate_block(); }
void _M_throw_length_error() const;
void _M_throw_out_of_range() const;
};
// ------------------------------------------------------------
// 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; in particular,
// 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 {};
template <class _CharT, class _Traits, class _Alloc>
class basic_string : protected _String_base<_CharT,_Alloc> {
private: // Protected members inherited from base.
typedef _String_base<_CharT,_Alloc> _Base;
typedef basic_string<_CharT, _Traits, _Alloc> _Self;
// fbp : used to optimize char/wchar_t cases, and to simplify
// __STL_DEFAULT_CONSTRUCTOR_BUG problem workaround
typedef typename _Is_integer<_CharT>::_Integral _Char_Is_Integral;
public:
#if defined( __STL_HAS_NAMESPACES )
__STL_USING_BASE_MEMBER _String_base<_CharT,_Alloc>::_M_allocate_block;
# ifndef __STL_DEBUG
__STL_USING_BASE_MEMBER _String_base<_CharT,_Alloc>::_M_deallocate_block;
# endif
__STL_USING_BASE_MEMBER _String_base<_CharT,_Alloc>::_M_throw_length_error;
__STL_USING_BASE_MEMBER _String_base<_CharT,_Alloc>::_M_throw_out_of_range;
__STL_USING_BASE_MEMBER _String_base<_CharT,_Alloc>::_M_start;
__STL_USING_BASE_MEMBER _String_base<_CharT,_Alloc>::_M_finish;
__STL_USING_BASE_MEMBER _String_base<_CharT,_Alloc>::_M_end_of_storage;
#endif /* __STL_HAS_NAMESPACES */
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 size_t size_type;
typedef ptrdiff_t difference_type;
# if defined (__STL_DEBUG)
typedef _Vec_iter<_CharT, _Nonconst_traits<_CharT> > iterator;
typedef _Vec_iter<_CharT, _Const_traits<_CharT> > const_iterator;
private:
mutable __owned_list _M_iter_list;
public:
# else
typedef const value_type* const_iterator;
typedef value_type* iterator;
# endif
#if defined ( __STL_CLASS_PARTIAL_SPECIALIZATION ) && \
! defined (__STL_PARTIAL_SPECIALIZATION_BUG)
typedef __STLPORT_STD::reverse_iterator<const_iterator> const_reverse_iterator;
typedef __STLPORT_STD::reverse_iterator<iterator> reverse_iterator;
#else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
# if defined (__STL_MSVC50_COMPATIBILITY)
typedef __STLPORT_STD::reverse_iterator<const_iterator, value_type, const_reference,
const_pointer, difference_type> const_reverse_iterator;
typedef __STLPORT_STD::reverse_iterator<iterator, value_type, reference, pointer, difference_type>
reverse_iterator;
# else
typedef __STLPORT_STD::reverse_iterator<const_iterator, value_type, const_reference,
difference_type> const_reverse_iterator;
typedef __STLPORT_STD::reverse_iterator<iterator, value_type, reference, difference_type>
reverse_iterator;
# endif
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
static const size_type npos;
typedef _String_reserve_t _Reserve_t;
# ifdef __STL_USE_OWN_NAMESPACE
// this typedef is being used for conversions
typedef __STL_VENDOR_STD::basic_string<_CharT,_Traits,
__STL_VENDOR_STD::allocator<_CharT> > __std_string;
# endif
public: // Constructor, destructor, assignment.
typedef typename _Base::allocator_type allocator_type;
allocator_type get_allocator() const {
return __STL_CONVERT_ALLOCATOR((const allocator_type&)_M_end_of_storage, _CharT);
}
basic_string()
: _String_base<_CharT,_Alloc>(allocator_type(), 8) {
__stl_debug_do(_M_iter_list._Safe_init(&_M_start));
_M_terminate_string();
}
explicit basic_string(const allocator_type& __a)
: _String_base<_CharT,_Alloc>(__a, 8) {
__stl_debug_do(_M_iter_list._Safe_init(&_M_start));
_M_terminate_string();
}
basic_string(_Reserve_t, size_t __n,
const allocator_type& __a = __STL_ALLOC_INSTANCE(allocator_type))
: _String_base<_CharT,_Alloc>(__a, __n + 1) {
__stl_debug_do(_M_iter_list._Safe_init(&_M_start));
_M_terminate_string();
}
basic_string(const _Self& __s) : _String_base<_CharT,_Alloc>(__s.get_allocator())
{
_M_range_initialize(__s._M_start, __s._M_finish);
}
basic_string(const _Self& __s, size_type __pos, size_type __n = npos,
const allocator_type& __a = __STL_ALLOC_INSTANCE(allocator_type))
: _String_base<_CharT,_Alloc>(__a) {
if (__pos > __s.size())
_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 _CharT* __s, size_type __n,
const allocator_type& __a = __STL_ALLOC_INSTANCE(allocator_type))
: _String_base<_CharT,_Alloc>(__a)
{
__STL_FIX_LITERAL_BUG(__s)
_M_range_initialize(__s, __s + __n);
}
basic_string(const _CharT* __s,
const allocator_type& __a = __STL_ALLOC_INSTANCE(allocator_type))
: _String_base<_CharT,_Alloc>(__a)
{
__STL_FIX_LITERAL_BUG(__s)
_M_range_initialize(__s, __s + _Traits::length(__s));
}
basic_string(size_type __n, _CharT __c,
const allocator_type& __a = __STL_ALLOC_INSTANCE(allocator_type))
: _String_base<_CharT,_Alloc>(__a, __n + 1)
{
__stl_debug_do(_M_iter_list._Safe_init(&_M_start));
_M_finish = uninitialized_fill_n(_M_start, __n, __c);
_M_terminate_string();
}
// Check to see if _InputIterator is an integer type. If so, then
// it can't be an iterator.
#if defined (__STL_MEMBER_TEMPLATES) && ! defined (__STL_MSVC)
template <class _InputIterator>
basic_string(_InputIterator __f, _InputIterator __l,
const allocator_type & __a = __STL_ALLOC_INSTANCE(allocator_type))
: _String_base<_CharT,_Alloc>(__a)
{
typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
_M_initialize_dispatch(__f, __l, _Integral());
}
#else /* __STL_MEMBER_TEMPLATES */
# ifdef __STL_DEBUG
basic_string(const_iterator __f, const_iterator __l)
: _String_base<_CharT,_Alloc>(allocator_type())
{
_M_range_initialize(_Make_ptr(__f), _Make_ptr(__l));
}
basic_string(const_iterator __f, const_iterator __l,
const allocator_type& __a )
: _String_base<_CharT,_Alloc>(__a)
{
_M_range_initialize(_Make_ptr(__f), _Make_ptr(__l));
}
# endif
basic_string(const _CharT* __f, const _CharT* __l,
const allocator_type& __a = __STL_ALLOC_INSTANCE(allocator_type))
: _String_base<_CharT,_Alloc>(__a)
{
__STL_FIX_LITERAL_BUG(__f) __STL_FIX_LITERAL_BUG(__l)
_M_range_initialize(__f, __l);
}
#endif
# ifdef __STL_USE_OWN_NAMESPACE
// these conversion operations still needed for
// strstream, etc.
basic_string (const __std_string& __x): _String_base<_CharT,_Alloc>(allocator_type())
{
const char* __s = __x.c_str();
_M_range_initialize(__s, __s + _Traits::length(__s));
}
operator __std_string() const { return __std_string(this->c_str()); }
# endif
~basic_string() { destroy(_M_start, _M_finish + 1); }
_Self& operator=(const _Self& __s) {
if (&__s != this)
assign(__s._M_start, __s._M_finish);
return *this;
}
_Self& operator=(const _CharT* __s) {
__STL_FIX_LITERAL_BUG(__s)
return assign(__s, __s + _Traits::length(__s));
}
_Self& operator=(_CharT __c)
{ return assign(__STATIC_CAST(size_type,1), __c); }
static _CharT _M_null() {
# ifndef __STL_DEFAULT_CONSTRUCTOR_BUG
return _CharT();
# else
return (_CharT) 0;
# endif
}
private: // Helper functions used by constructors
// and elsewhere.
// fbp : simplify integer types (char, wchar)
void _M_construct_null_aux(_CharT* __p, __false_type) {
construct(__p);
}
void _M_construct_null_aux(_CharT* __p, __true_type) {
*__p = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -