mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-13 21:36:00 +03:00
Perform Trim, TrimLeft & TrimRight transformations in-place
This commit is contained in:
parent
74d150c068
commit
2915ee60e2
@ -15,36 +15,43 @@
|
||||
|
||||
#include "trim.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
namespace modsecurity::actions::transformations {
|
||||
|
||||
|
||||
std::string *Trim::ltrim(std::string *s) {
|
||||
s->erase(
|
||||
s->begin(),
|
||||
std::find_if(s->begin(), s->end(), [](unsigned char c) {
|
||||
bool Trim::ltrim(std::string &s) {
|
||||
auto it = std::find_if(s.begin(), s.end(), [](unsigned char c) {
|
||||
return !std::isspace(c);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
return s;
|
||||
const bool changed = it != s.begin();
|
||||
|
||||
s.erase(s.begin(), it);
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
|
||||
std::string *Trim::rtrim(std::string *s) {
|
||||
s->erase(
|
||||
std::find_if(s->rbegin(), s->rend(), [](unsigned char c) {
|
||||
bool Trim::rtrim(std::string &s) {
|
||||
auto it = std::find_if(s.rbegin(), s.rend(), [](unsigned char c) {
|
||||
return !std::isspace(c);
|
||||
}).base(),
|
||||
s->end()
|
||||
);
|
||||
}).base();
|
||||
|
||||
return s;
|
||||
const bool changed = it != s.end();
|
||||
|
||||
s.erase(it, s.end());
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
|
||||
std::string *Trim::trim(std::string *s) {
|
||||
return ltrim(rtrim(s));
|
||||
bool Trim::trim(std::string &s) {
|
||||
bool changed = false;
|
||||
changed |= rtrim(s);
|
||||
changed |= ltrim(s);
|
||||
return changed;
|
||||
}
|
||||
|
||||
|
||||
@ -55,11 +62,7 @@ Trim::Trim(const std::string &action)
|
||||
|
||||
|
||||
bool Trim::transform(std::string &value, const Transaction *trans) const {
|
||||
std::string ret(value);
|
||||
this->trim(&ret);
|
||||
const auto changed = ret != value;
|
||||
value = ret;
|
||||
return changed;
|
||||
return trim(value);
|
||||
}
|
||||
|
||||
|
||||
|
@ -26,9 +26,11 @@ class Trim : public Transformation {
|
||||
|
||||
bool transform(std::string &value, const Transaction *trans) const override;
|
||||
|
||||
static std::string *ltrim(std::string *s);
|
||||
static std::string *rtrim(std::string *s);
|
||||
static std::string *trim(std::string *s);
|
||||
protected:
|
||||
|
||||
static bool ltrim(std::string &s);
|
||||
static bool rtrim(std::string &s);
|
||||
static bool trim(std::string &s);
|
||||
};
|
||||
|
||||
} // namespace modsecurity::actions::transformations
|
||||
|
@ -26,11 +26,7 @@ TrimLeft::TrimLeft(const std::string &action)
|
||||
}
|
||||
|
||||
bool TrimLeft::transform(std::string &value, const Transaction *trans) const {
|
||||
std::string ret(value);
|
||||
this->ltrim(&ret);
|
||||
const auto changed = ret != value;
|
||||
value = ret;
|
||||
return changed;
|
||||
return ltrim(value);
|
||||
}
|
||||
|
||||
|
||||
|
@ -25,11 +25,7 @@ TrimRight::TrimRight(const std::string &action)
|
||||
}
|
||||
|
||||
bool TrimRight::transform(std::string &value, const Transaction *trans) const {
|
||||
std::string ret(value);
|
||||
this->rtrim(&ret);
|
||||
const auto changed = ret != value;
|
||||
value = ret;
|
||||
return changed;
|
||||
return rtrim(value);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user