Commit a6719f47 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Fixed bug in parseObjectType function and added tests

The original algorithm used space as one of the separators between types
in template parameter lists, so as a result "A< short int >" was parsed
into a list of ("A", "short", "int").
parent 929cf233
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -71,6 +71,10 @@ template< class T > class List

      const List& operator = ( const List& lst );

      bool operator == ( const List& lst ) const;

      bool operator != ( const List& lst ) const;

      //! Append new data element
      bool Append( const T& data );

+17 −0
Original line number Diff line number Diff line
@@ -107,6 +107,23 @@ const List< T >& List< T >::operator = ( const List& lst )
   return( *this );
}

template< typename T >
bool List< T >::operator == ( const List& lst ) const
{
   if( this->getSize() != lst.getSize() )
      return false;
   for( int i = 0; i < this->getSize(); i++ )
      if( (*this)[ i ] != lst[ i ] )
         return false;
   return true;
}

template< typename T >
bool List< T >::operator != ( const List& lst ) const
{
   return ! operator==( lst );
}

template< typename T >
bool List< T >::Append( const T& data )
{
+3 −4
Original line number Diff line number Diff line
@@ -189,7 +189,7 @@ bool parseObjectType( const String& objectType,

   /****
    * Now, we will extract the parameters.
    * Each parameter can be template, so we must compute and pair
    * Each parameter can be template, so we must count and pair
    * '<' with '>'.
    */
   int templateBrackets( 0 );
@@ -201,13 +201,12 @@ bool parseObjectType( const String& objectType,
         templateBrackets ++;
      if( ! templateBrackets )
      {
         if( objectType[ i ] == ' ' ||
             objectType[ i ] == ',' ||
         if( objectType[ i ] == ',' ||
             objectType[ i ] == '>' )
         {
            if( buffer != "" )
            {
               if( ! parsedObjectType. Append( buffer ) )
               if( ! parsedObjectType. Append( buffer.strip( ' ' ) ) )
                  return false;
               buffer. setString( "" );
            }
+17 −0
Original line number Diff line number Diff line
@@ -269,6 +269,23 @@ replace( const String& pattern,
   this->string = newString;
}

String
String::strip( char strip ) const
{
   int prefix_cut_off = 0;
   int sufix_cut_off = 0;

   while( prefix_cut_off < getLength() && (*this)[ prefix_cut_off ] == strip )
      prefix_cut_off++;

   while( sufix_cut_off < getLength() && (*this)[ getLength() - 1 - sufix_cut_off ] == strip )
      sufix_cut_off++;

   if( prefix_cut_off + sufix_cut_off < getLength() )
      return String( getString(), prefix_cut_off, sufix_cut_off );
   return "";
}


const char* String :: getString() const
{
+2 −0
Original line number Diff line number Diff line
@@ -128,6 +128,8 @@ class String
   void replace( const String& pattern,
                 const String& replaceWith );

   String strip( char strip = ' ' ) const;

   // TODO: remove
   //! Write to a binary file
   bool save( std::ostream& file ) const;
Loading