File tree Expand file tree Collapse file tree 3 files changed +26
-24
lines changed
src/actions/transformations Expand file tree Collapse file tree 3 files changed +26
-24
lines changed Original file line number Diff line number Diff line change 1616
1717#include " lower_case.h"
1818
19- #include < locale >
19+ #include < cctype >
2020
2121
2222namespace 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
Original file line number Diff line number Diff line change 1818
1919#include " transformation.h"
2020
21+ #include < algorithm>
22+
2123namespace modsecurity ::actions::transformations {
2224
2325class 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
Original file line number Diff line number Diff line change 1616
1717#include " upper_case.h"
1818
19- #include < locale>
19+ #include < cctype>
20+
21+ #include " lower_case.h"
22+
2023
2124namespace 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
You can’t perform that action at this time.
0 commit comments