Fixed replace(inPlace) function

and added tests (should have done that in the first place - I'll never learn!)
This commit is contained in:
Phil Nash 2014-12-19 18:16:19 +00:00
parent db0421e840
commit 458b3ae257
4 changed files with 50 additions and 15 deletions

View File

@ -71,6 +71,7 @@ namespace Catch {
void toLowerInPlace( std::string& s ); void toLowerInPlace( std::string& s );
std::string toLower( std::string const& s ); std::string toLower( std::string const& s );
std::string trim( std::string const& str ); std::string trim( std::string const& str );
bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis );
struct pluralise { struct pluralise {
pluralise( std::size_t count, std::string const& label ); pluralise( std::size_t count, std::string const& label );

View File

@ -36,7 +36,21 @@ namespace Catch {
return start != std::string::npos ? str.substr( start, 1+end-start ) : ""; return start != std::string::npos ? str.substr( start, 1+end-start ) : "";
} }
bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) {
bool replaced = false;
std::size_t i = str.find( replaceThis );
while( i != std::string::npos ) {
replaced = true;
str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() );
if( i < str.size()-withThis.size() )
i = str.find( replaceThis, i+withThis.size() );
else
i = std::string::npos;
}
return replaced;
}
pluralise::pluralise( std::size_t count, std::string const& label ) pluralise::pluralise( std::size_t count, std::string const& label )
: m_count( count ), : m_count( count ),
m_label( label ) m_label( label )

View File

@ -21,22 +21,14 @@ namespace Catch {
: StreamingReporterBase( _config ) : StreamingReporterBase( _config )
{} {}
static bool replace( std::string& str, std::string const& replaceThis, std::string const& withThis ) {
std::size_t i = str.find( replaceThis );
if( i != std::string::npos ) {
str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() );
return true;
}
return false;
}
static std::string escape( std::string const& str ) { static std::string escape( std::string const& str ) {
std::string escaped = str; std::string escaped = str;
while( replace( escaped, "\'", "|\'" ) || replaceInPlace( escaped, "\'", "|\'" );
replace( escaped, "\n", "|n" ) || replaceInPlace( escaped, "\n", "|n" );
replace( escaped, "\r", "|r" ) || replaceInPlace( escaped, "\r", "|r" );
replace( escaped, "|", "||" ) || replaceInPlace( escaped, "|", "||" );
replace( escaped, "[", "|[" ) || replaceInPlace( escaped, "[", "|[" );
replace( escaped, "]", "|]" ) ); replaceInPlace( escaped, "]", "|]" );
return escaped; return escaped;
} }
virtual ~TeamCityReporter(); virtual ~TeamCityReporter();

View File

@ -348,6 +348,34 @@ private:
std::vector<ColourIndex> colours; std::vector<ColourIndex> colours;
}; };
TEST_CASE( "replaceInPlace", "" ) {
std::string letters = "abcdefcg";
SECTION( "replace single char" ) {
CHECK( replaceInPlace( letters, "b", "z" ) );
CHECK( letters == "azcdefcg" );
}
SECTION( "replace two chars" ) {
CHECK( replaceInPlace( letters, "c", "z" ) );
CHECK( letters == "abzdefzg" );
}
SECTION( "replace first char" ) {
CHECK( replaceInPlace( letters, "a", "z" ) );
CHECK( letters == "zbcdefcg" );
}
SECTION( "replace last char" ) {
CHECK( replaceInPlace( letters, "g", "z" ) );
CHECK( letters == "abcdefcz" );
}
SECTION( "replace all chars" ) {
CHECK( replaceInPlace( letters, letters, "replaced" ) );
CHECK( letters == "replaced" );
}
SECTION( "replace no chars" ) {
CHECK_FALSE( replaceInPlace( letters, "x", "z" ) );
CHECK( letters == letters );
}
}
// !TBD: This will be folded into Text class // !TBD: This will be folded into Text class
TEST_CASE( "Strings can be rendered with colour", "[.colour]" ) { TEST_CASE( "Strings can be rendered with colour", "[.colour]" ) {