分享
 
 
 

C++ young 程序库——y_optm_string.hpp 和 y_string.hpp

王朝c/c++·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

文件位置:young/string/y_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_YOUNG_LIBRARY_OPTIMIZATION_STRING_HEADER_FILE__

#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_OPTIMIZATION_STRING_HEADER_FILE__

//-----------------------------------------------------------------------------

#include "../y_allocator.hpp"

#include "../y_iterator.hpp"

#include "../y_char_traits.hpp"

#include "../y_exception.hpp"

#include "../algorithm/y_algorithm_base.hpp"

//-----------------------------------------------------------------------------

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_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 self&);

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 size ) : m_pstr( new ref_str(size) ) {}

optm_string( int size, char_type c ) : m_pstr( new ref_str(size) )

{ assign( static_cast<size_type>(size), c ); }

optm_string( long size, char_type c ) : m_pstr( new ref_str(size) )

{ assign( static_cast<size_type>(size), c ); }

optm_string( size_type size, char_type c ) : m_pstr( new ref_str(size) )

{ assign( size, c ); }

optm_string( const char_type* str )

{

size_type size = traits_type::length( str );

m_pstr = new ref_str( size );

assign( str, size );

}

optm_string( const char_type* str, size_type size )

: m_pstr( new ref_str(size) )

{

assign( 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_length - 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(); }

reference front() { return *begin(); }

reference back() { return *(--end()); }

const_reference front() const { return *begin(); }

const_reference back() const { return *(--end()); }

self substr( size_type pos = 0, size_type count = npos ) const

{

return self( *this, pos, count );

}

const char_type* c_str() const

{

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

{

if( index == size() )

return traits_type::eos();

else

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, pdata, 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 = 0,

size_type after_count = npos );

//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, 1, c ); }

//replace:7

self& replace( iterator first, iterator last, const self& str )

{ return replace( first - begin(), last - first, str ); }

//replace:8

self& replace( iterator first, iterator last,

const char_type* str, size_type count )

{ return replace( first - begin(), last - first, str, count ); }

//replace:9

self& replace( iterator first, iterator last, const char_type* str )

{ return replace( first - begin(), last - first, str ); }

//replace:10

self& replace( iterator first, iterator last,

size_type count, char_type c )

{ return replace( first - begin(), last - first, count, c ); }

self& assign( const self& str, size_type pos = 0, size_type count = npos )

{ return replace( 0, npos, str, pos, count ); }

self& assign( const char_type* str, size_type count )

{ return replace( 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( 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(), 0, str, pos, count ); }

self& append( const char_type* str, size_type count )

{ return replace( size(), 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(), 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, 0, str, str_pos, count ); }

self& insert( size_type pos, const char_type* str, size_type count )

{ return replace( pos, 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, 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, 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;

size_type find_last_of( const self& str, size_type pos = npos ) const

{ return find_last_of( str.data(), pos, str.size() ); }

size_type find_last_of( const char_type* str, size_type pos = npos ) const

{ return find_last_of( str, pos, traits_type::length(str) ); }

size_type find_last_of( char_type c, size_type pos = npos ) const

{ return rfind(c, pos); }

size_type find_first_not_of( const char_type* str,

size_type pos, size_type count ) const;

size_type find_first_not_of( char_type c, size_type pos = 0 ) const;

size_type find_first_not_of( const char_type* str, size_type pos = 0 ) const

{ return find_first_not_of( str, pos, traits_type::length(str) ); }

size_type find_first_not_of( const self& str, size_type pos = 0 ) const

{ return find_first_not_of( str.data(), pos, str.size() ); }

size_type find_last_not_of( const char_type* str,

size_type pos, size_type count ) const;

size_type find_last_not_of( char_type c, size_type pos = npos ) const;

size_type find_last_not_of( const char_type* str, size_type pos = npos ) const

{ return find_last_not_of( str, pos, traits_type::length(str) ); }

size_type find_last_not_of( const self& str, size_type pos = npos ) const

{ return find_last_not_of( str.data(), pos, str.size() ); }

protected:

void release()

{

if( --(m_pstr->ref) == 0 )

delete m_pstr;

}

void copy( bool share = true )

{

if( m_pstr->ref > 1 && m_pstr->ref != unshareable )

{

self temp( data(), size() );

swap( temp );

}

m_pstr->ref = share ? 1 : unshareable;

}

bool check_realloc( size_type new_len )

{

if( (m_pstr->ref > 1 && m_pstr->ref != unshareable)

|| capacity() < new_len )

return true;

else

return false;

}

size_type find_char( const char_type* str, const char_type& c,

size_type pos, size_type end ) const

{

if( pos < end )

{

const char_type* p = traits_type::find( str + pos, end - pos, c );

if( p )

return ( p - str );

}

return npos;

}

}; //end class

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline typename optm_string<CharT, Traits, Allocator>::size_type

optm_string<CharT, Traits, Allocator>::ref_str::string_bytes( size_type n )

{

size_type n_bytes = n * sizeof( char_type );

size_type i_bytes = string_alignment_bytes;

while( i_bytes < n_bytes )

i_bytes *= 2;

return i_bytes;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

void optm_string<CharT, Traits, Allocator>::ref_str::alloc_data( size_type n )

{

if( n < 1 )

{

data = NULL_POINTER;

capa = 0;

len = 0;

}

else

{

if( n > max_size() )

throw_length_error( "optm_string::alloc_data()" );

size_type bytes = string_bytes( n + 1 );

len = 0;

capa = bytes / sizeof( char_type );

data = alloc.allocate( capa );

--capa;

}

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

optm_string<CharT, Traits, Allocator>::optm_string( const self& rhs,

size_type pos,

size_type count )

{

if( rhs.m_pstr->ref != unshareable )

{

m_pstr = rhs.m_pstr;

++( m_pstr->ref );

}

else

{

if( pos > rhs.size() )

throw_length_error( "optm_string::optm_string" );

count = min( rhs.size() - pos, count );

m_pstr = new ref_str( count );

m_pstr->data_copy( 0, rhs.data() + pos, count );

m_pstr->len = count;

}

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

optm_string<CharT, Traits, Allocator>&

optm_string<CharT, Traits, Allocator>::operator=( const self& rhs )

{

if( m_pstr != rhs.m_pstr )

{

if( m_pstr->ref == unshareable || rhs.m_pstr->ref == unshareable )

{

if( capacity() < rhs.size() )

{

self temp( rhs );

swap( temp );

}

else

m_pstr->data_copy( 0, rhs.data(), rhs.size() );

}

else //均为可共享状态

{

if( m_pstr->ref > 1 || capacity() < rhs.size() )

{

release();

m_pstr = rhs.m_pstr;

++( m_pstr->ref );

}

else

m_pstr->data_copy( 0, rhs.data(), rhs.size() );

}

}

return *this;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

void optm_string<CharT, Traits, Allocator>::resize( size_type new_size,

char_type c )

{

if( new_size > max_size() )

throw_length_error( "optm_string::resize()" );

if( m_pstr->ref > 1 || capacity() < new_size )

{

self temp( new_size, c );

swap( temp );

}

else if( new_size > m_pstr->len )

append( new_size - m_pstr->len, c );

else if( new_size < m_pstr->len )

erase( new_size, m_pstr->len - new_size );

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline optm_string<CharT, Traits, Allocator>&

optm_string<CharT, Traits, Allocator>::replace( size_type pos,

size_type before_count,

const self& str,

size_type str_pos,

size_type after_count )

{

const size_type str_len = str.size();

if( pos == 0 && before_count >= size()

&& str_pos == 0 && after_count >= str_len )

return operator=( str );

if( str_pos > str_len )

throw_out_of_range( "optm_string::replace()" );

if( after_count > str_len - str_pos )

after_count = str_len - str_pos;

return replace( pos, before_count, str.data() + str_pos, after_count );

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

optm_string<CharT, Traits, Allocator>&

optm_string<CharT, Traits, Allocator>::replace( size_type pos,

size_type before_count,

const char_type* str,

size_type after_count )

{

const size_type old_len = size();

if( pos > old_len )

throw_out_of_range( "optm_string::replace()" );

if( before_count > old_len - pos )

before_count = old_len - pos;

if( old_len - before_count > max_size() - after_count )

throw_length_error( "optm_string::replace()" );

size_type new_len = old_len - before_count + after_count;

if( check_realloc(new_len) )

{

self temp( new_len );

temp.m_pstr->data_copy( 0, data(), pos );

temp.m_pstr->data_copy( pos + after_count, data() + pos + before_count,

old_len - (pos + before_count) );

temp.m_pstr->data_copy( pos, str, after_count );

swap( temp );

}

else

{

m_pstr->data_move( pos + after_count, data() + pos + before_count,

old_len - (pos + before_count) );

m_pstr->data_copy( pos, str, after_count );

}

m_pstr->len = new_len;

return *this;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

optm_string<CharT, Traits, Allocator>&

optm_string<CharT, Traits, Allocator>::replace( size_type pos,

size_type before_count,

size_type after_count,

char_type c )

{

const size_type old_len = size();

if( pos > old_len )

throw_out_of_range( "optm_string::replace()" );

if( before_count > old_len - pos )

before_count = old_len - pos;

if( old_len - before_count > max_size() - after_count )

throw_length_error( "optm_string::replace()" );

size_type new_len = old_len - before_count + after_count;

if( check_realloc(new_len) )

{

self temp( new_len );

temp.m_pstr->data_copy( 0, data(), pos );

temp.m_pstr->data_copy( pos + after_count, data() + pos + before_count,

old_len - (pos + before_count) );

temp.m_pstr->data_set( pos, c, after_count );

swap( temp );

}

else

{

m_pstr->data_move( pos + after_count, data() + pos + before_count,

old_len - (pos + before_count) );

m_pstr->data_set( pos, c, after_count );

}

m_pstr->len = new_len;

return *this;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

template< typename InputIterator >

optm_string<CharT, Traits, Allocator>&

optm_string<CharT, Traits, Allocator>::replace( iterator first_pos,

iterator last_pos,

InputIterator first,

InputIterator last )

{

if( first == last )

return *this;

const size_type old_len = size();

size_type pos = first_pos - begin();

if( pos > old_len )

throw_out_of_range( "optm_string::replace()" );

size_type before_count = last_pos - first_pos;

size_type after_count = distance( first, last );

if( before_count > old_len - pos )

before_count = old_len - pos;

if( old_len - before_count > max_size() - after_count )

throw_length_error( "optm_string::replace()" );

size_type new_len = old_len - before_count + after_count;

if( check_realloc(new_len) )

{

self temp( new_len );

temp.m_pstr->data_copy( 0, data(), pos );

temp.m_pstr->data_copy( pos + after_count, data() + pos + before_count,

old_len - (pos + before_count) );

for( ; after_count > 0; ++first,++pos,--after_count )

traits_type::assign( *(m_pstr->data + pos), *first );

swap( temp );

}

else

{

m_pstr->data_move( pos + after_count, data() + pos + before_count,

old_len - (pos + before_count) );

for( ; after_count > 0; ++first,++pos,--after_count )

traits_type::assign( *(m_pstr->data + pos), *first );

}

m_pstr->len = new_len;

return *this;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline

int optm_string<CharT, Traits, Allocator>::compare( const self& str ) const

{

int result = traits_type::compare( data(), str.data(),

min( size(), str.size() ) );

if( result == 0 )

result = size() - str.size();

return result;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline

int optm_string<CharT, Traits, Allocator>::compare( const char_type* str ) const

{

size_type str_len = traits_type::length( str );

int result = traits_type::compare( data(), str, min(size(), str_len) );

if( result == 0 )

result = size() - str_len;

return result;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

int optm_string<CharT, Traits, Allocator>::compare( size_type pos,

size_type count,

const self& str,

size_type str_pos,

size_type str_count ) const

{

if( pos > size() || str_pos > str.size() )

throw_out_of_range( "optm_string::compare()" );

size_type str_len = min( str.size() - str_pos, str_count );

size_type this_len = min( size() - pos, count );

int result = traits_type::compare( data() + pos, str.data() + str_pos,

min(str_len, this_len) );

if( result == 0 )

result = this_len - str_len;

return result;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

int optm_string<CharT, Traits, Allocator>::compare( size_type pos,

size_type count,

const char_type* str,

size_type str_count ) const

{

if( pos > size() )

throw_out_of_range( "optm_string::compare()" );

size_type str_len = min( traits_type::length(str), str_count );

size_type this_len = min( size() - pos, count );

int result = traits_type::compare( data() + pos, str,

min(str_len, this_len) );

if( result == 0 )

result = this_len - str_len;

return result;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

typename optm_string<CharT, Traits, Allocator>::size_type

optm_string<CharT, Traits, Allocator>::find( const char_type* str,

size_type pos,

size_type count ) const

{

for( ; pos + count <= size(); ++pos )

{

if( traits_type::eq( data()[pos], *str )

&& traits_type::compare( data() + pos, str, count ) == 0 )

return pos;

}

return npos;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

typename optm_string<CharT, Traits, Allocator>::size_type

optm_string<CharT, Traits, Allocator>::rfind( const char_type* str,

size_type pos,

size_type count ) const

{

if( count > size() )

return npos;

size_type end_pos = size() - 1;

if( end_pos > pos )

end_pos = pos;

for( ++end_pos; end_pos-- > 0; )

{

if( traits_type::eq( data()[end_pos], *str )

&& traits_type::compare( data() + end_pos, str, count ) == 0 )

return end_pos;

}

return npos;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

typename optm_string<CharT, Traits, Allocator>::size_type

optm_string<CharT, Traits, Allocator>::rfind( char_type c,

size_type pos ) const

{

if( size() < 1 )

return npos;

size_type end_pos = size() - 1;

if( end_pos > pos )

end_pos = pos;

for( ++end_pos; end_pos-- > 0; )

{

if( traits_type::eq( data()[end_pos], c ) )

return end_pos;

}

return npos;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

typename optm_string<CharT, Traits, Allocator>::size_type

optm_string<CharT, Traits, Allocator>::find_first_of( const char_type* str,

size_type pos,

size_type count ) const

{

for( ; pos < size(); ++pos )

{

if( find_char( str, data()[pos], 0, count ) != npos )

return pos;

}

return npos;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

typename optm_string<CharT, Traits, Allocator>::size_type

optm_string<CharT, Traits, Allocator>::find_last_of( const char_type* str,

size_type pos,

size_type count ) const

{

if( size() == 0 )

return npos;

size_type end_pos = size() - 1;

if( end_pos > pos )

end_pos = pos;

for( ++end_pos; end_pos-- > 0; )

{

if( find_char( str, data()[end_pos], 0, count ) != npos )

return end_pos;

}

return npos;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

typename optm_string<CharT, Traits, Allocator>::size_type

optm_string<CharT, Traits, Allocator>::find_first_not_of( const char_type* str,

size_type pos,

size_type count ) const

{

for( ; pos < size(); ++pos )

{

if( find_char( str, data()[pos], 0, count ) == npos )

return pos;

}

return npos;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

typename optm_string<CharT, Traits, Allocator>::size_type

optm_string<CharT, Traits, Allocator>::find_first_not_of( char_type c,

size_type pos ) const

{

for( ; pos < size(); ++pos )

{

if( !traits_type::eq( data()[pos], c ) )

return pos;

}

return npos;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

typename optm_string<CharT, Traits, Allocator>::size_type

optm_string<CharT, Traits, Allocator>::find_last_not_of( const char_type* str,

size_type pos,

size_type count ) const

{

if( size() == 0 )

return npos;

size_type end_pos = size() - 1;

if( end_pos > pos )

end_pos = pos;

for( ++end_pos; end_pos-- > 0; )

{

if( find_char( str, data()[end_pos], 0, count ) == npos )

return end_pos;

}

return npos;

}

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

typename optm_string<CharT, Traits, Allocator>::size_type

optm_string<CharT, Traits, Allocator>::find_last_not_of( char_type c,

size_type pos ) const

{

if( size() < 1 )

return npos;

size_type end_pos = size() - 1;

if( end_pos > pos )

end_pos = pos;

for( ++end_pos; end_pos-- > 0; )

{

if( !traits_type::eq( data()[end_pos], c ) )

return end_pos;

}

return npos;

}

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline void swap( optm_string<CharT, Traits, Allocator>& lhs,

optm_string<CharT, Traits, Allocator>& rhs )

{

lhs.swap( rhs );

}

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

//以下为重载的运算符

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline optm_string<CharT, Traits, Allocator>

operator+( const optm_string<CharT, Traits, Allocator>& lhs,

const optm_string<CharT, Traits, Allocator>& rhs )

{

optm_string<CharT, Traits, Allocator> str( lhs );

str.append( rhs );

return str;

}

template< typename CharT, typename Traits, typename Allocator >

inline optm_string<CharT, Traits, Allocator>

operator+( const char* lhs, const optm_string<CharT, Traits, Allocator>& rhs )

{

optm_string<CharT, Traits, Allocator> str( lhs );

str.append( rhs );

return str;

}

template< typename CharT, typename Traits, typename Allocator >

inline optm_string<CharT, Traits, Allocator>

operator+( const optm_string<CharT, Traits, Allocator>& lhs, const char* rhs )

{

optm_string<CharT, Traits, Allocator> str( lhs );

str.append( rhs );

return str;

}

template< typename CharT, typename Traits, typename Allocator >

inline optm_string<CharT, Traits, Allocator>

operator+( const optm_string<CharT, Traits, Allocator>& lhs, CharT c )

{

optm_string<CharT, Traits, Allocator> str( lhs );

str.append( 1, c );

return str;

}

template< typename CharT, typename Traits, typename Allocator >

inline optm_string<CharT, Traits, Allocator>

operator+( CharT c, const optm_string<CharT, Traits, Allocator>& rhs )

{

optm_string<CharT, Traits, Allocator> str( rhs );

str.append( 1, c );

return str;

}

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline bool operator==( const optm_string<CharT, Traits, Allocator>& lhs,

const optm_string<CharT, Traits, Allocator>& rhs )

{

return ( lhs.compare(rhs) == 0 );

}

template< typename CharT, typename Traits, typename Allocator >

inline bool operator==( const optm_string<CharT, Traits, Allocator>& lhs,

const CharT* rhs )

{

return ( lhs.compare(rhs) == 0 );

}

template< typename CharT, typename Traits, typename Allocator >

inline bool operator==( const CharT* lhs,

const optm_string<CharT, Traits, Allocator>& rhs )

{

return ( rhs.compare(lhs) == 0 );

}

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline bool operator!=( const optm_string<CharT, Traits, Allocator>& lhs,

const optm_string<CharT, Traits, Allocator>& rhs )

{

return ( lhs.compare(rhs) != 0 );

}

template< typename CharT, typename Traits, typename Allocator >

inline bool operator!=( const optm_string<CharT, Traits, Allocator>& lhs,

const CharT* rhs )

{

return ( lhs.compare(rhs) != 0 );

}

template< typename CharT, typename Traits, typename Allocator >

inline bool operator!=( const CharT* lhs,

const optm_string<CharT, Traits, Allocator>& rhs )

{

return ( rhs.compare(lhs) != 0 );

}

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline bool operator<( const optm_string<CharT, Traits, Allocator>& lhs,

const optm_string<CharT, Traits, Allocator>& rhs )

{

return ( lhs.compare(rhs) < 0 );

}

template< typename CharT, typename Traits, typename Allocator >

inline bool operator<( const optm_string<CharT, Traits, Allocator>& lhs,

const CharT* rhs )

{

return ( lhs.compare(rhs) < 0 );

}

template< typename CharT, typename Traits, typename Allocator >

inline bool operator<( const CharT* lhs,

const optm_string<CharT, Traits, Allocator>& rhs )

{

return ( rhs.compare(lhs) > 0 );

}

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline bool operator<=( const optm_string<CharT, Traits, Allocator>& lhs,

const optm_string<CharT, Traits, Allocator>& rhs )

{

return ( lhs.compare(rhs) <= 0 );

}

template< typename CharT, typename Traits, typename Allocator >

inline bool operator<=( const optm_string<CharT, Traits, Allocator>& lhs,

const CharT* rhs )

{

return ( lhs.compare(rhs) <= 0 );

}

template< typename CharT, typename Traits, typename Allocator >

inline bool operator<=( const CharT* lhs,

const optm_string<CharT, Traits, Allocator>& rhs )

{

return ( rhs.compare(lhs) >= 0 );

}

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline bool operator>( const optm_string<CharT, Traits, Allocator>& lhs,

const optm_string<CharT, Traits, Allocator>& rhs )

{

return ( lhs.compare(rhs) > 0 );

}

template< typename CharT, typename Traits, typename Allocator >

inline bool operator>( const optm_string<CharT, Traits, Allocator>& lhs,

const CharT* rhs )

{

return ( lhs.compare(rhs) > 0 );

}

template< typename CharT, typename Traits, typename Allocator >

inline bool operator>( const CharT* lhs,

const optm_string<CharT, Traits, Allocator>& rhs )

{

return ( rhs.compare(lhs) < 0 );

}

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline bool operator>=( const optm_string<CharT, Traits, Allocator>& lhs,

const optm_string<CharT, Traits, Allocator>& rhs )

{

return ( lhs.compare(rhs) >= 0 );

}

template< typename CharT, typename Traits, typename Allocator >

inline bool operator>=( const optm_string<CharT, Traits, Allocator>& lhs,

const CharT* rhs )

{

return ( lhs.compare(rhs) >= 0 );

}

template< typename CharT, typename Traits, typename Allocator >

inline bool operator>=( const CharT* lhs,

const optm_string<CharT, Traits, Allocator>& rhs )

{

return ( rhs.compare(lhs) <= 0 );

}

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__

#endif

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

文件位置:young/y_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_YOUNG_LIBRARY_STRING_HEADER_FILE__

#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_STRING_HEADER_FILE__

//-----------------------------------------------------------------------------

#include <iostream>

#include "string/y_basic_string.hpp"

#include "string/y_optm_string.hpp"

//-----------------------------------------------------------------------------

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

typedef basic_string<char> string;

typedef basic_string<wchar_t> wstring;

typedef basic_string<char, lower_char_traits> lstring;

typedef basic_string<char, upper_char_traits> ustring;

typedef basic_string<char, cmp_no_case_char_traits> ncstring;

typedef basic_string<char, cmp_no_case_lower_char_traits> nclstring;

typedef basic_string<char, cmp_no_case_upper_char_traits> ncustring;

typedef basic_string<wchar_t, lower_wchar_traits> lwstring;

typedef basic_string<wchar_t, upper_wchar_traits> uwstring;

typedef basic_string<wchar_t, cmp_no_case_wchar_traits> ncwstring;

typedef basic_string<wchar_t, cmp_no_case_lower_wchar_traits> nclwstring;

typedef basic_string<wchar_t, cmp_no_case_upper_wchar_traits> ncuwstring;

//-----------------------------------------------------------------------------

typedef optm_string<char> o_string;

typedef optm_string<wchar_t> o_wstring;

typedef optm_string<char, lower_char_traits> o_lstring;

typedef optm_string<char, upper_char_traits> o_ustring;

typedef optm_string<char, cmp_no_case_char_traits> o_ncstring;

typedef optm_string<char, cmp_no_case_lower_char_traits> o_nclstring;

typedef optm_string<char, cmp_no_case_upper_char_traits> o_ncustring;

typedef optm_string<wchar_t, lower_wchar_traits> o_lwstring;

typedef optm_string<wchar_t, upper_wchar_traits> o_uwstring;

typedef optm_string<wchar_t, cmp_no_case_wchar_traits> o_ncwstring;

typedef optm_string<wchar_t, cmp_no_case_lower_wchar_traits> o_nclwstring;

typedef optm_string<wchar_t, cmp_no_case_upper_wchar_traits> o_ncuwstring;

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline std::basic_ostream<CharT>&

operator<<( std::basic_ostream<CharT>& os,

const basic_string<CharT, Traits, Allocator>& str )

{

return os.write( str.data(), str.size() );

}

template< typename CharT, typename Traits, typename Allocator >

std::basic_istream<CharT>&

operator>>( std::basic_istream<CharT>& is,

basic_string<CharT, Traits, Allocator>& str )

{

typedef std::ctype<CharT> ctype_t;

typedef std::basic_istream<CharT> istream_type;

typedef std::basic_streambuf<CharT> streambuf_type;

typedef basic_string<CharT, Traits, Allocator> string_type;

typedef typename istream_type::int_type int_type;

typedef typename string_type::size_type size_type;

size_type extracted = 0;

typename istream_type::sentry cerb( is, false );

if( cerb )

{

str.erase();

std::streamsize w = is.width(); //取得输入流的域宽

size_type n = w > 0 ? (size_type)w : str.max_size();

const ctype_t& ct = std::use_facet<ctype_t>( is.getloc() ); //取得现场

const int_type eof = Traits::eof(); //字符串结束符

streambuf_type* buf = is.rdbuf();

int_type c = buf->sgetc(); //取当前字符

while( extracted < n

&& !Traits::eq_int_type(c, eof)

&& !ct.is(std::ctype_base::space, c) )

{

str += Traits::to_char_type( c );

++extracted;

c = buf->snextc(); //跳过当前字符,取下一个字符

}

if( Traits::eq_int_type(c, eof) )

is.setstate( std::ios_base::eofbit ); //设置IO状态,读取结束

is.width( 0 );

}

if( extracted == 0 )

is.setstate( std::ios_base::failbit ); //设置IO状态,读取操作失败

return is;

}

template< typename CharT, typename Traits, typename Allocator >

std::basic_istream<CharT>&

getline( std::basic_istream<CharT>& is,

basic_string<CharT, Traits, Allocator>& str,

CharT delim = '\n' )

{

typedef std::ctype<CharT> ctype_t;

typedef std::basic_istream<CharT> istream_type;

typedef std::basic_streambuf<CharT> streambuf_type;

typedef basic_string<CharT, Traits, Allocator> string_type;

typedef typename istream_type::int_type int_type;

typedef typename string_type::size_type size_type;

size_type extracted = 0;

bool testdelim = false;

typename istream_type::sentry cerb( is, true );

if( cerb )

{

str.erase(); //将字符串清空

const int_type eof = Traits::eof(); //字符串结束符

size_type n = str.max_size();

streambuf_type* buf = is.rdbuf();

int_type c = buf->sbumpc(); //将buf->gptr()推进1,即已填入的下一个字符

int_type idelim = Traits::to_int_type( delim );

testdelim = Traits::eq_int_type( c, idelim );

while( extracted <= n

&& !Traits::eq_int_type(c, eof) && !testdelim )

{

str += Traits::to_char_type( c );

++extracted;

c = buf->sbumpc();

testdelim = Traits::eq_int_type( c, idelim ); //下一个字符是否是结束符

}

if( Traits::eq_int_type(c, eof) )

is.setstate( std::ios_base::eofbit );

is.width( 0 );

}

if( extracted == 0 && !testdelim )

is.setstate( std::ios_base::failbit );

return is;

}

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

template< typename CharT, typename Traits, typename Allocator >

inline std::basic_ostream<CharT>&

operator<<( std::basic_ostream<CharT>& os,

const optm_string<CharT, Traits, Allocator>& str )

{

return os.write( str.data(), str.size() );

}

template< typename CharT, typename Traits, typename Allocator >

std::basic_istream<CharT>&

operator>>( std::basic_istream<CharT>& is,

optm_string<CharT, Traits, Allocator>& str )

{

typedef std::ctype<CharT> ctype_t;

typedef std::basic_istream<CharT> istream_type;

typedef std::basic_streambuf<CharT> streambuf_type;

typedef optm_string<CharT, Traits, Allocator> string_type;

typedef typename istream_type::int_type int_type;

typedef typename string_type::size_type size_type;

size_type extracted = 0;

typename istream_type::sentry cerb( is, false );

if( cerb )

{

str.erase();

std::streamsize w = is.width();

size_type n = w > 0 ? (size_type)w : str.max_size();

const ctype_t& ct = std::use_facet<ctype_t>( is.getloc() );

const int_type eof = Traits::eof();

streambuf_type* buf = is.rdbuf();

int_type c = buf->sgetc();

while( extracted < n

&& !Traits::eq_int_type(c, eof)

&& !ct.is(std::ctype_base::space, c) )

{

str += Traits::to_char_type( c );

++extracted;

c = buf->snextc();

}

if( Traits::eq_int_type(c, eof) )

is.setstate( std::ios_base::eofbit );

is.width( 0 );

}

if( extracted == 0 )

is.setstate( std::ios_base::failbit );

return is;

}

template< typename CharT, typename Traits, typename Allocator >

std::basic_istream<CharT>&

getline( std::basic_istream<CharT>& is,

optm_string<CharT, Traits, Allocator>& str,

CharT delim = '\n' )

{

typedef std::ctype<CharT> ctype_t;

typedef std::basic_istream<CharT> istream_type;

typedef std::basic_streambuf<CharT> streambuf_type;

typedef optm_string<CharT, Traits, Allocator> string_type;

typedef typename istream_type::int_type int_type;

typedef typename string_type::size_type size_type;

size_type extracted = 0;

bool testdelim = false;

typename istream_type::sentry cerb( is, true );

if( cerb )

{

str.erase();

const int_type eof = Traits::eof();

size_type n = str.max_size();

streambuf_type* buf = is.rdbuf();

int_type c = buf->sbumpc();

int_type idelim = Traits::to_int_type( delim );

testdelim = Traits::eq_int_type( c, idelim );

while( extracted <= n

&& !Traits::eq_int_type(c, eof) && !testdelim )

{

str += Traits::to_char_type( c );

++extracted;

c = buf->sbumpc();

testdelim = Traits::eq_int_type( c, idelim );

}

if( Traits::eq_int_type(c, eof) )

is.setstate( std::ios_base::eofbit );

is.width( 0 );

}

if( extracted == 0 && !testdelim )

is.setstate( std::ios_base::failbit );

return is;

}

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__

#endif

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有