Add trim for StringRef

This commit is contained in:
Martin Hořeňovský 2019-09-07 11:31:00 +02:00
parent b77ab74b72
commit f2c2711bdc
No known key found for this signature in database
GPG key ID: DE48307B8B0D381A
8 changed files with 106 additions and 5 deletions

View file

@ -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 );