文件位置:young/string/y_basic_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_BASIC_STRING_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_BASIC_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 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 size )
{
alloc_data( size );
}
basic_string( const char_type* str )
{
size_type size = traits_type::length( str );
alloc_data( size );
assign( str, size );
}
basic_string( const char_type* str, size_type size )
{
alloc_data( size );
assign( str, size );
}
basic_string( size_type size, char_type c )
{
alloc_data( size );
assign( size, c );
}
basic_string( int size, char_type c )
{
alloc_data( static_cast<size_type>(size) );
assign( static_cast<size_type>(size), c );
}
basic_string( long size, char_type c )
{
alloc_data( static_cast<size_type>(size) );
assign( static_cast<size_type>(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()); }
const_reference front() const { return *begin(); }
const_reference back() const { return *(--end()); }
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() >= 1 )
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
{
if( index == size() )
return traits_type::eos();
else
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 ( index == size() ? traits_type::eos() : 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 = 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:
size_type string_bytes( size_type n );
void alloc_data( size_type n );
void dealloc_data()
{
if( m_data )
m_alloc.deallocate( m_data, ++m_capacity );
}
void data_copy( size_type pos, const char_type* source, size_type count )
{
if( count > 0 )
traits_type::copy( m_data + pos, source, count );
}
void data_move( size_type pos, const char_type* source, size_type count )
{
if( count > 0 )
traits_type::move( m_data + pos, source, count );
}
void data_set( size_type pos, const char_type& c, size_type count )
{
if( count > 0 )
traits_type::assign( m_data + pos, count, c );
}
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 basic_string<CharT, Traits, Allocator>::size_type
basic_string<CharT, Traits, Allocator>::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 basic_string<CharT, Traits, Allocator>::alloc_data( size_type n )
{
if( n < 1 )
init();
else
{
if( n > max_size() )
throw_length_error( "basic_string::alloc_data()" );
size_type bytes = string_bytes( n + 1 );
m_length = 0;
m_capacity = bytes / sizeof( char_type );
m_data = m_alloc.allocate( m_capacity );
--m_capacity;
}
}
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
basic_string<CharT, Traits, Allocator>::basic_string( const self& rhs,
size_type pos,
size_type count )
{
if( pos > rhs.size() )
throw_length_error( "basic_string::basic_string" );
count = min( rhs.size() - pos, count );
alloc_data( count );
data_copy( 0, rhs.data() + pos, count );
m_length = count;
}
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
basic_string<CharT, Traits, Allocator>&
basic_string<CharT, Traits, Allocator>::operator=( const self& rhs )
{
if( m_data != rhs.m_data )
{
if( capacity() < rhs.size() )
{
self temp( rhs, 0, rhs.size() );
swap( temp );
}
else
data_copy( 0, rhs.data(), rhs.size() );
m_length = rhs.size();
}
return *this;
}
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>&
basic_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( "basic_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 >
basic_string<CharT, Traits, Allocator>&
basic_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( "basic_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( "basic_string::replace()" );
size_type new_len = old_len - before_count + after_count;
if( capacity() < new_len )
{
self temp( new_len );
temp.data_copy( 0, data(), pos );
temp.data_copy( pos + after_count, data() + pos + before_count,
old_len - (pos + before_count) );
temp.data_copy( pos, str, after_count );
swap( temp );
}
else
{
data_move( pos + after_count, data() + pos + before_count,
old_len - (pos + before_count) );
data_copy( pos, str, after_count );
}
m_length = new_len;
return *this;
}
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
basic_string<CharT, Traits, Allocator>&
basic_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( "basic_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( "basic_string::replace()" );
size_type new_len = old_len - before_count + after_count;
if( capacity() < new_len )
{
self temp( new_len );
temp.data_copy( 0, data(), pos );
temp.data_copy( pos + after_count, data() + pos + before_count,
old_len - (pos + before_count) );
temp.data_set( pos, c, after_count );
swap( temp );
}
else
{
data_move( pos + after_count, data() + pos + before_count,
old_len - (pos + before_count) );
data_set( pos, c, after_count );
}
m_length = new_len;
return *this;
}
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
template< typename InputIterator >
basic_string<CharT, Traits, Allocator>&
basic_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( "basic_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( "basic_string::replace()" );
size_type new_len = old_len - before_count + after_count;
if( capacity() < new_len )
{
self temp( new_len );
temp.data_copy( 0, data(), pos );
temp.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_data + pos), *first );
swap( temp );
}
else
{
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_data + pos), *first );
}
m_length = new_len;
return *this;
}
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
inline
int basic_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 basic_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 basic_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( "basic_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 basic_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( "basic_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 basic_string<CharT, Traits, Allocator>::size_type
basic_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 basic_string<CharT, Traits, Allocator>::size_type
basic_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 basic_string<CharT, Traits, Allocator>::size_type
basic_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 basic_string<CharT, Traits, Allocator>::size_type
basic_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 basic_string<CharT, Traits, Allocator>::size_type
basic_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 basic_string<CharT, Traits, Allocator>::size_type
basic_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 basic_string<CharT, Traits, Allocator>::size_type
basic_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 basic_string<CharT, Traits, Allocator>::size_type
basic_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 basic_string<CharT, Traits, Allocator>::size_type
basic_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( basic_string<CharT, Traits, Allocator>& lhs,
basic_string<CharT, Traits, Allocator>& rhs )
{
lhs.swap( rhs );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//以下为重载的运算符
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( const basic_string<CharT, Traits, Allocator>& lhs,
const basic_string<CharT, Traits, Allocator>& rhs )
{
basic_string<CharT, Traits, Allocator> str( lhs );
str.append( rhs );
return str;
}
template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( const char* lhs, const basic_string<CharT, Traits, Allocator>& rhs )
{
basic_string<CharT, Traits, Allocator> str( lhs );
str.append( rhs );
return str;
}
template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( const basic_string<CharT, Traits, Allocator>& lhs, const char* rhs )
{
basic_string<CharT, Traits, Allocator> str( rhs );
str.append( lhs );
return str;
}
template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( const basic_string<CharT, Traits, Allocator>& lhs, CharT c )
{
basic_string<CharT, Traits, Allocator> str( lhs );
str.append( 1, c );
return str;
}
template< typename CharT, typename Traits, typename Allocator >
inline basic_string<CharT, Traits, Allocator>
operator+( CharT c, const basic_string<CharT, Traits, Allocator>& rhs )
{
basic_string<CharT, Traits, Allocator> str( rhs );
str.append( 1, c );
return str;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
inline bool operator==( const basic_string<CharT, Traits, Allocator>& lhs,
const basic_string<CharT, Traits, Allocator>& rhs )
{
return ( lhs.compare(rhs) == 0 );
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator==( const basic_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 basic_string<CharT, Traits, Allocator>& rhs )
{
return ( rhs.compare(lhs) == 0 );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
inline bool operator!=( const basic_string<CharT, Traits, Allocator>& lhs,
const basic_string<CharT, Traits, Allocator>& rhs )
{
return ( lhs.compare(rhs) != 0 );
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator!=( const basic_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 basic_string<CharT, Traits, Allocator>& rhs )
{
return ( rhs.compare(lhs) != 0 );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
inline bool operator<( const basic_string<CharT, Traits, Allocator>& lhs,
const basic_string<CharT, Traits, Allocator>& rhs )
{
return ( lhs.compare(rhs) < 0 );
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator<( const basic_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 basic_string<CharT, Traits, Allocator>& rhs )
{
return ( rhs.compare(lhs) > 0 );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
inline bool operator<=( const basic_string<CharT, Traits, Allocator>& lhs,
const basic_string<CharT, Traits, Allocator>& rhs )
{
return ( lhs.compare(rhs) <= 0 );
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator<=( const basic_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 basic_string<CharT, Traits, Allocator>& rhs )
{
return ( rhs.compare(lhs) >= 0 );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
inline bool operator>( const basic_string<CharT, Traits, Allocator>& lhs,
const basic_string<CharT, Traits, Allocator>& rhs )
{
return ( lhs.compare(rhs) > 0 );
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator>( const basic_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 basic_string<CharT, Traits, Allocator>& rhs )
{
return ( rhs.compare(lhs) < 0 );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename CharT, typename Traits, typename Allocator >
inline bool operator>=( const basic_string<CharT, Traits, Allocator>& lhs,
const basic_string<CharT, Traits, Allocator>& rhs )
{
return ( lhs.compare(rhs) >= 0 );
}
template< typename CharT, typename Traits, typename Allocator >
inline bool operator>=( const basic_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 basic_string<CharT, Traits, Allocator>& rhs )
{
return ( rhs.compare(lhs) <= 0 );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------