Skip to content

Commit ea01d37

Browse files
committed
Perform RemoveNulls & RemoveWhitespace transformations in-place
- Refactored to share implementation.
1 parent 98b77b2 commit ea01d37

File tree

3 files changed

+24
-37
lines changed

3 files changed

+24
-37
lines changed

src/actions/transformations/remove_nulls.cc

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,8 @@
1919
namespace modsecurity::actions::transformations {
2020

2121

22-
bool RemoveNulls::transform(std::string &val, const Transaction *trans) const {
23-
size_t i = 0;
24-
std::string transformed_value;
25-
transformed_value.reserve(val.size());
26-
27-
while (i < val.size()) {
28-
if (val.at(i) == '\0') {
29-
// do nothing; continue on to next char in original val
30-
} else {
31-
transformed_value += val.at(i);
32-
}
33-
i++;
34-
}
35-
36-
const auto changed = transformed_value != val;
37-
val = transformed_value;
38-
return changed;
22+
bool RemoveNulls::transform(std::string &value, const Transaction *trans) const {
23+
return remove_if(value, [](const auto c) { return c == '\0'; });
3924
}
4025

4126

src/actions/transformations/remove_nulls.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ class RemoveNulls : public Transformation {
2626
: Transformation(action) { }
2727

2828
bool transform(std::string &value, const Transaction *trans) const override;
29+
30+
template<typename Pred>
31+
static bool remove_if(std::string &val, Pred pred) {
32+
const auto old_size = val.size();
33+
34+
val.erase(
35+
std::remove_if(
36+
val.begin(), val.end(), pred),
37+
val.end());
38+
39+
return val.size() != old_size;
40+
}
2941
};
3042

3143
} // namespace modsecurity::actions::transformations

src/actions/transformations/remove_whitespace.cc

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include "remove_whitespace.h"
1717

18+
#include "remove_nulls.h"
19+
1820
namespace modsecurity::actions::transformations {
1921

2022

@@ -23,30 +25,18 @@ RemoveWhitespace::RemoveWhitespace(const std::string &action)
2325
this->action_kind = 1;
2426
}
2527

26-
bool RemoveWhitespace::transform(std::string &val, const Transaction *trans) const {
27-
std::string transformed_value;
28-
transformed_value.reserve(val.size());
29-
30-
size_t i = 0;
28+
bool RemoveWhitespace::transform(std::string &value, const Transaction *trans) const {
3129
const char nonBreakingSpaces = 0xa0;
3230
const char nonBreakingSpaces2 = 0xc2;
3331

34-
// loop through all the chars
35-
while (i < val.size()) {
32+
auto pred = [](const auto c) {
3633
// remove whitespaces and non breaking spaces (NBSP)
37-
if (std::isspace(static_cast<unsigned char>(val[i]))
38-
|| (val[i] == nonBreakingSpaces)
39-
|| val[i] == nonBreakingSpaces2) {
40-
// don't copy; continue on to next char in original val
41-
} else {
42-
transformed_value += val.at(i);
43-
}
44-
i++;
45-
}
46-
47-
const auto changed = transformed_value != val;
48-
val = transformed_value;
49-
return changed;
34+
return std::isspace(static_cast<unsigned char>(c))
35+
|| c == nonBreakingSpaces
36+
|| c == nonBreakingSpaces2;
37+
};
38+
39+
return RemoveNulls::remove_if(value, pred);
5040
}
5141

5242

0 commit comments

Comments
 (0)