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

📄 mstl_basic_string.hpp

📁 一个类STL的多平台可移植的算法容器库,主要用于嵌入式系统编程时的内存管理等方面
💻 HPP
📖 第 1 页 / 共 3 页
字号:
/*
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_BASIC_STRING_HEADER_FILE__
#define __MACRO_CPLUSPLUS_MINI_STL_BASIC_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 basic_string
{
public:
    typedef  basic_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;

protected:
    char_type*      m_data;
    size_type       m_capacity, m_length;
    allocator_type  m_alloc;

    void init()
    {
        m_data = NULL_POINTER;
        m_capacity = 0;
        m_length = 0;
    }

public:
    basic_string() : m_data(NULL_POINTER), m_capacity(0), m_length(0)  {}

    explicit basic_string( size_type str_size )
    {
        alloc_data( str_size );
    }

    basic_string( const char_type* str )
    {
        size_type str_size = traits_type::length( str );
        alloc_data( str_size );
        assign( str, str_size );
    }

    basic_string( const char_type* str, size_type str_size )
    {
        alloc_data( str_size );
        assign( str, str_size );
    }

    basic_string( size_type str_size, char_type c )
    {
        alloc_data( str_size );
        assign( str_size, c );
    }
    basic_string( int str_size, char_type c )
    {
        alloc_data( static_cast<size_type>(str_size) );
        assign( static_cast<size_type>(str_size), c );
    }
    basic_string( long str_size, char_type c )
    {
        alloc_data( static_cast<size_type>(str_size) );
        assign( static_cast<size_type>(str_size), c );
    }

    template< typename InputIterator >
    basic_string( InputIterator first, InputIterator last )
    : m_data(NULL_POINTER), m_capacity(0), m_length(0)
    {
        assign( first, last );
    }

    basic_string( const self& rhs, size_type pos = 0, size_type count = npos );

    ~basic_string()  {  dealloc_data();  }

    const char_type* data() const  {  return m_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_length - 1, 1 );  }

    size_type size() const      {  return m_length;  }
    size_type capacity() const  {  return m_capacity;  }
    size_type length() const    {  return m_length;  }
    size_type space() const     {  return ( m_capacity - m_length );  }
    size_type max_size() const  {  return (npos - 1) / sizeof(char_type);  }
    bool empty() const          {  return m_length == 0;  }

    iterator begin()              {  return m_data;  }
    iterator end()                {  return ( m_data + m_length );  }
    const_iterator begin() const  {  return m_data;  }
    const_iterator end() const    {  return ( m_data + m_length );  }

    reverse_iterator rbegin()              {  return end();  }
    reverse_iterator rend()                {  return begin();  }
    const_reverse_iterator rbegin() const  {  return end();  }
    const_reverse_iterator rend() const    {  return begin();  }

    reference front()              {  return *begin();  }
    reference back()               {  return *(end() - 1);  }
    const_reference front() const  {  return *begin();  }
    const_reference back() const   {  return *(end() - 1);  }

    void clear()  {  dealloc_data();  init();  }

    void resize( size_type new_size )
        {  resize( new_size, traits_type::eos() );  }
    void resize( size_type new_size, char_type c )
    {
        if( new_size > max_size() )
            throw_length_error( "basic_string::resize()" );

        if( new_size > m_length )
            append( new_size - m_length, c );
        else if( new_size < m_length )
            erase( new_size, m_length - new_size );
    }

    void reserve( size_type new_size )
    {
        if( capacity() < new_size )
        {
            self temp( new_size );
            temp.insert( 0, m_data, m_length );
            swap( temp );
        }
    }

    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_data[m_length], traits_type::eos() );
        return data();
    }

    char_type& operator[]( size_type index )
        {  return m_data[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( "basic_string::at()" );
        return m_data[index];
    }
    const char_type& at( size_type index ) const
    {
        if( index >= size() )
            throw_out_of_range( "basic_string::at()" );
        return data()[index];
    }

    void swap( self& rhs )
    {
        if( this != &rhs )
        {
            data_swap( m_data, rhs.m_data );
            data_swap( m_capacity, rhs.m_capacity );
            data_swap( m_length, rhs.m_length );
            data_swap( m_alloc, rhs.m_alloc );
        }
    }

    size_type copy( char_type* str, size_type count = npos,
                    size_type pos = 0 ) const
    {
        if( pos > m_length )
            throw_out_of_range( "basic_string::copy()" );

        if( count > m_length - pos )
            count = m_length - 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 );  }  //replace:1
    self& assign( const char_type* str, size_type count )
        {  return replace( size_type(0), npos, str, count );  }  //replace:2
    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 );  }  //replace:3
    template< typename InputIterator >
    self& assign( InputIterator first, InputIterator last )  //replace:4
        {  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 );  }  //replace:1
    self& append( const char_type* str, size_type count )
        {  return replace( size(), size_type(0), str, count );  }  //replace:2
    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 );  }  //replace:3
    template< typename InputIterator >
    self& append( InputIterator first, InputIterator last )
        {  return replace( end(), end(), first, last );  }  //replace:4


    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 );  }  //replace:1
    self& insert( size_type pos, const char_type* str, size_type count )
        {  return replace( pos, size_type(0), str, count );  }  //replace:2
    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 );  }  //replace:3
    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 );  }  //replace:4


    iterator erase( iterator pos )
    {
        size_type n = pos - begin();
        replace( pos, pos + 1, size_type(0), char_type() );  //replace:10
        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() );  //replace:3
        return begin() + pos;
    }
    self& erase( size_type pos = 0, size_type count = npos )
    {
        return replace( pos, count, size_type(0), char_type() );  //replace:3
    }


    // (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;
    size_type find_last_of( const self& str, size_type pos = npos ) const
        {  return find_last_of( str.data(), pos, str.size() );  }

⌨️ 快捷键说明

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