Commit 3ff23e25 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Fixed String::split method and added skipEmpty flag.

parent 66995c1c
Loading
Loading
Loading
Loading
+14 −12
Original line number Diff line number Diff line
@@ -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 ++ )
   {
      if( copy[ i ] == 0 ) continue;
      String new_string;
      new_string.setString( &copy.getString()[ i ] );
      i += new_string.getLength();
      list.Append( new_string );
   String s;
   for( int i = 0; i < this->getLength(); i ++ )
   {
      if( ( *this )[ i ] == separator )
      {
         if( ! skipEmpty || s != "" )
            list.Append( s );
         s = "";
      }
      else s += ( *this )[ i ];
   }
   if( ! skipEmpty || s != "" )
      list.Append( s );
   return list.getSize();
}

+1 −1
Original line number Diff line number Diff line
@@ -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.
+10 −2
Original line number Diff line number Diff line
@@ -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
}