From 3ff23e25c458a3d335c2c2b154eb29b86fddb675 Mon Sep 17 00:00:00 2001 From: Tomas Oberhuber <tomas.oberhuber@fjfi.cvut.cz> Date: Fri, 14 Dec 2018 15:49:03 +0100 Subject: [PATCH] Fixed String::split method and added skipEmpty flag. --- src/TNL/String.cpp | 26 ++++++++++++++------------ src/TNL/String.h | 2 +- src/UnitTests/StringTest.cpp | 12 ++++++++++-- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/TNL/String.cpp b/src/TNL/String.cpp index 817c62cca3..92663a4d8a 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 a580cc4ae0..44f42423fe 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 511bed7a80..f6ee1a5722 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 } + -- GitLab