diff --git a/src/TNL/String.cpp b/src/TNL/String.cpp index 817c62cca36af2362d8cde9da295ce5730fecc41..92663a4d8aed06405514f4a9e2ca69013832a92a 100644 --- a/src/TNL/String.cpp +++ b/src/TNL/String.cpp @@ -335,22 +335,24 @@ String::strip( char strip ) const return ""; } -int String::split( Containers::List< String >& list, const char separator ) const +int String::split( Containers::List< String >& list, + const char separator, + bool skipEmpty ) 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 s; + for( int i = 0; i < this->getLength(); i ++ ) { - if( copy[ i ] == 0 ) continue; - String new_string; - new_string.setString( ©.getString()[ i ] ); - i += new_string.getLength(); - list.Append( new_string ); + if( ( *this )[ i ] == separator ) + { + if( ! skipEmpty || s != "" ) + list.Append( s ); + s = ""; + } + else s += ( *this )[ i ]; } + if( ! skipEmpty || s != "" ) + list.Append( s ); return list.getSize(); } diff --git a/src/TNL/String.h b/src/TNL/String.h index a580cc4ae012a3f7c184807de2961bd5d33861f8..44f42423fe207955dab89e05eddf62344191ea19 100644 --- a/src/TNL/String.h +++ b/src/TNL/String.h @@ -253,7 +253,7 @@ class String /// @param list Name of list. /// @param separator Character, which separates substrings in given string. /// Empty character can not be used. - int split( Containers::List< String >& list, const char separator = ' ' ) const; + int split( Containers::List< String >& list, const char separator = ' ', bool skipEmpty = false ) const; ///// /// \brief Function for saving file. diff --git a/src/UnitTests/StringTest.cpp b/src/UnitTests/StringTest.cpp index 511bed7a803b367ed51cf749e2a64796b65fda50..f6ee1a57225d0287da3418a162363ee39e73925e 100644 --- a/src/UnitTests/StringTest.cpp +++ b/src/UnitTests/StringTest.cpp @@ -295,6 +295,15 @@ TEST( StringTest, split ) EXPECT_EQ( list[ 2 ], "C" ); String( "abracadabra" ).split( list, 'a' ); + ASSERT_EQ( list.getSize(), 6 ); + EXPECT_EQ( list[ 0 ], "" ); + EXPECT_EQ( list[ 1 ], "br" ); + EXPECT_EQ( list[ 2 ], "c" ); + EXPECT_EQ( list[ 3 ], "d" ); + EXPECT_EQ( list[ 4 ], "br" ); + EXPECT_EQ( list[ 5 ], "" ); + + String( "abracadabra" ).split( list, 'a', true ); ASSERT_EQ( list.getSize(), 4 ); EXPECT_EQ( list[ 0 ], "br" ); EXPECT_EQ( list[ 1 ], "c" ); @@ -311,7 +320,6 @@ TEST( StringTest, split ) ASSERT_EQ( list.getSize(), 1 ); EXPECT_EQ( list[ 0 ], "abracadabra" ); - // !!!! ma problem s prazdnym stringom !!!! String( "a,,b,c" ).split( list, ',' ); ASSERT_EQ( list.getSize(), 4 ); EXPECT_EQ( list[ 0 ], "a" ); @@ -353,7 +361,6 @@ TEST( StringTest, getLine ) #endif - #include "GtestMissingError.h" int main( int argc, char* argv[] ) { @@ -364,3 +371,4 @@ int main( int argc, char* argv[] ) throw GtestMissingError(); #endif } +