Perform CompressWhitespace transformation in-place

This commit is contained in:
Eduardo Arias 2024-08-19 07:14:11 -07:00
parent 13203ae5e7
commit 1236d9a7cd

View File

@ -25,29 +25,27 @@ CompressWhitespace::CompressWhitespace(const std::string &action)
} }
bool CompressWhitespace::transform(std::string &value, const Transaction *trans) const { bool CompressWhitespace::transform(std::string &value, const Transaction *trans) const {
bool inWhiteSpace = false;
std::string a; auto d = value.data();
int inWhiteSpace = 0;
int i = 0;
while (i < value.size()) { for(const auto c : value) {
if (isspace(value[i])) { if (isspace(c)) {
if (inWhiteSpace) { if (inWhiteSpace) {
i++;
continue; continue;
} else { } else {
inWhiteSpace = 1; inWhiteSpace = true;
a.append(" ", 1); *d++ = ' ';
} }
} else { } else {
inWhiteSpace = 0; inWhiteSpace = false;
a.append(&value.at(i), 1); *d++ = c;
} }
i++;
} }
const auto changed = a != value; const auto new_len = d - value.c_str();
value = a; const auto changed = new_len != value.length();
value.resize(new_len);
return changed; return changed;
} }