_strdef.mh

来自「开放源码的编译器open watcom 1.6.0版的源代码」· MH 代码 · 共 532 行 · 第 1/2 页

MH
532
字号
      const_reverse_iterator rbegin( ) const
          { return( const_reverse_iterator( buffer + str_length ) ); }
      reverse_iterator rend( )
          { return( reverse_iterator( buffer ) ); }
      const_reverse_iterator rend( ) const
          { return( const_reverse_iterator( buffer ) ); }

      size_type size( )     const  { return( str_length ); }
      size_type length( )   const  { return( str_length ); }
      size_type capacity( ) const  { return( buf_length - 1 ); }
      size_type max_size( ) const  { return( npos - 1 ); } // ?

      void      resize( size_type n, CharT c );
      void      resize( size_type n ) { resize( n, CharT( ) ); }
      void      reserve( size_type new_capacity );

      void      clear( )       { str_length = 0;            }
      bool      empty( ) const { return( str_length == 0 ); }

      const_reference operator[]( size_type pos ) const
          { return( buffer[pos] ); }
      reference operator[]( size_type pos )
          { return( buffer[pos] ); }

      const_reference at( size_type pos ) const;
      reference       at( size_type pos );

      basic_string &operator+=( const basic_string &str );
      basic_string &operator+=( const CharT *s );
      basic_string &operator+=( CharT c );

      basic_string &append( const basic_string &str, size_type pos = 0, size_type n = npos );
      basic_string &append( const CharT *s, size_type n );
      basic_string &append( const CharT *s );
      basic_string &append( size_type n, CharT c );

      void          push_back( CharT c );

      basic_string &insert( size_type pos1, const basic_string &str, size_type pos2 = 0, size_type n = npos );
      basic_string &insert( size_type pos, const CharT *s, size_type n );
      basic_string &insert( size_type pos, const CharT *s );
      basic_string &insert( size_type pos, size_type n, CharT c );
      iterator      insert( iterator p, CharT c );
      void          insert( iterator p, size_type n, CharT c );

      basic_string &erase( size_type pos = 0, size_type n = npos );
      iterator      erase( iterator p );
      iterator      erase( iterator first, iterator last );

      basic_string &replace( size_type pos1, size_type n, const basic_string &str );
      basic_string &replace( size_type pos1, size_type n1, const basic_string &str, size_type pos2, size_type n2 );
      basic_string &replace( size_type pos, size_type n1, const CharT *s, size_type n2 );
      basic_string &replace( size_type pos, size_type n1, const CharT *s );
      basic_string &replace( size_type pos, size_type n1, size_type n2, CharT c );
      basic_string &replace( iterator i1, iterator i2, const basic_string &str );
      basic_string &replace( iterator i1, iterator i2, const CharT *s, size_type n );
      basic_string &replace( iterator i1, iterator i2, const CharT *s );
      basic_string &replace( iterator i1, iterator i2, size_type n, CharT c );

      size_type     copy( CharT *s, size_type n, size_type pos = 0 ) const;
      void          swap( basic_string &str );

      const CharT   *c_str( )         const;
      const CharT   *data( )          const  { return( buffer ); }
      allocator_type get_allocator( ) const  { return( mem);     }

      size_type find( const basic_string &str, size_type pos = 0 ) const;
      size_type find( const CharT *s, size_type pos, size_type n ) const;
      size_type find( const CharT *s, size_type pos = 0 ) const;
      size_type find( CharT c, size_type pos = 0 ) const;

      size_type rfind( const basic_string &str, size_type pos = npos ) const;
      size_type rfind( const CharT *s, size_type pos, size_type n ) const;
      size_type rfind( const CharT *s, size_type pos = npos ) const;
      size_type rfind( CharT c, size_type pos = npos ) const;

      size_type find_first_of( const basic_string &str, size_type pos = 0 ) const;
      size_type find_first_of( const CharT *s, size_type pos, size_type n ) const;
      size_type find_first_of( const CharT *s, size_type pos = 0 ) const;
      size_type find_first_of( CharT c, size_type pos = 0 ) const;

      size_type find_last_of( const basic_string &str, size_type pos = npos ) const;
      size_type find_last_of( const CharT *s, size_type pos, size_type n ) const;
      size_type find_last_of( const CharT *s, size_type pos = npos ) const;
      size_type find_last_of( CharT c, size_type pos = npos ) const;

      size_type find_first_not_of( const basic_string &str, size_type pos = 0 ) const;
      size_type find_first_not_of( const CharT *s, size_type pos, size_type n ) const;
      size_type find_first_not_of( const CharT *s, size_type pos = 0 ) const;
      size_type find_first_not_of( CharT c, size_type pos = 0 ) const;

      size_type find_last_not_of( const basic_string &str, size_type pos = npos ) const;
      size_type find_last_not_of( const CharT *s, size_type pos, size_type n ) const;
      size_type find_last_not_of( const CharT *s, size_type pos = npos ) const;
      size_type find_last_not_of( CharT c, size_type pos = npos ) const;

      basic_string substr( size_type pos = 0, size_type n = npos ) const;

      bool _Sane( ) const;  // Check invariants (approximately).

    private:
      // Invariants:
      // 1. buffer has size buf_length.
      // 2. buf_length is a power of two.
      // 3. buf_length > str_length. Extra space for an O(1) c_str().
      // 4. buffer never shrinks.
      // 5. buffer allocated with mem or a copy of mem.
      // 6. Every string has its own buffer (no reference counting).
      //
      Allocator mem;         // Object used to get and release memory.
      pointer   buffer;      // Points at start of string.
      size_type str_length;  // Number of buffer slots with characters.
      size_type buf_length;  // Total number of buffer slots.

      // This method encapsulates the memory allocation policy.
      pointer alloc( size_type required, size_type &found );

      // This method replaces current text with other text.
      void replace_buffer( const_pointer other_buf, size_type other_length );
  };


  // ======================================
  // Specific typedefs for char and wchar_t
  // ======================================
  typedef basic_string< char > string;
  typedef basic_string< wchar_t > wstring;


  // ================================================================
  // Members of basic_string that are needed by the exception classes
  // ================================================================

  template< class CharT, class Traits, class Allocator >
  typename basic_string< CharT, Traits, Allocator >::pointer
    basic_string< CharT, Traits, Allocator >::alloc(
      size_type required,
      size_type &found )
  {
    pointer   result;
    size_type length = 8;

    // Find a power of two that produces a sufficient size.
    while( length < required ) length <<= 1;
    result = mem.allocate( length );

    // Update outputs only if allocation successful.
    found = length;
    return( result );
  }


  template< class CharT, class Traits, class Allocator >
  void basic_string< CharT, Traits, Allocator >::replace_buffer(
    const_pointer other_buffer,
    size_type other_length )
  {
    size_type new_length;
    pointer new_buffer = alloc( other_length + 1, new_length );
    Traits::copy( new_buffer, other_buffer, other_length );

    // Commit.
    mem.deallocate( buffer, buf_length );
    buffer = new_buffer;
    buf_length = new_length;
    str_length = other_length;
  }


  template< class CharT, class Traits, class Allocator >
  bool basic_string< CharT, Traits, Allocator >::_Sane( ) const
  {
    if( buf_length == 0 ) return( false );
    if( buf_length <= str_length ) return( false );

    // Is buf_length a power of 2?
    size_type temp = buf_length;
    while( temp != 1 ) {
      if( temp & 0x1 ) return( false );
      temp >>= 1; 
    }
    return( true );
  }


  // basic_string( const Allocator & )
  // *********************************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator >::basic_string(
    const Allocator &a ) : mem( a )
  {
    buffer = alloc(1, buf_length);
    str_length = 0;
  }


  // basic_string( const basic_string &, const Allocator & )
  // ***************************************************************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator >::basic_string(
    const basic_string &other ) : mem( other.mem )
  {
    buffer = alloc( other.str_length + 1, buf_length );
    Traits::copy( buffer, other.buffer, other.str_length );
    str_length = other.str_length;
  }


  // basic_string( const CharT *, const Allocator & )
  // ************************************************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator >::basic_string(
    const CharT     *other,
    const Allocator &a ) : mem( a )
  {
    size_type other_length = Traits::length( other );
    buffer = alloc( other_length + 1, buf_length );
    Traits::copy( buffer, other, other_length );
    str_length = other_length;
  }


  // ~basic_string( )
  // ****************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator >::~basic_string( )
  {
    // No need to destroy characters. CharT must be POD type.
    mem.deallocate( buffer, buf_length );
  }


  // operator=( const basic_string & )
  // *********************************
  template< class CharT, class Traits, class Allocator >
  basic_string< CharT, Traits, Allocator > &
    basic_string< CharT, Traits, Allocator >::operator=(
      const basic_string &other )
  {
    if( this == &other ) return *this;

    if( buf_length > other.str_length ) {
      Traits::copy( buffer, other.buffer, other.str_length );
      str_length = other.str_length;
    }
    else {
      replace_buffer( other.buffer, other.str_length );
    }
    return( *this );
  }


  // c_str( ) const
  // **************
  template< class CharT, class Traits, class Allocator >
  inline
  const CharT *basic_string< CharT, Traits, Allocator >::c_str( ) const
  {
    buffer[str_length] = CharT();
    return( buffer );
  }

} // namespace std

#endif

⌨️ 快捷键说明

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