mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-01 12:55:40 +02:00
Fixed replace(inPlace) function
and added tests (should have done that in the first place - I'll never learn!)
This commit is contained in:
@@ -71,6 +71,7 @@ namespace Catch {
|
||||
void toLowerInPlace( std::string& s );
|
||||
std::string toLower( std::string const& s );
|
||||
std::string trim( std::string const& str );
|
||||
bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis );
|
||||
|
||||
struct pluralise {
|
||||
pluralise( std::size_t count, std::string const& label );
|
||||
|
@@ -36,7 +36,21 @@ namespace Catch {
|
||||
|
||||
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 )
|
||||
: m_count( count ),
|
||||
m_label( label )
|
||||
|
@@ -21,22 +21,14 @@ namespace Catch {
|
||||
: 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 ) {
|
||||
std::string escaped = str;
|
||||
while( replace( escaped, "\'", "|\'" ) ||
|
||||
replace( escaped, "\n", "|n" ) ||
|
||||
replace( escaped, "\r", "|r" ) ||
|
||||
replace( escaped, "|", "||" ) ||
|
||||
replace( escaped, "[", "|[" ) ||
|
||||
replace( escaped, "]", "|]" ) );
|
||||
replaceInPlace( escaped, "\'", "|\'" );
|
||||
replaceInPlace( escaped, "\n", "|n" );
|
||||
replaceInPlace( escaped, "\r", "|r" );
|
||||
replaceInPlace( escaped, "|", "||" );
|
||||
replaceInPlace( escaped, "[", "|[" );
|
||||
replaceInPlace( escaped, "]", "|]" );
|
||||
return escaped;
|
||||
}
|
||||
virtual ~TeamCityReporter();
|
||||
|
Reference in New Issue
Block a user