mirror of https://github.com/qt/qtgrpc.git
tools [utils]: implement trim as part of ltrim & rtrim
This is the only logical solution for this code... As a drive-by make asciiSpacing constexpr and adjust capitalization. Change-Id: I2cda5006a9eec94b80701d71e5c4000c3c852bf3 Pick-to: 6.9 6.8 Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
This commit is contained in:
parent
cb5cc69cb3
commit
0e6a9f6890
|
@ -12,7 +12,7 @@
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
const std::string_view asciiSpacing = " \t\n\r\f\v";
|
constexpr std::string_view AsciiSpacing = " \t\n\r\f\v";
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace qtprotoccommon::utils {
|
namespace qtprotoccommon::utils {
|
||||||
|
@ -129,30 +129,21 @@ std::string deCapitalizeAsciiName(std::string_view name)
|
||||||
|
|
||||||
std::string &rtrim(std::string &s)
|
std::string &rtrim(std::string &s)
|
||||||
{
|
{
|
||||||
const size_t cut = s.find_last_not_of(asciiSpacing);
|
const size_t cut = s.find_last_not_of(AsciiSpacing);
|
||||||
s.erase(cut != std::string::npos ? cut + 1 : 0);
|
s.erase(cut != std::string::npos ? cut + 1 : 0);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string <rim(std::string &s)
|
std::string <rim(std::string &s)
|
||||||
{
|
{
|
||||||
const size_t cut = s.find_first_not_of(asciiSpacing);
|
const size_t cut = s.find_first_not_of(AsciiSpacing);
|
||||||
s.erase(0, cut == std::string::npos ? s.size() : cut);
|
s.erase(0, cut == std::string::npos ? s.size() : cut);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string &trim(std::string &s)
|
std::string &trim(std::string &s)
|
||||||
{
|
{
|
||||||
const size_t lastKept = s.find_last_not_of(asciiSpacing);
|
return ltrim(rtrim(s));
|
||||||
if (lastKept == std::string::npos) { // true, in particular, if empty
|
|
||||||
s.erase(0);
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
const size_t firstKept = s.find_first_not_of(asciiSpacing);
|
|
||||||
assert(firstKept != std::string::npos);
|
|
||||||
assert(firstKept <= lastKept);
|
|
||||||
s = s.substr(firstKept, lastKept + 1 - firstKept);
|
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HeaderComparator::operator()(const std::string &lhs, const std::string &rhs) const
|
bool HeaderComparator::operator()(const std::string &lhs, const std::string &rhs) const
|
||||||
|
|
Loading…
Reference in New Issue