Skip to content

Commit 4670710

Browse files
committed
Perform LowerCase & UpperCase transformations in-place
- Refactored to share implementation and reduce code duplication.
1 parent fd8a979 commit 4670710

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

src/actions/transformations/lower_case.cc

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include "lower_case.h"
1818

19-
#include <locale>
19+
#include <cctype>
2020

2121

2222
namespace modsecurity::actions::transformations {
@@ -26,17 +26,9 @@ LowerCase::LowerCase(const std::string &a)
2626
: Transformation(a) {
2727
}
2828

29-
bool LowerCase::transform(std::string &val, const Transaction *trans) const {
30-
std::locale loc;
31-
std::string value(val);
32-
33-
for (std::string::size_type i=0; i < value.length(); ++i) {
34-
value[i] = std::tolower(value[i], loc);
35-
}
36-
37-
const auto changed = val != value;
38-
val = value;
39-
return changed;
29+
bool LowerCase::transform(std::string &value, const Transaction *trans) const {
30+
return convert(value, [](auto c) {
31+
return std::tolower(c); });
4032
}
4133

4234

src/actions/transformations/lower_case.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,28 @@
1818

1919
#include "transformation.h"
2020

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

2325
class LowerCase : public Transformation {
2426
public:
2527
explicit LowerCase(const std::string &action);
2628

2729
bool transform(std::string &value, const Transaction *trans) const override;
30+
31+
template<typename Operation>
32+
static bool convert(std::string &val, Operation op) {
33+
bool changed = false;
34+
35+
std::transform(val.begin(), val.end(), val.data(),
36+
[&](auto c) {
37+
const auto nc = op(c);
38+
if(nc != c) changed = true;
39+
return nc; });
40+
41+
return changed;
42+
}
2843
};
2944

3045
} // namespace modsecurity::actions::transformations

src/actions/transformations/upper_case.cc

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
#include "upper_case.h"
1818

19-
#include <locale>
19+
#include <cctype>
20+
21+
#include "lower_case.h"
22+
2023

2124
namespace modsecurity::actions::transformations {
2225

@@ -25,17 +28,9 @@ UpperCase::UpperCase(const std::string &a)
2528
: Transformation(a) {
2629
}
2730

28-
bool UpperCase::transform(std::string &val, const Transaction *trans) const {
29-
std::string value(val);
30-
std::locale loc;
31-
32-
for (std::string::size_type i=0; i < value.length(); ++i) {
33-
value[i] = std::toupper(value[i], loc);
34-
}
35-
36-
const auto changed = val != value;
37-
val = value;
38-
return changed;
31+
bool UpperCase::transform(std::string &value, const Transaction *trans) const {
32+
return LowerCase::convert(value, [](auto c)
33+
{ return std::toupper(c); });
3934
}
4035

4136

0 commit comments

Comments
 (0)