Skip to content

Commit 1505025

Browse files
committed
Perform RemoveNulls & RemoveWhitespace transformations in-place
- Refactored to share implementation.
1 parent 1236d9a commit 1505025

File tree

3 files changed

+26
-37
lines changed

3 files changed

+26
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include "transformation.h"
2020

21+
#include <algorithm>
22+
2123
namespace modsecurity::actions::transformations {
2224

2325
class RemoveNulls : public Transformation {
@@ -26,6 +28,18 @@ class RemoveNulls : public Transformation {
2628
: Transformation(action) { }
2729

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

3145
} // 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)