Loading src/TNL/Matrices/MatrixReader_impl.h +3 −3 Original line number Diff line number Diff line Loading @@ -159,7 +159,7 @@ bool MatrixReader< Matrix >::checkMtxHeader( const String& header, bool& symmetric ) { Containers::List< String > parsedLine; header.parse( parsedLine ); header.split( parsedLine ); if( parsedLine.getSize() < 5 ) return false; if( parsedLine[ 0 ] != "%%MatrixMarket" ) Loading Loading @@ -226,7 +226,7 @@ bool MatrixReader< Matrix >::readMtxHeader( std::istream& file, } parsedLine.reset(); line. parse( parsedLine ); line.split( parsedLine ); if( parsedLine. getSize() != 3 ) { std::cerr << "Wrong number of parameters in the matrix header." << std::endl; Loading Loading @@ -361,7 +361,7 @@ bool MatrixReader< Matrix >::parseMtxLineWithElement( const String& line, RealType& value ) { Containers::List< String > parsedLine; line.parse( parsedLine ); line.split( parsedLine ); if( parsedLine.getSize() != 3 ) { std::cerr << "Wrong number of parameters in the matrix row at line:" << line << std::endl; Loading src/TNL/String.cpp +203 −191 Original line number Diff line number Diff line Loading @@ -10,7 +10,6 @@ #include <cstring> #include <string.h> #include <assert.h> #include <TNL/String.h> #include <TNL/Assert.h> #include <TNL/Containers/List.h> Loading @@ -25,96 +24,91 @@ namespace TNL { const unsigned int STRING_PAGE = 256; String::String() : string( nullptr ), length( 0 ) { string = new char[ STRING_PAGE ]; string[ 0 ] = 0; length = STRING_PAGE; setString( nullptr ); } String::String( const char* c, int prefix_cut_off, int sufix_cut_off ) : string( 0 ), length( 0 ) : string( nullptr ), length( 0 ) { setString( c, prefix_cut_off, sufix_cut_off ); } String::String( const String& str ) : string( 0 ), length( 0 ) : string( nullptr ), length( 0 ) { setString( str.getString() ); } String :: String( unsigned number ) : string( 0 ), length( 0 ) String String::getType() { this->setString( convertToString( number ).getString() ); return String( "String" ); } String :: String( int number ) : string( 0 ), length( 0 ) String::~String() { this->setString( convertToString( number ).getString() ); if( string ) delete[] string; } String :: String( unsigned long int number ) : string( 0 ), length( 0 ) int String::getLength() const { this->setString( convertToString( number ).getString() ); return getSize(); } String :: String( long int number ) : string( 0 ), length( 0 ) int String::getSize() const { this->setString( convertToString( number ).getString() ); return strlen( string ); } String :: String( float number ) : string( 0 ), length( 0 ) int String::getAllocatedSize() const { this->setString( convertToString( number ).getString() ); return length; } String :: String( double number ) : string( 0 ), length( 0 ) void String::setSize( int size ) { this->setString( convertToString( number ).getString() ); TNL_ASSERT_GE( size, 0, "string size must be non-negative" ); const int _length = STRING_PAGE * ( size / STRING_PAGE + 1 ); if( length != _length ) { if( string ) { delete[] string; string = nullptr; } String String :: getType() { return String( "String" ); string = new char[ _length ]; length = _length; } String :: ~String() { if( string ) delete[] string; } void String::setString( const char* c, int prefix_cut_off, int sufix_cut_off ) { if( ! c ) { if( ! c ) { if( ! string ) { string = new char[ STRING_PAGE ]; length = STRING_PAGE; } setSize( 1 ); string[ 0 ] = 0; return; } int c_len = ( int ) strlen( c ); int _length = max( 0, c_len - prefix_cut_off - sufix_cut_off ); const int c_len = ( int ) strlen( c ); const int _length = max( 0, c_len - prefix_cut_off - sufix_cut_off ); if( length < _length || length == 0 ) { if( string ) delete[] string; length = STRING_PAGE * ( _length / STRING_PAGE + 1 ); string = new char[ length ]; } setSize( _length ); TNL_ASSERT( string, ); memcpy( string, c + min( c_len, prefix_cut_off ), sizeof( char ) * ( _length ) ); memcpy( string, c + min( c_len, prefix_cut_off ), _length * sizeof( char ) ); string[ _length ] = 0; } const char* String::getString() const { return string; } char* String::getString() { return string; } const char& String::operator[]( int i ) const { TNL_ASSERT( i >= 0 && i < length, Loading @@ -129,9 +123,13 @@ char& String :: operator[]( int i ) return string[ i ]; } String& String :: operator = ( const String& str ) /**** * Operators for C strings */ String& String::operator=( const char* str ) { setString( str. getString() ); setString( str ); return *this; } Loading @@ -139,8 +137,8 @@ String& String :: operator += ( const char* str ) { if( str ) { int len1 = strlen( string ); int len2 = strlen( str ); const int len1 = strlen( string ); const int len2 = strlen( str ); if( len1 + len2 < length ) memcpy( string + len1, str, sizeof( char ) * ( len2 + 1 ) ); else Loading @@ -155,24 +153,29 @@ String& String :: operator += ( const char* str ) return *this; } String& String :: operator += ( const char str ) String String::operator+( const char* str ) const { int len1 = strlen( string ); if( len1 + 1 < length ) return String( *this ) += str; } bool String::operator==( const char* str ) const { string[ len1 ] = str; string[ len1 + 1 ] = 0; TNL_ASSERT( string && str, ); return strcmp( string, str ) == 0; } else bool String::operator!=( const char* str ) const { char* tmp_string = string; length += STRING_PAGE; string = new char[ length ]; memcpy( string, tmp_string, sizeof( char ) * len1 ); string[ len1 ] = str; string[ len1 + 1 ] = 0; return ! operator==( str ); } /**** * Operators for Strings */ String& String::operator=( const String& str ) { setString( str.getString() ); return *this; } Loading @@ -186,19 +189,10 @@ String String :: operator + ( const String& str ) const return String( *this ) += str; } String String :: operator + ( const char* str ) const { return String( *this ) += str; } bool String::operator==( const String& str ) const { assert( string && str. string ); if( str. length != length ) return false; if( strcmp( string, str. string ) == 0 ) return true; return false; TNL_ASSERT( string && str.string, ); return strcmp( string, str.string ) == 0; } bool String::operator!=( const String& str ) const Loading @@ -206,78 +200,121 @@ bool String :: operator != ( const String& str ) const return ! operator==( str ); } bool String :: operator == ( const char* str ) const /**** * Operators for single characters */ String& String::operator=( char str ) { //cout << ( void* ) string << " " << ( void* ) str << std::endl; assert( string && str ); if( strcmp( string, str ) == 0 ) return true; return false; string[ 0 ] = str; string[ 1 ] = 0; return *this; } String :: operator bool () const String& String::operator+=( const char str ) { if( string[ 0 ] ) return true; return false; const int len1 = strlen( string ); if( len1 + 1 < length ) { string[ len1 ] = str; string[ len1 + 1 ] = 0; } else { char* tmp_string = string; length += STRING_PAGE; string = new char[ length ]; memcpy( string, tmp_string, sizeof( char ) * len1 ); string[ len1 ] = str; string[ len1 + 1 ] = 0; } bool String :: operator ! () const return *this; } String String::operator+( char str ) const { return ! operator bool(); return String( *this ) += str; } bool String :: operator != ( const char* str ) const bool String::operator==( char str ) const { return *this == String( str ); } bool String::operator!=( char str ) const { return ! operator==( str ); } int String :: getLength() const String::operator bool () const { return strlen( string ); if( string[ 0 ] ) return true; return false; } void String:: replace( const String& pattern, const String& replaceWith ) bool String::operator!() const { return ! operator bool(); } String String::replace( const String& pattern, const String& replaceWith, int count ) const { int occurences( 0 ); int patternLength = pattern.getLength(); const int length = this->getLength(); int patternPointer( 0 ); const int patternLength = pattern.getLength(); const int replaceWithLength = replaceWith.getLength(); int patternPointer = 0; int occurrences = 0; for( int i = 0; i < length; i++ ) { if( this->string[ i ] == pattern[ patternPointer ] ) patternPointer++; if( patternPointer == patternLength ) { occurences++; occurrences++; patternPointer = 0; } } const int replaceWithLength = replaceWith.getLength(); int newStringLength = length + occurences * ( replaceWithLength - patternLength ); char* newString = new char[ newStringLength ]; int newStringPointer( 0 ); int lastPatternStart( 0 ); for( int i = 0; i < length; i++ ) { if( count > 0 && occurrences > count ) occurrences = count; String newString; const int newStringLength = length + occurrences * ( replaceWithLength - patternLength ); newString.setSize( newStringLength ); int newStringHead = 0; patternPointer = 0; occurrences = 0; for( int i = 0; i < length; i++ ) { // copy current character newString[ newStringHead++ ] = this->string[ i ]; // check if pattern matches if( this->string[ i ] == pattern[ patternPointer ] ) { if( patternPointer == 0 ) lastPatternStart = newStringPointer; patternPointer++; } newString[ newStringPointer++ ] = this->string[ i ]; if( patternPointer == patternLength ) { newStringPointer = lastPatternStart; else patternPointer = 0; // handle full match if( patternPointer == patternLength ) { // skip unwanted replacements if( count == 0 || occurrences < count ) { newStringHead -= patternLength; for( int j = 0; j < replaceWithLength; j++ ) newString[ newStringPointer++ ] = replaceWith[ j ]; newString[ newStringHead++ ] = replaceWith[ j ]; } occurrences++; patternPointer = 0; } } delete[] this->string; this->string = newString; newString[ newStringHead ] = 0; return newString; } String Loading @@ -297,15 +334,23 @@ String::strip( char strip ) const return ""; } const char* String :: getString() const int String::split( Containers::List< String >& list, const char separator ) const { return string; } char* String :: getString() list.reset(); String copy( *this ); int len = copy.getLength(); for( int i = 0; i < len; i ++ ) if( copy[ i ] == separator ) copy[ i ] = 0; for( int i = 0; i < len; i ++ ) { return string; if( copy[ i ] == 0 ) continue; String new_string; new_string.setString( ©.getString()[ i ] ); i += new_string.getLength(); list.Append( new_string ); } return list.getSize(); } Loading @@ -325,31 +370,12 @@ bool String :: save( File& file ) const bool String::load( File& file ) { int _length; if( ! file. read( &_length ) ) { if( ! file.read( &_length ) ) { std::cerr << "I was not able to read String length." << std::endl; return false; } if( ! _length ) { string[ 0 ] = 0; length = 0; return true; } if( string && length < _length ) { delete[] string; string = NULL; } if( ! string ) { //dbgCout( "Reallocating string..." ); length = STRING_PAGE * ( _length / STRING_PAGE + 1 ); string = new char[ length ]; } if( ! file. read( string, _length ) ) { setSize( _length ); if( _length && ! file.read( string, _length ) ) { std::cerr << "I was not able to read a String with a length " << length << "." << std::endl; return false; } Loading @@ -363,7 +389,7 @@ void String :: MPIBcast( int root, MPI_Comm comm ) dbgFunctionName( "mString", "MPIBcast" ); int iproc; MPI_Comm_rank( MPI_COMM_WORLD, &iproc ); assert( string ); TNL_ASSERT( string, ); int len = strlen( string ); MPI_Bcast( &len, 1, MPI_INT, root, comm ); dbgExpr( iproc ); Loading @@ -388,28 +414,14 @@ bool String :: getLine( std::istream& stream ) { std::string str; getline( stream, str ); this->setString( str. data() ); this->setString( str.c_str() ); if( ! ( *this ) ) return false; return true; } int String :: parse( Containers::List< String >& list, const char separator ) const { list.reset(); String copy( *this ); int len = copy. getLength(); for( int i = 0; i < len; i ++ ) if( copy[ i ] == separator ) copy[ i ] = 0; for( int i = 0; i < len; i ++ ) String operator+( char string1, const String& string2 ) { if( copy[ i ] == 0 ) continue; String new_string; new_string. setString( ©. getString()[ i ] ); i += new_string. getLength(); list. Append( new_string ); } return list. getSize(); return String( string1 ) + string2; } String operator+( const char* string1, const String& string2 ) Loading src/TNL/String.h +59 −61 Original line number Diff line number Diff line Loading @@ -10,7 +10,6 @@ #pragma once #include <stdio.h> #include <iostream> #include <sstream> #include <TNL/mpi-supp.h> Loading @@ -23,6 +22,9 @@ namespace Containers { template< class T > class List; } template< typename T > String convertToString( const T& value ); //! Class for managing strings class String { Loading @@ -33,7 +35,6 @@ class String int length; public: //! Basic constructor String(); Loading @@ -50,21 +51,26 @@ class String //! Copy constructor String( const String& str ); //! Convert number to a string String( unsigned number ); String( int number ); String( unsigned long int number ); //! Convert anything to a string template< typename T > String( T value ) : string( nullptr ), length( 0 ) { setString( convertToString( value ).getString() ); } String( long int number ); //! Destructor ~String(); String( float number ); //! Return length of the string int getLength() const; int getSize() const; String( double number ); //! Return currently allocated size int getAllocatedSize() const; //! Destructor ~String(); //! Reserve space for given number of characters void setSize( int size ); //! Set string from given char pointer /*! @param prefix_cut_off says length of the prefix that is going to be omitted and Loading @@ -87,39 +93,31 @@ class String char& operator[]( int i ); /**** * TODO: the operators do not work properly * for example String + const char* * Operators for C strings */ //! Operator = String& operator = ( const String& str ); //! Operator += String& operator=( const char* str ); String& operator+=( const char* str ); String operator+( const char* str ) const; bool operator==( const char* str ) const; bool operator!=( const char* str ) const; //! Operator += String& operator += ( const char str ); //! Operator += /**** * Operators for Strings */ String& operator=( const String& str ); String& operator+=( const String& str ); //! Operator + String operator+( const String& str ) const; //! Operator + String operator + ( const char* str ) const; //! Comparison operator bool operator==( const String& str ) const; //! Comparison operator bool operator!=( const String& str ) const; //! Comparison operator bool operator == ( const char* ) const; //! Comparison operator bool operator != ( const char* ) const; /**** * Operators for single characters */ String& operator=( char str ); String& operator+=( char str ); String operator+( char str ) const; bool operator==( char str ) const; bool operator!=( char str ) const; //! Cast to bool operator operator bool() const; Loading @@ -127,14 +125,15 @@ class String //! Cast to bool with negation operator bool operator!() const; //! Return length of the string int getLength() const; void replace( const String& pattern, const String& replaceWith ); String replace( const String& pattern, const String& replaceWith, int count = 0 ) const; String strip( char strip = ' ' ) const; //! Split the string into list of strings w.r.t. given separator. int split( Containers::List< String >& list, const char separator = ' ' ) const; //! Write to a binary file bool save( File& file ) const; Loading @@ -147,12 +146,11 @@ class String //! Read one line from given stream. bool getLine( std::istream& stream ); //! Parse the string into list of strings w.r.t. given separator. int parse( Containers::List< String >& list, const char separator = ' ' ) const; friend std::ostream& operator<<( std::ostream& stream, const String& str ); }; String operator+( char string1, const String& string2 ); String operator+( const char* string1, const String& string2 ); std::ostream& operator<<( std::ostream& stream, const String& str ); Loading src/UnitTests/StringTest.cpp +185 −44 File changed.Preview size limit exceeded, changes collapsed. Show changes Loading
src/TNL/Matrices/MatrixReader_impl.h +3 −3 Original line number Diff line number Diff line Loading @@ -159,7 +159,7 @@ bool MatrixReader< Matrix >::checkMtxHeader( const String& header, bool& symmetric ) { Containers::List< String > parsedLine; header.parse( parsedLine ); header.split( parsedLine ); if( parsedLine.getSize() < 5 ) return false; if( parsedLine[ 0 ] != "%%MatrixMarket" ) Loading Loading @@ -226,7 +226,7 @@ bool MatrixReader< Matrix >::readMtxHeader( std::istream& file, } parsedLine.reset(); line. parse( parsedLine ); line.split( parsedLine ); if( parsedLine. getSize() != 3 ) { std::cerr << "Wrong number of parameters in the matrix header." << std::endl; Loading Loading @@ -361,7 +361,7 @@ bool MatrixReader< Matrix >::parseMtxLineWithElement( const String& line, RealType& value ) { Containers::List< String > parsedLine; line.parse( parsedLine ); line.split( parsedLine ); if( parsedLine.getSize() != 3 ) { std::cerr << "Wrong number of parameters in the matrix row at line:" << line << std::endl; Loading
src/TNL/String.cpp +203 −191 Original line number Diff line number Diff line Loading @@ -10,7 +10,6 @@ #include <cstring> #include <string.h> #include <assert.h> #include <TNL/String.h> #include <TNL/Assert.h> #include <TNL/Containers/List.h> Loading @@ -25,96 +24,91 @@ namespace TNL { const unsigned int STRING_PAGE = 256; String::String() : string( nullptr ), length( 0 ) { string = new char[ STRING_PAGE ]; string[ 0 ] = 0; length = STRING_PAGE; setString( nullptr ); } String::String( const char* c, int prefix_cut_off, int sufix_cut_off ) : string( 0 ), length( 0 ) : string( nullptr ), length( 0 ) { setString( c, prefix_cut_off, sufix_cut_off ); } String::String( const String& str ) : string( 0 ), length( 0 ) : string( nullptr ), length( 0 ) { setString( str.getString() ); } String :: String( unsigned number ) : string( 0 ), length( 0 ) String String::getType() { this->setString( convertToString( number ).getString() ); return String( "String" ); } String :: String( int number ) : string( 0 ), length( 0 ) String::~String() { this->setString( convertToString( number ).getString() ); if( string ) delete[] string; } String :: String( unsigned long int number ) : string( 0 ), length( 0 ) int String::getLength() const { this->setString( convertToString( number ).getString() ); return getSize(); } String :: String( long int number ) : string( 0 ), length( 0 ) int String::getSize() const { this->setString( convertToString( number ).getString() ); return strlen( string ); } String :: String( float number ) : string( 0 ), length( 0 ) int String::getAllocatedSize() const { this->setString( convertToString( number ).getString() ); return length; } String :: String( double number ) : string( 0 ), length( 0 ) void String::setSize( int size ) { this->setString( convertToString( number ).getString() ); TNL_ASSERT_GE( size, 0, "string size must be non-negative" ); const int _length = STRING_PAGE * ( size / STRING_PAGE + 1 ); if( length != _length ) { if( string ) { delete[] string; string = nullptr; } String String :: getType() { return String( "String" ); string = new char[ _length ]; length = _length; } String :: ~String() { if( string ) delete[] string; } void String::setString( const char* c, int prefix_cut_off, int sufix_cut_off ) { if( ! c ) { if( ! c ) { if( ! string ) { string = new char[ STRING_PAGE ]; length = STRING_PAGE; } setSize( 1 ); string[ 0 ] = 0; return; } int c_len = ( int ) strlen( c ); int _length = max( 0, c_len - prefix_cut_off - sufix_cut_off ); const int c_len = ( int ) strlen( c ); const int _length = max( 0, c_len - prefix_cut_off - sufix_cut_off ); if( length < _length || length == 0 ) { if( string ) delete[] string; length = STRING_PAGE * ( _length / STRING_PAGE + 1 ); string = new char[ length ]; } setSize( _length ); TNL_ASSERT( string, ); memcpy( string, c + min( c_len, prefix_cut_off ), sizeof( char ) * ( _length ) ); memcpy( string, c + min( c_len, prefix_cut_off ), _length * sizeof( char ) ); string[ _length ] = 0; } const char* String::getString() const { return string; } char* String::getString() { return string; } const char& String::operator[]( int i ) const { TNL_ASSERT( i >= 0 && i < length, Loading @@ -129,9 +123,13 @@ char& String :: operator[]( int i ) return string[ i ]; } String& String :: operator = ( const String& str ) /**** * Operators for C strings */ String& String::operator=( const char* str ) { setString( str. getString() ); setString( str ); return *this; } Loading @@ -139,8 +137,8 @@ String& String :: operator += ( const char* str ) { if( str ) { int len1 = strlen( string ); int len2 = strlen( str ); const int len1 = strlen( string ); const int len2 = strlen( str ); if( len1 + len2 < length ) memcpy( string + len1, str, sizeof( char ) * ( len2 + 1 ) ); else Loading @@ -155,24 +153,29 @@ String& String :: operator += ( const char* str ) return *this; } String& String :: operator += ( const char str ) String String::operator+( const char* str ) const { int len1 = strlen( string ); if( len1 + 1 < length ) return String( *this ) += str; } bool String::operator==( const char* str ) const { string[ len1 ] = str; string[ len1 + 1 ] = 0; TNL_ASSERT( string && str, ); return strcmp( string, str ) == 0; } else bool String::operator!=( const char* str ) const { char* tmp_string = string; length += STRING_PAGE; string = new char[ length ]; memcpy( string, tmp_string, sizeof( char ) * len1 ); string[ len1 ] = str; string[ len1 + 1 ] = 0; return ! operator==( str ); } /**** * Operators for Strings */ String& String::operator=( const String& str ) { setString( str.getString() ); return *this; } Loading @@ -186,19 +189,10 @@ String String :: operator + ( const String& str ) const return String( *this ) += str; } String String :: operator + ( const char* str ) const { return String( *this ) += str; } bool String::operator==( const String& str ) const { assert( string && str. string ); if( str. length != length ) return false; if( strcmp( string, str. string ) == 0 ) return true; return false; TNL_ASSERT( string && str.string, ); return strcmp( string, str.string ) == 0; } bool String::operator!=( const String& str ) const Loading @@ -206,78 +200,121 @@ bool String :: operator != ( const String& str ) const return ! operator==( str ); } bool String :: operator == ( const char* str ) const /**** * Operators for single characters */ String& String::operator=( char str ) { //cout << ( void* ) string << " " << ( void* ) str << std::endl; assert( string && str ); if( strcmp( string, str ) == 0 ) return true; return false; string[ 0 ] = str; string[ 1 ] = 0; return *this; } String :: operator bool () const String& String::operator+=( const char str ) { if( string[ 0 ] ) return true; return false; const int len1 = strlen( string ); if( len1 + 1 < length ) { string[ len1 ] = str; string[ len1 + 1 ] = 0; } else { char* tmp_string = string; length += STRING_PAGE; string = new char[ length ]; memcpy( string, tmp_string, sizeof( char ) * len1 ); string[ len1 ] = str; string[ len1 + 1 ] = 0; } bool String :: operator ! () const return *this; } String String::operator+( char str ) const { return ! operator bool(); return String( *this ) += str; } bool String :: operator != ( const char* str ) const bool String::operator==( char str ) const { return *this == String( str ); } bool String::operator!=( char str ) const { return ! operator==( str ); } int String :: getLength() const String::operator bool () const { return strlen( string ); if( string[ 0 ] ) return true; return false; } void String:: replace( const String& pattern, const String& replaceWith ) bool String::operator!() const { return ! operator bool(); } String String::replace( const String& pattern, const String& replaceWith, int count ) const { int occurences( 0 ); int patternLength = pattern.getLength(); const int length = this->getLength(); int patternPointer( 0 ); const int patternLength = pattern.getLength(); const int replaceWithLength = replaceWith.getLength(); int patternPointer = 0; int occurrences = 0; for( int i = 0; i < length; i++ ) { if( this->string[ i ] == pattern[ patternPointer ] ) patternPointer++; if( patternPointer == patternLength ) { occurences++; occurrences++; patternPointer = 0; } } const int replaceWithLength = replaceWith.getLength(); int newStringLength = length + occurences * ( replaceWithLength - patternLength ); char* newString = new char[ newStringLength ]; int newStringPointer( 0 ); int lastPatternStart( 0 ); for( int i = 0; i < length; i++ ) { if( count > 0 && occurrences > count ) occurrences = count; String newString; const int newStringLength = length + occurrences * ( replaceWithLength - patternLength ); newString.setSize( newStringLength ); int newStringHead = 0; patternPointer = 0; occurrences = 0; for( int i = 0; i < length; i++ ) { // copy current character newString[ newStringHead++ ] = this->string[ i ]; // check if pattern matches if( this->string[ i ] == pattern[ patternPointer ] ) { if( patternPointer == 0 ) lastPatternStart = newStringPointer; patternPointer++; } newString[ newStringPointer++ ] = this->string[ i ]; if( patternPointer == patternLength ) { newStringPointer = lastPatternStart; else patternPointer = 0; // handle full match if( patternPointer == patternLength ) { // skip unwanted replacements if( count == 0 || occurrences < count ) { newStringHead -= patternLength; for( int j = 0; j < replaceWithLength; j++ ) newString[ newStringPointer++ ] = replaceWith[ j ]; newString[ newStringHead++ ] = replaceWith[ j ]; } occurrences++; patternPointer = 0; } } delete[] this->string; this->string = newString; newString[ newStringHead ] = 0; return newString; } String Loading @@ -297,15 +334,23 @@ String::strip( char strip ) const return ""; } const char* String :: getString() const int String::split( Containers::List< String >& list, const char separator ) const { return string; } char* String :: getString() list.reset(); String copy( *this ); int len = copy.getLength(); for( int i = 0; i < len; i ++ ) if( copy[ i ] == separator ) copy[ i ] = 0; for( int i = 0; i < len; i ++ ) { return string; if( copy[ i ] == 0 ) continue; String new_string; new_string.setString( ©.getString()[ i ] ); i += new_string.getLength(); list.Append( new_string ); } return list.getSize(); } Loading @@ -325,31 +370,12 @@ bool String :: save( File& file ) const bool String::load( File& file ) { int _length; if( ! file. read( &_length ) ) { if( ! file.read( &_length ) ) { std::cerr << "I was not able to read String length." << std::endl; return false; } if( ! _length ) { string[ 0 ] = 0; length = 0; return true; } if( string && length < _length ) { delete[] string; string = NULL; } if( ! string ) { //dbgCout( "Reallocating string..." ); length = STRING_PAGE * ( _length / STRING_PAGE + 1 ); string = new char[ length ]; } if( ! file. read( string, _length ) ) { setSize( _length ); if( _length && ! file.read( string, _length ) ) { std::cerr << "I was not able to read a String with a length " << length << "." << std::endl; return false; } Loading @@ -363,7 +389,7 @@ void String :: MPIBcast( int root, MPI_Comm comm ) dbgFunctionName( "mString", "MPIBcast" ); int iproc; MPI_Comm_rank( MPI_COMM_WORLD, &iproc ); assert( string ); TNL_ASSERT( string, ); int len = strlen( string ); MPI_Bcast( &len, 1, MPI_INT, root, comm ); dbgExpr( iproc ); Loading @@ -388,28 +414,14 @@ bool String :: getLine( std::istream& stream ) { std::string str; getline( stream, str ); this->setString( str. data() ); this->setString( str.c_str() ); if( ! ( *this ) ) return false; return true; } int String :: parse( Containers::List< String >& list, const char separator ) const { list.reset(); String copy( *this ); int len = copy. getLength(); for( int i = 0; i < len; i ++ ) if( copy[ i ] == separator ) copy[ i ] = 0; for( int i = 0; i < len; i ++ ) String operator+( char string1, const String& string2 ) { if( copy[ i ] == 0 ) continue; String new_string; new_string. setString( ©. getString()[ i ] ); i += new_string. getLength(); list. Append( new_string ); } return list. getSize(); return String( string1 ) + string2; } String operator+( const char* string1, const String& string2 ) Loading
src/TNL/String.h +59 −61 Original line number Diff line number Diff line Loading @@ -10,7 +10,6 @@ #pragma once #include <stdio.h> #include <iostream> #include <sstream> #include <TNL/mpi-supp.h> Loading @@ -23,6 +22,9 @@ namespace Containers { template< class T > class List; } template< typename T > String convertToString( const T& value ); //! Class for managing strings class String { Loading @@ -33,7 +35,6 @@ class String int length; public: //! Basic constructor String(); Loading @@ -50,21 +51,26 @@ class String //! Copy constructor String( const String& str ); //! Convert number to a string String( unsigned number ); String( int number ); String( unsigned long int number ); //! Convert anything to a string template< typename T > String( T value ) : string( nullptr ), length( 0 ) { setString( convertToString( value ).getString() ); } String( long int number ); //! Destructor ~String(); String( float number ); //! Return length of the string int getLength() const; int getSize() const; String( double number ); //! Return currently allocated size int getAllocatedSize() const; //! Destructor ~String(); //! Reserve space for given number of characters void setSize( int size ); //! Set string from given char pointer /*! @param prefix_cut_off says length of the prefix that is going to be omitted and Loading @@ -87,39 +93,31 @@ class String char& operator[]( int i ); /**** * TODO: the operators do not work properly * for example String + const char* * Operators for C strings */ //! Operator = String& operator = ( const String& str ); //! Operator += String& operator=( const char* str ); String& operator+=( const char* str ); String operator+( const char* str ) const; bool operator==( const char* str ) const; bool operator!=( const char* str ) const; //! Operator += String& operator += ( const char str ); //! Operator += /**** * Operators for Strings */ String& operator=( const String& str ); String& operator+=( const String& str ); //! Operator + String operator+( const String& str ) const; //! Operator + String operator + ( const char* str ) const; //! Comparison operator bool operator==( const String& str ) const; //! Comparison operator bool operator!=( const String& str ) const; //! Comparison operator bool operator == ( const char* ) const; //! Comparison operator bool operator != ( const char* ) const; /**** * Operators for single characters */ String& operator=( char str ); String& operator+=( char str ); String operator+( char str ) const; bool operator==( char str ) const; bool operator!=( char str ) const; //! Cast to bool operator operator bool() const; Loading @@ -127,14 +125,15 @@ class String //! Cast to bool with negation operator bool operator!() const; //! Return length of the string int getLength() const; void replace( const String& pattern, const String& replaceWith ); String replace( const String& pattern, const String& replaceWith, int count = 0 ) const; String strip( char strip = ' ' ) const; //! Split the string into list of strings w.r.t. given separator. int split( Containers::List< String >& list, const char separator = ' ' ) const; //! Write to a binary file bool save( File& file ) const; Loading @@ -147,12 +146,11 @@ class String //! Read one line from given stream. bool getLine( std::istream& stream ); //! Parse the string into list of strings w.r.t. given separator. int parse( Containers::List< String >& list, const char separator = ' ' ) const; friend std::ostream& operator<<( std::ostream& stream, const String& str ); }; String operator+( char string1, const String& string2 ); String operator+( const char* string1, const String& string2 ); std::ostream& operator<<( std::ostream& stream, const String& str ); Loading
src/UnitTests/StringTest.cpp +185 −44 File changed.Preview size limit exceeded, changes collapsed. Show changes