mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-01 12:55:40 +02:00
Add trim for StringRef
This commit is contained in:
@@ -53,6 +53,18 @@ namespace Catch {
|
||||
return start != std::string::npos ? str.substr( start, 1+end-start ) : std::string();
|
||||
}
|
||||
|
||||
StringRef trim(StringRef ref) {
|
||||
const auto is_ws = [](char c) {
|
||||
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
|
||||
};
|
||||
size_t real_begin = 0;
|
||||
while (real_begin < ref.size() && is_ws(ref[real_begin])) { ++real_begin; }
|
||||
size_t real_end = ref.size();
|
||||
while (real_end > real_begin && is_ws(ref[real_end - 1])) { --real_end; }
|
||||
|
||||
return ref.substr(real_begin, real_end - real_begin);
|
||||
}
|
||||
|
||||
bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) {
|
||||
bool replaced = false;
|
||||
std::size_t i = str.find( replaceThis );
|
||||
|
@@ -22,7 +22,10 @@ namespace Catch {
|
||||
bool contains( std::string const& s, std::string const& infix );
|
||||
void toLowerInPlace( std::string& s );
|
||||
std::string toLower( std::string const& s );
|
||||
//! Returns a new string without whitespace at the start/end
|
||||
std::string trim( std::string const& str );
|
||||
//! Returns a substring of the original ref without whitespace. Beware lifetimes!
|
||||
StringRef trim(StringRef ref);
|
||||
|
||||
// !!! Be aware, returns refs into original string - make sure original string outlives them
|
||||
std::vector<StringRef> splitStringRef( StringRef str, char delimiter );
|
||||
|
Reference in New Issue
Block a user