Skip to content

Commit 1236d9a

Browse files
committed
Perform CompressWhitespace transformation in-place
1 parent 13203ae commit 1236d9a

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/actions/transformations/compress_whitespace.cc

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,27 @@ CompressWhitespace::CompressWhitespace(const std::string &action)
2525
}
2626

2727
bool CompressWhitespace::transform(std::string &value, const Transaction *trans) const {
28+
bool inWhiteSpace = false;
2829

29-
std::string a;
30-
int inWhiteSpace = 0;
31-
int i = 0;
30+
auto d = value.data();
3231

33-
while (i < value.size()) {
34-
if (isspace(value[i])) {
32+
for(const auto c : value) {
33+
if (isspace(c)) {
3534
if (inWhiteSpace) {
36-
i++;
3735
continue;
3836
} else {
39-
inWhiteSpace = 1;
40-
a.append(" ", 1);
37+
inWhiteSpace = true;
38+
*d++ = ' ';
4139
}
4240
} else {
43-
inWhiteSpace = 0;
44-
a.append(&value.at(i), 1);
41+
inWhiteSpace = false;
42+
*d++ = c;
4543
}
46-
i++;
4744
}
4845

49-
const auto changed = a != value;
50-
value = a;
46+
const auto new_len = d - value.c_str();
47+
const auto changed = new_len != value.length();
48+
value.resize(new_len);
5149
return changed;
5250
}
5351

0 commit comments

Comments
 (0)