Skip to content

Commit

Permalink
[bugfix] fix trim function for some edgecases
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterPetrik committed Jan 10, 2020
1 parent c6eca00 commit 5d64904
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
21 changes: 19 additions & 2 deletions mdal/mdal_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,16 @@ std::string MDAL::ltrim( const std::string &s, const std::string &delimiters )
if ( s.empty() )
return s;

return s.substr( s.find_first_not_of( delimiters ) );
size_t found = s.find_first_not_of( delimiters );

if ( found == std::string::npos )
{
return "";
}
else
{
return s.substr( found );
}
}

// http://www.cplusplus.com/faq/sequences/strings/trim/
Expand All @@ -305,7 +314,15 @@ std::string MDAL::rtrim( const std::string &s, const std::string &delimiters )
if ( s.empty() )
return s;

return s.substr( 0, s.find_last_not_of( delimiters ) + 1 );
size_t found = s.find_last_not_of( delimiters );
if ( found == std::string::npos )
{
return "";
}
else
{
return s.substr( 0, found + 1 );
}
}

MDAL::BBox MDAL::computeExtent( const MDAL::Vertices &vertices )
Expand Down
20 changes: 20 additions & 0 deletions tests/unittests/test_mdal_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ TEST( MdalUtilsTest, SplitChar )
}
}

TEST( MdalUtilsTest, TrimString )
{
std::vector<SplitTestData> tests =
{
SplitTestData( "", {"", "", ""} ),
SplitTestData( " ", {"", "", ""} ),
SplitTestData( " a", {"a", " a", "a"} ),
SplitTestData( "a ", {"a", "a", "a "} ),
SplitTestData( " a ", {"a", " a", "a "} ),
SplitTestData( " a b ", {"a b", " a b", "a b "} ),
SplitTestData( "\na b ", {"a b", "\na b", "a b "} )
};
for ( const auto &test : tests )
{
EXPECT_EQ( test.mExpectedResult[0], MDAL::trim( test.mInput ) );
EXPECT_EQ( test.mExpectedResult[1], MDAL::rtrim( test.mInput ) );
EXPECT_EQ( test.mExpectedResult[2], MDAL::ltrim( test.mInput ) );
}
}

TEST( MdalUtilsTest, TimeParsing )
{
std::vector<std::pair<std::string, double>> tests =
Expand Down

0 comments on commit 5d64904

Please sign in to comment.