📄 mstl_optm_string.hpp
字号:
/*
The young Library
Copyright (c) 2005 by 杨桓
Permission to use, copy, modify, distribute and sell this software for any
purpose is hereby granted without fee, provided that the above copyright
notice appear in all copies and that both that copyright notice and this
permission notice appear in supporting documentation.
The author make no representations about the suitability of this software
for any purpose. It is provided "as is" without express or implied warranty.
*/
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_MINI_STL_OPTIMIZATION_STRING_HEADER_FILE__
#define __MACRO_CPLUSPLUS_MINI_STL_OPTIMIZATION_STRING_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "../mstl_allocator.hpp"
#include "../mstl_iterator.hpp"
#include "../mstl_char_traits.hpp"
#include "../mstl_exception.hpp"
#include "../algorithm/mstl_algorithm_base.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits = char_traits<CharT>,
typename Allocator = allocator<CharT> >
class optm_string
{
public:
typedef optm_string<CharT, Traits, Allocator> self;
typedef CharT char_type;
typedef Traits traits_type;
typedef Allocator allocator_type;
typedef char_type value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef def_size_t size_type;
typedef def_ptrdiff_t difference_type;
typedef value_type* iterator;
typedef const value_type* const_iterator;
typedef Reverse_Iterator<iterator> reverse_iterator;
typedef Reverse_Iterator<const_iterator> const_reverse_iterator;
static const size_type npos = size_t_max;
static const size_type unshareable = size_t_max;
protected:
struct ref_str
{
size_type ref;
char_type* data;
size_type capa, len;
allocator_type alloc;
size_type max_size() const
{ return (npos - 1) / sizeof(char_type); }
explicit ref_str( size_type n ):ref(1)
{ alloc_data( n ); }
ref_str( const ref_str& rhs ):ref(1)
{
alloc_data( rhs.len );
data_copy( 0, rhs.data, rhs.len );
}
~ref_str()
{
if( data )
alloc.deallocate( data, ++capa );
}
void data_copy( size_type pos, const char_type* str, size_type count )
{
if( count > 0 )
traits_type::copy( data + pos, str, count );
}
void data_move( size_type pos, const char_type* str, size_type count )
{
if( count > 0 )
traits_type::move( data + pos, str, count );
}
void data_set( size_type pos, const char_type c, size_type count )
{
if( count > 0 )
traits_type::assign( data + pos, count, c );
}
private:
ref_str& operator=(const ref_str&);
void alloc_data( size_type n );
size_type string_bytes( size_type n );
} *m_pstr; //end struct
public:
optm_string() : m_pstr( new ref_str(0) ) {}
explicit optm_string( size_type str_size ) : m_pstr( new ref_str(str_size) ) {}
optm_string( int str_size, char_type c ) : m_pstr( new ref_str(str_size) )
{ assign( static_cast<size_type>(str_size), c ); }
optm_string( long str_size, char_type c ) : m_pstr( new ref_str(str_size) )
{ assign( static_cast<size_type>(str_size), c ); }
optm_string( size_type str_size, char_type c ) : m_pstr( new ref_str(str_size) )
{ assign( str_size, c ); }
optm_string( const char_type* str )
{
size_type str_size = traits_type::length( str );
m_pstr = new ref_str( str_size );
assign( str, str_size );
}
optm_string( const char_type* str, size_type str_size )
: m_pstr( new ref_str(str_size) )
{
assign( str, str_size );
}
optm_string( const self& rhs, size_type pos = 0, size_type count = npos );
template< typename InputIterator >
optm_string( InputIterator first, InputIterator last )
: m_pstr( new ref_str(0) )
{
assign( first, last );
}
~optm_string() { release(); }
const char_type* data() const { return m_pstr->data; }
self& operator=( const self& rhs );
self& operator=( char_type c ) { return assign( 1, c ); }
self& operator=( const char_type* str ) { return assign( str ); }
self& operator+=( const self& rhs ) { return append( rhs ); }
self& operator+=( char_type c ) { return append( 1, c ); }
self& operator+=( const char_type* str ) { return append( str ); }
void push_back( const char_type& c ) { append( 1, c ); }
void pop_back() { erase( m_pstr->len - 1, 1 ); }
size_type size() const { return m_pstr->len; }
size_type length() const { return m_pstr->len; }
size_type space() const { return ( m_pstr->capa - m_pstr->len ); }
size_type capacity() const { return m_pstr->capa; }
size_type max_size() const { return m_pstr->max_size(); }
bool empty() const { return size() == 0; }
iterator begin() { return m_pstr->data; }
iterator end() { return ( m_pstr->data + m_pstr->len ); }
const_iterator begin() const { return m_pstr->data; }
const_iterator end() const { return ( m_pstr->data + m_pstr->len ); }
reverse_iterator rbegin() { return end(); }
reverse_iterator rend() { return begin(); }
const_reverse_iterator rbegin() const { return end(); }
const_reverse_iterator rend() const { return begin(); }
self substr( size_type pos = 0, size_type count = npos ) const
{ return self( *this, pos, count ); }
const char_type* c_str() const
{
if( size() > 0 )
traits_type::assign( m_pstr->data[m_pstr->len], traits_type::eos() );
return data();
}
char_type& operator[]( size_type index )
{
copy( false );
return *( begin() + index );
}
const char_type& operator[]( size_type index ) const
{
return data()[index];
}
char_type& at( size_type index )
{
if( index >= size() )
throw_out_of_range( "optm_string::at()" );
copy( false );
return *( begin() + index );
}
const char_type& at( size_type index ) const
{
if( index >= size() )
throw_out_of_range( "optm_string::at()" );
return data()[index];
}
void clear()
{
release();
m_pstr = NULL_POINTER;
}
void reserve( size_type new_size )
{
if( capacity() < new_size )
{
self temp( new_size );
temp.insert( 0, m_pstr->data, size() );
swap( temp );
}
}
void resize( size_type new_size )
{ resize( new_size, traits_type::eos() ); }
void resize( size_type new_size, char_type c );
void swap( self& rhs )
{
if( m_pstr != rhs.m_pstr )
data_swap( m_pstr, rhs.m_pstr );
}
size_type copy( char_type* str, size_type count = npos,
size_type pos = 0 ) const
{
if( pos > m_pstr->len )
throw_out_of_range( "optm_string::copy()" );
if( count > m_pstr->len - pos )
count = m_pstr->len - pos;
traits_type::copy( str, data() + pos, count );
return count;
}
//replace:1
self& replace( size_type pos, size_type before_count,
const self& str, size_type str_pos,
size_type after_count );
//replace:2
self& replace( size_type pos, size_type before_count,
const char_type* str, size_type after_count );
//replace:3
self& replace( size_type pos, size_type before_count,
size_type after_count, char_type c );
//replace:4
template< typename InputIterator >
self& replace( iterator first_pos, iterator last_pos,
InputIterator first, InputIterator last );
//replace:5
self& replace( size_type pos, size_type count, const char_type* str )
{ return replace( pos, count, str, traits_type::length(str) ); }
//replace:6
self& replace( size_type pos, size_type count, char_type c )
{ return replace( pos, count, size_type(1), c ); }
//replace:7
self& replace( iterator first, iterator last, const self& str )
{ return replace( size_type(first - begin()),
size_type(last - first), str, size_type(0), npos ); }
//replace:8
self& replace( iterator first, iterator last,
const char_type* str, size_type count )
{ return replace( size_type(first - begin()),
size_type(last - first), str, count ); }
//replace:9
self& replace( iterator first, iterator last, const char_type* str )
{ return replace( size_type(first - begin()), size_type(last - first),
str, traits_type::length(str) ); }
//replace:10
self& replace( iterator first, iterator last,
size_type count, char_type c )
{ return replace( size_type(first - begin()),
size_type(last - first), count, c ); }
self& assign( const self& str, size_type pos = 0, size_type count = npos )
{ return replace( size_type(0), npos, str, pos, count ); }
self& assign( const char_type* str, size_type count )
{ return replace( size_type(0), npos, str, count ); }
self& assign( const char_type* str )
{ return assign( str, traits_type::length(str) ); }
self& assign( size_type count, char_type c )
{ return replace( size_type(0), npos, count, c ); }
template< typename InputIterator >
self& assign( InputIterator first, InputIterator last )
{ return replace( begin(), end(), first, last ); }
self& append( const self& str, size_type pos = 0, size_type count = npos )
{ return replace( size(), size_type(0), str, pos, count ); }
self& append( const char_type* str, size_type count )
{ return replace( size(), size_type(0), str, count ); }
self& append( const char_type* str )
{ return append( str, traits_type::length(str) ); }
self& append( size_type count, char_type c )
{ return replace( size(), size_type(0), count, c ); }
template< typename InputIterator >
self& append( InputIterator first, InputIterator last )
{ return replace( end(), end(), first, last ); }
self& insert( size_type pos, const self& str,
size_type str_pos = 0, size_type count = npos )
{ return replace( pos, size_type(0), str, str_pos, count ); }
self& insert( size_type pos, const char_type* str, size_type count )
{ return replace( pos, size_type(0), str, count ); }
self& insert( size_type pos, const char_type* str )
{ return insert( pos, str, traits_type::length(str) ); }
self& insert( size_type pos, size_type count, char_type c )
{ return replace( pos, size_type(0), count, c ); }
iterator insert( iterator pos, char_type c )
{
size_type position = pos - begin();
insert( position, 1, c );
return begin() + position;
}
void insert( iterator pos, size_type count, char_type c )
{
size_type position = pos - begin();
insert( position, count, c );
}
template< typename InputIterator >
void insert( iterator pos, InputIterator first, InputIterator last )
{ replace( pos, pos, first, last ); }
iterator erase( iterator pos )
{
size_type n = pos - begin();
replace( pos, pos + 1, size_type(0), char_type() );
return begin() + n;
}
iterator erase( iterator first, iterator last )
{
size_type pos = first - begin();
size_type n = last - first;
replace( pos, n, size_type(0), char_type() );
return begin() + pos;
}
self& erase( size_type pos = 0, size_type count = npos )
{
return replace( pos, count, size_type(0), char_type() );
}
// (1) < 0 :this < str; (2) = 0 : this = str; (3) > 0 : this > str
int compare( const self& str ) const;
int compare( const char_type* str ) const;
int compare( size_type pos, size_type count, const self& str,
size_type str_pos, size_type str_count = npos ) const;
int compare( size_type pos, size_type count, const char_type* str,
size_type str_count = npos ) const;
size_type find( const char_type* str, size_type pos, size_type count ) const;
size_type find( char_type c, size_type pos = 0 ) const
{ return find_char( data(), c, pos, size() ); }
size_type find( const char_type* str, size_type pos = 0 ) const
{ return find( str, pos, traits_type::length(str) ); }
size_type find( const self& str, size_type pos = 0 ) const
{ return find( str.data(), pos, str.size() ); }
size_type rfind( const char_type* str, size_type pos, size_type count ) const;
size_type rfind( char_type c, size_type pos = npos ) const;
size_type rfind( const char_type* str, size_type pos = npos ) const
{ return rfind( str, pos, traits_type::length(str) ); }
size_type rfind( const self& str, size_type pos = npos) const
{ return rfind( str.data(), pos, str.size() ); }
size_type find_first_of( const char_type* str,
size_type pos, size_type count ) const;
size_type find_first_of( const self& str, size_type pos = 0 ) const
{ return find_first_of( str.data(), pos, str.size() ); }
size_type find_first_of( const char_type* str, size_type pos = 0 ) const
{ return find_first_of( str, pos, traits_type::length(str) ); }
size_type find_first_of( char_type c, size_type pos = 0 ) const
{ return find(c, pos); }
size_type find_last_of( const char_type* str,
size_type pos, size_type count ) const;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -