mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-13 21:36:00 +03:00
Perform LowerCase & UpperCase transformations in-place
- Refactored to share implementation and reduce code duplication.
This commit is contained in:
parent
fd8a979463
commit
4670710376
@ -16,7 +16,7 @@
|
||||
|
||||
#include "lower_case.h"
|
||||
|
||||
#include <locale>
|
||||
#include <cctype>
|
||||
|
||||
|
||||
namespace modsecurity::actions::transformations {
|
||||
@ -26,17 +26,9 @@ LowerCase::LowerCase(const std::string &a)
|
||||
: Transformation(a) {
|
||||
}
|
||||
|
||||
bool LowerCase::transform(std::string &val, const Transaction *trans) const {
|
||||
std::locale loc;
|
||||
std::string value(val);
|
||||
|
||||
for (std::string::size_type i=0; i < value.length(); ++i) {
|
||||
value[i] = std::tolower(value[i], loc);
|
||||
}
|
||||
|
||||
const auto changed = val != value;
|
||||
val = value;
|
||||
return changed;
|
||||
bool LowerCase::transform(std::string &value, const Transaction *trans) const {
|
||||
return convert(value, [](auto c) {
|
||||
return std::tolower(c); });
|
||||
}
|
||||
|
||||
|
||||
|
@ -18,6 +18,8 @@
|
||||
|
||||
#include "transformation.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace modsecurity::actions::transformations {
|
||||
|
||||
class LowerCase : public Transformation {
|
||||
@ -25,6 +27,19 @@ class LowerCase : public Transformation {
|
||||
explicit LowerCase(const std::string &action);
|
||||
|
||||
bool transform(std::string &value, const Transaction *trans) const override;
|
||||
|
||||
template<typename Operation>
|
||||
static bool convert(std::string &val, Operation op) {
|
||||
bool changed = false;
|
||||
|
||||
std::transform(val.begin(), val.end(), val.data(),
|
||||
[&](auto c) {
|
||||
const auto nc = op(c);
|
||||
if(nc != c) changed = true;
|
||||
return nc; });
|
||||
|
||||
return changed;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace modsecurity::actions::transformations
|
||||
|
@ -16,7 +16,10 @@
|
||||
|
||||
#include "upper_case.h"
|
||||
|
||||
#include <locale>
|
||||
#include <cctype>
|
||||
|
||||
#include "lower_case.h"
|
||||
|
||||
|
||||
namespace modsecurity::actions::transformations {
|
||||
|
||||
@ -25,17 +28,9 @@ UpperCase::UpperCase(const std::string &a)
|
||||
: Transformation(a) {
|
||||
}
|
||||
|
||||
bool UpperCase::transform(std::string &val, const Transaction *trans) const {
|
||||
std::string value(val);
|
||||
std::locale loc;
|
||||
|
||||
for (std::string::size_type i=0; i < value.length(); ++i) {
|
||||
value[i] = std::toupper(value[i], loc);
|
||||
}
|
||||
|
||||
const auto changed = val != value;
|
||||
val = value;
|
||||
return changed;
|
||||
bool UpperCase::transform(std::string &value, const Transaction *trans) const {
|
||||
return LowerCase::convert(value, [](auto c)
|
||||
{ return std::toupper(c); });
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user