Lift toLower(char) to header

This commit is contained in:
Martin Hořeňovský 2021-11-08 23:58:09 +01:00
parent 141e384c60
commit 7800fe9708
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
2 changed files with 8 additions and 7 deletions

View File

@ -16,12 +16,6 @@
namespace Catch { namespace Catch {
namespace {
char toLowerCh(char c) {
return static_cast<char>( std::tolower( static_cast<unsigned char>(c) ) );
}
}
bool startsWith( std::string const& s, std::string const& prefix ) { bool startsWith( std::string const& s, std::string const& prefix ) {
return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin()); return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin());
} }
@ -38,13 +32,19 @@ namespace Catch {
return s.find( infix ) != std::string::npos; return s.find( infix ) != std::string::npos;
} }
void toLowerInPlace( std::string& s ) { void toLowerInPlace( std::string& s ) {
std::transform( s.begin(), s.end(), s.begin(), toLowerCh ); std::transform( s.begin(), s.end(), s.begin(), []( char c ) {
return toLower( c );
} );
} }
std::string toLower( std::string const& s ) { std::string toLower( std::string const& s ) {
std::string lc = s; std::string lc = s;
toLowerInPlace( lc ); toLowerInPlace( lc );
return lc; return lc;
} }
char toLower(char c) {
return static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
std::string trim( std::string const& str ) { std::string trim( std::string const& str ) {
static char const* whitespaceChars = "\n\r\t "; static char const* whitespaceChars = "\n\r\t ";
std::string::size_type start = str.find_first_not_of( whitespaceChars ); std::string::size_type start = str.find_first_not_of( whitespaceChars );

View File

@ -23,6 +23,7 @@ namespace Catch {
bool contains( std::string const& s, std::string const& infix ); bool contains( std::string const& s, std::string const& infix );
void toLowerInPlace( std::string& s ); void toLowerInPlace( std::string& s );
std::string toLower( std::string const& s ); std::string toLower( std::string const& s );
char toLower( char c );
//! Returns a new string without whitespace at the start/end //! Returns a new string without whitespace at the start/end
std::string trim( std::string const& str ); std::string trim( std::string const& str );
//! Returns a substring of the original ref without whitespace. Beware lifetimes! //! Returns a substring of the original ref without whitespace. Beware lifetimes!