/******************************************************************** * * * String.h * * 2009.8.23 * * by commander * * 模拟CString类 * ********************************************************************/ #include<iostream> #include<stdarg.h> //#include<sstream> //#include<string> //#include<stdio.h> using namespace std; namespace LBO_Object { #ifndef _LBO_Object_String #define _LBO_Object_String 1 class String { friend ostream & operator << ( ostream & output, const String & sourse ); friend bool operator == ( const char * char_sourse ,const String & sourse ); friend bool operator >= ( const char * char_sourse ,const String & sourse ); friend bool operator > ( const char * char_sourse ,const String & sourse ); friend bool operator < ( const char * char_sourse ,const String & sourse ); friend bool operator <= ( const char * char_sourse ,const String & sourse ); friend String & operator + ( const char * char_sourse , String & sourse ); public : String( void ); String( const char * sourse ); String( const String & sourse ); String & operator = ( const String & sourse ); String & operator = ( const char * sourse ); String & operator += ( const String & add ); String & operator += ( const char * add ); String & operator + ( const String & sourse ); String & operator + ( const char * sourse ); bool operator == ( String & sourse ) const; bool operator == ( char * sourse ) const; bool operator > ( String & sourse ) const; bool operator > ( char * sourse ) const; bool operator < ( String & sourse ) const; bool operator < ( char * sourse ) const; bool operator >= ( String & sourse ) const; bool operator >= ( char * sourse ) const; bool operator <= ( String & sourse ) const; bool operator <= ( char * sourse ) const; char operator [] ( int position ) const; int Size( void ) const; char *Word( void ) const; int Fine_First( const char fine_ch );//return the char position and can't fine //the char will return -1,but begin at zero int Fine_First( const char * fine_ch );//return the char position and can't fine //the char will return -1,but begin at zero int Fine( int start_position, const char fine_ch );//return the char position and can't fine //the char will return -1,but begin at start_position int Fine( int start_position, const char * fine_ch );//return the char position and can't fine //the char will return -1,but begin at start_position String Left( const int end_position );//get the string left of the end_position string String Right( const int start_position );//get the string right of the start_position string String Middile( const int start_position, const int end_position ); char * String_To_Char( void ) const; String & Capital_To_Lowercase( void ); String & Lowercase_To_Capital( void ); String Capital_To_Lowercase( const int position ); String Lowercase_To_Capital( const int position ); String Eraser( const int position ); String & Char_To_String( const char * sourse ); String & Interchange_String( String & change_sourse_string );//互相交换字符串 String Format( char * parameter, ... ); virtual ~String( void ); protected : int m_size; char *m_word; }; #endif }//end of namespace LBO_Object
/******************************************************************** * * * String.cpp * * 2009.8.23 * * by commander * * * ********************************************************************/ #include"String.h" namespace LBO_Object { inline int String::Size() const { return m_size; } inline char * String::Word() const { return m_word; } String::String() : m_size( 0 ), m_word( NULL ) { } String::String( const char * sourse ) { if ( sourse == NULL ) { m_size = 0; m_word = NULL; } else { m_word = new char[ ( m_size = strlen( sourse ) ) + 1 ]; if ( m_word == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, sourse ), sourse ) ) { cerr << " copy error(45)..."; exit( 0 ); } } } String::String( const String & sourse ) { if ( sourse.Word() == NULL ) { m_size = 0; m_word = NULL; } else { m_word = new char[ ( m_size = sourse.Size() ) + 1 ]; if ( m_word == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, sourse.Word() ), sourse.Word() ) ) { cerr << " copy error(70)..."; exit( 0 ); } } } String & String::operator = ( const String & sourse ) { if ( this == &sourse ) return *this; if ( sourse.Word() == NULL ) { if ( m_word != NULL ) //避免 m_word = NULL 时释放内存 delete [] m_word; m_size = 0; m_word = NULL; } else { m_size = sourse.Size(); delete [] m_word; m_word = new char[ m_size + 1 ]; if ( m_word == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, sourse.Word() ), sourse.Word() ) ) { cerr << " copy error(102)..."; exit( 0 ); } } return *this; } String & String::operator = ( const char * sourse ) { if ( sourse == NULL ) { if ( m_size != 0 ) delete [] m_word; m_size = 0; m_word = NULL; } else { m_size = strlen( sourse ); m_word = new char[ m_size + 1 ]; if ( m_word == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, sourse ), sourse ) ) { cerr << " copy error(129)..."; exit( 0 ); } } return *this; } String::~String() { delete [] m_word; } ostream & operator << ( ostream & output, const String & sourse ) { if ( sourse.Word() == NULL ) { output << "Word : NULL"; } else { output << "Word : " << sourse.Word(); } return output; } bool String::operator == ( String & sourse ) const { if ( sourse.Word() == NULL ) return false; if ( m_word == NULL ) return false; return strcmp( m_word, sourse.Word() ) ? false : true; } bool String::operator == ( char * sourse ) const { if ( sourse == NULL ) return false; if ( m_word == NULL ) return false; return strcmp( m_word, sourse ) ? false : true; } bool operator == ( const char * char_sourse, const String & sourse ) { if ( char_sourse == NULL ) return false; if ( sourse.Word() == NULL ) return false; return strcmp( sourse.Word(), char_sourse ) ? false : true; } bool String::operator > ( String & sourse ) const { if ( sourse.Word() == NULL ) if ( m_word == NULL ) return false; else return true; if ( m_word == NULL ) return false; else return strcmp( m_word, sourse.Word() ) > 0 ? true : false; } bool String::operator > ( char * sourse ) const { if ( sourse == NULL ) if ( m_word == NULL ) return false; else return true; if ( m_word == NULL ) return false; else return strcmp( m_word, sourse ) > 0 ? true : false; } bool operator > ( const char * char_sourse ,const String & sourse ) { if ( char_sourse == NULL ) if ( sourse.Word() == NULL ) return false; else return true; if ( sourse.Word() == NULL ) return false; else return strcmp( sourse.Word(), char_sourse ) > 0 ? true : false; } bool String::operator < ( String & sourse ) const { if ( sourse.Word() == NULL ) return false; if ( m_word == NULL ) return true; else return strcmp( m_word, sourse.Word() ) < 0 ? true : false; } bool String::operator < ( char * sourse ) const { if ( sourse == NULL ) return false; if ( m_word == NULL ) return true; else return strcmp( m_word, sourse ) < 0 ? true : false; } bool operator < ( const char * char_sourse ,const String & sourse ) { if ( char_sourse == NULL ) return false; if ( sourse.Word() == NULL ) return true; else return strcmp( sourse.Word(), char_sourse ) < 0 ? true : false; } bool String::operator >= ( String & sourse ) const { if ( sourse.Word() == NULL ) if ( m_word == NULL ) return false; else return true; if ( m_word == NULL ) return false; else return strcmp( m_word, sourse.Word() ) >= 0 ? true : false; } bool String::operator >= ( char * sourse ) const { if ( sourse == NULL ) if ( m_word == NULL ) return false; else return true; if ( m_word == NULL ) return false; else return strcmp( m_word, sourse ) >= 0 ? true : false; } bool operator >= ( const char * char_sourse ,const String & sourse ) { if ( char_sourse == NULL ) if ( sourse.Word() == NULL ) return false; else return true; if ( sourse.Word() == NULL ) return false; else return strcmp( sourse.Word(), char_sourse ) >= 0 ? true : false; } bool String::operator <= ( String & sourse ) const { if ( sourse.Word() == NULL ) return false; if ( m_word == NULL ) return true; else return strcmp( m_word, sourse.Word() ) <= 0 ? true : false; } bool String::operator <= ( char * sourse ) const { if ( sourse == NULL ) return false; if ( m_word == NULL ) return true; else return strcmp( m_word, sourse ) <= 0 ? true : false; } bool operator <= ( const char * char_sourse ,const String & sourse ) { if ( char_sourse == NULL ) return false; if ( sourse.Word() == NULL ) return true; else return strcmp( sourse.Word(), char_sourse ) <= 0 ? true : false; } String & String::operator += ( const String & add ) { if ( add.Word() == NULL ) return *this; if ( this == &add )//自我复制 { char *temp = new char[ ( m_size += m_size ) + 1 ]; if ( temp == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } strcat( strcpy( temp, m_word ), m_word ); delete [] m_word; m_word = new char[ m_size + 1 ]; if ( m_word == NULL ) { delete [] temp; cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, temp ), temp ) ) { delete [] temp; cerr << " copy error(335)..."; exit( 0 ); } delete [] temp; return *this; } if ( m_word != NULL ) { char *temp = new char[ m_size + 1 ]; if ( temp == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( temp, m_word ), m_word ) ) { delete [] temp; cerr << " copy error(353)..."; exit( 0 ); } delete [] m_word; m_word = new char[ ( m_size += add.Size() ) + 1 ]; if ( m_word == NULL ) { delete [] temp; cerr << " can't applay buffer"; exit( 0 ); } strcat( strcpy( m_word, temp ), add.Word() ); delete [] temp; } else { m_size = add.Size(); m_word = new char[ add.Size() + 1 ]; if ( m_word == NULL ) { cerr << " can't applay for buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, add.Word() ), add.Word() ) ) { cerr << " copy error(378)..."; exit( 0 ); } } return *this; } String & String::operator += ( const char * add ) { if ( add == NULL ) return *this; if ( m_word != NULL ) { char *temp = new char[ m_size +1 ]; if ( temp == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( temp, m_word ), m_word ) ) { delete [] temp; cerr << " copy error(400)..."; exit( 0 ); } m_size += strlen( add ); delete [] m_word; m_word = new char[ m_size +1 ]; if ( m_word == NULL ) { delete [] temp; cerr << " can't applay buffer"; exit( 0 ); } strcat( strcpy( m_word, temp ), add ); delete [] temp; } else { m_size = strlen( add ); m_word = new char[ m_size + 1 ]; if ( m_word == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, add ), add ) ) { cerr << " copy error(425)..."; exit( 0 ); } } return *this; } String & String::operator + ( const String & sourse ) { if ( sourse.Word() == NULL ) return *this; if ( this == &sourse )//自我复制 { char *temp = new char[ ( m_size += m_size ) + 1 ]; if ( temp == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } strcat( strcpy( temp, m_word ), m_word ); delete [] m_word; m_word = new char[ m_size + 1 ]; if ( m_word == NULL ) { delete [] temp; cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, temp ), temp ) ) { delete [] temp; cerr << " copy error(457)..."; exit( 0 ); } delete [] temp; return *this; } if ( m_word != NULL ) { char *temp = new char[ m_size + 1 ]; if ( temp == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( temp, m_word ), m_word ) ) { delete [] temp; cerr << " copy error(475)..."; exit( 0 ); } delete [] m_word; m_word = new char[ ( m_size += sourse.Size() ) + 1 ]; if ( m_word == NULL ) { delete [] temp; cerr << " can't applay buffer"; exit( 0 ); } strcat( strcpy( m_word, temp ), sourse.Word() ); delete [] temp; } else { m_word = new char[ ( m_size = sourse.Size() ) + 1 ]; if ( m_word == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, sourse.Word() ), sourse.Word() ) ) { cerr << " copy error(500)..."; exit( 0 ); } } return *this; } String & String::operator + ( const char * sourse ) { if ( sourse == NULL ) return *this; if ( m_word != NULL ) { char *temp = new char[ m_size + 1 ]; if ( temp == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( temp, m_word ), m_word ) ) { delete [] temp; cerr << " copy error(522)..."; exit( 0 ); } delete [] m_word; m_word = new char[ ( m_size += strlen( sourse ) ) + 1 ]; if ( m_word == NULL ) { delete [] temp; cerr << " can't applay buffer"; exit( 0 ); } strcat( strcpy( m_word, temp ), sourse ); delete [] temp; } else { m_word = new char[ ( m_size = strlen( sourse ) ) + 1 ]; if ( m_word == NULL ) { cerr << " can't applay for buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, sourse ), sourse ) ) { cerr << " copy error(547)..."; exit( 0 ); } } return *this; } String & operator + ( const char * char_sourse , String & sourse ) { if ( char_sourse == NULL ) return sourse; else { String temp( char_sourse ); if ( sourse.Word() != NULL ) { return sourse = temp + sourse; } else { sourse = temp; return sourse; } } } char String::operator [] ( int position ) const { if ( position < 0 || position >= m_size || m_word == NULL ) return '\0'; return m_word[ position ]; } int String::Fine_First( const char fine_ch ) { if ( m_word == NULL ) return -1; int count = 0; while ( m_word[ count ] != '\0' && fine_ch != m_word[ count ] ) count++; return m_word[ count ] == '\0' ? -1 : count; } int String::Fine( int start_position, const char fine_ch ) { if ( start_position >= m_size || start_position < 0 ) return -1; while ( m_word[ start_position ] != '\0' && fine_ch != m_word[ start_position ] ) start_position++; return m_word[ start_position ] == '\0' ? -1 : start_position; } String String::Left( const int end_position ) { if ( m_word == NULL ) { String error( "NULL_STRING" ); return error; } if ( end_position < 0 || end_position >= m_size ) { String error( "ERROR_POSITION" ); return error; } char *temp = new char[ end_position + 2 ]; if ( temp == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } m_size = end_position +1; strncpy( temp, m_word, m_size ); temp[ end_position + 1 ] = '\0'; delete [] m_word; m_word = new char[ m_size + 1 ]; if ( m_word == NULL ) { delete [] temp; cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, temp ), temp ) ) { delete [] temp; cerr << " copy error(630)..."; exit( 0 ); } delete [] temp; return *this; } String String::Right( const int start_position ) { if ( m_word == NULL ) { String error( "NULL_STRING" ); return error; } if ( start_position < 0 || start_position >= m_size ) { String error( "ERROR_POSITION" ); return error; } m_size = m_size - start_position; char *temp = new char[ m_size + 1 ]; if ( temp == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } strncpy( temp, m_word + start_position, m_size + 1 ); delete [] m_word; m_word = new char[ m_size + 1 ]; if ( m_word == NULL ) { delete [] temp; cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, temp ), temp ) ) { delete [] temp; cerr << " copy error(669)..."; exit( 0 ); } delete [] temp; return *this; } char * String::String_To_Char( void ) const { return m_word; } String & String::Capital_To_Lowercase( void ) { if ( m_word == NULL ) return *this; for ( int i = 0; m_word[ i ] != '\0'; i++ ) m_word[ i ] = toupper( m_word[ i ] ); return *this; } String & String::Lowercase_To_Capital( void ) { if ( m_word == NULL ) return *this; for ( int i = 0; m_word[ i ] != '\0'; i++ ) m_word[ i ] = tolower( m_word[ i ] ); return *this; } String String::Capital_To_Lowercase( const int position ) { if ( m_word == NULL ) return *this; if ( position < 0 || position > m_size ) { String error( "ERROR_POSITION" ); return error; } m_word[ position ] = toupper( m_word[ position ] ); return *this; } String String::Lowercase_To_Capital( const int position ) { if ( m_word == NULL ) return *this; if ( position < 0 || position > m_size ) { String error( "ERROR_POSITION" ); return error; } m_word[ position ] = tolower( m_word[ position ] ); return *this; } String String::Eraser( const int position ) { if ( m_word == NULL )//以效率换取错误提示 { String error( "NULL_STRING" ); return error; } if ( position < 0 || position >= m_size ) { String error( "ERROR_POSITION" ); return error; } if ( m_size == 1 ) { m_size = 0; delete [] m_word; m_word = NULL; return *this; } const int bufsize = m_size; m_size -= 1; if ( position == 0 ) { char * const temp = new char[ bufsize + 1 ]; if ( temp == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( temp, m_word ), m_word ) ) { delete [] temp; cerr << " copy error(759)..."; exit( 0 ); } delete [] m_word; m_word = new char[ bufsize ]; if ( m_word == NULL ) { delete [] temp; cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, temp + 1 ), temp + 1 ) ) { delete [] temp; cerr << " copy error(772)..."; exit( 0 ); } delete [] temp; } else { if ( position == m_size ) { m_word[ m_size ] = '\0'; } else { char *temp = new char[ bufsize ]; if ( temp == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } strncpy( temp, m_word, position ); temp[ position ] = '\0'; strcat( temp, m_word + position + 1 ); delete [] m_word; m_word = new char[ bufsize ]; if ( m_word == NULL ) { delete [] temp; cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, temp ), temp ) ) { delete [] temp; cerr << " copy error(804)..."; exit( 0 ); } delete [] temp; } } return *this; } String & String::Char_To_String( const char * sourse ) { if ( sourse == NULL ) return *this; if ( m_word != NULL ) delete [] m_word; m_size = strlen( sourse ); m_word = new char[ m_size + 1 ]; if ( m_word == NULL ) { cerr << " can't applay buffer"; exit( 0 ); } if ( strcmp( strcpy( m_word, sourse ), sourse ) ) { cerr << " copy error(828)..."; exit( 0 ); } return *this; } String & String::Interchange_String( String & change_sourse_string ) { if ( m_word == NULL || change_sourse_string.Word() == NULL ) { cerr << "one of String is NULL"; exit( 0 ); } String temp = *this; *this = change_sourse_string; change_sourse_string = temp; return *this; } String String::Middile( const int start_position, const int end_position ) { if ( m_word == NULL ) { String error( "NULL_STRING" ); return error; } if ( start_position < 0 || start_position >= m_size || end_position < start_position || end_position > m_size ) { String error( "ERROR_POSITION" ); return error; } char *temp = new char[ m_size + 1 ]; if ( temp == NULL ) { cerr << "can't applay buffer"; exit( 0 ); } m_size = end_position - start_position; if ( strcmp( strcpy( temp, m_word ), m_word ) ) { delete [] temp; cerr << " copy error(874)..."; exit( 0 ); } delete [] m_word; m_word = new char[ m_size + 1 ]; if ( m_word == NULL ) { delete [] temp; cerr << "can't applay buffer"; exit( 0 ); } strncpy( m_word, temp + start_position, m_size ); m_word[ m_size ] = '\0'; delete [] temp; return *this; }// end of " String String::Middile( int start_position, int end_position ) " // add new foundation... int String::Fine_First( const char * fine_ch )//return the char position and can't fine //the char will return -1,but begin at zero { if ( strlen( fine_ch ) != 2 ) return - 1; if ( m_word == NULL ) return - 1; int count = 0; char fine[ 3 ] = { 0 }; while ( m_word[ count ] != '\0' && strcmp( fine, fine_ch ) ) { fine[ 0 ] = m_word[ count++ ]; fine[ 1 ] = m_word[ count ]; fine[ 2 ] = '\0'; } return m_word[ count ] == '\0' ? -1 : count - 1; } int String::Fine( int start_position, const char * fine_ch )//return the char position and can't fine //the char will return -1,but begin at start_position { if ( start_position >= m_size || start_position < 0 ) return -1; if ( strlen( fine_ch ) != 2 ) return - 1; char fine[ 3 ] = { 0 }; while ( m_word[ start_position ] != '\0' && strcmp( fine, fine_ch ) ) { fine[ 0 ] = m_word[ start_position++ ]; fine[ 1 ] = m_word[ start_position ]; fine[ 2 ] = '\0'; } return m_word[ start_position ] == '\0' ? -1 : start_position - 1; } String String::Format( char * parameter, ... ) { try { char buffer[ 2048 ] = { 0 }; va_list parameter_list; va_start( parameter_list, parameter ); if ( _vsnprintf( buffer, 2048, parameter, parameter_list ) == -1 ) return String( "ERROR_FORMAT" ); return String( buffer ); } catch (...) { return String( "ERROR_FORMAT" ); } } // can't add new foundation.... }// end of namespace LBO_Object.....