Perform Trim, TrimLeft & TrimRight transformations in-place

This commit is contained in:
Eduardo Arias 2024-08-19 07:45:16 -07:00
parent 74d150c068
commit 2915ee60e2
4 changed files with 31 additions and 34 deletions

View File

@ -15,36 +15,43 @@
#include "trim.h" #include "trim.h"
#include <algorithm>
namespace modsecurity::actions::transformations { namespace modsecurity::actions::transformations {
std::string *Trim::ltrim(std::string *s) { bool Trim::ltrim(std::string &s) {
s->erase( auto it = std::find_if(s.begin(), s.end(), [](unsigned char c) {
s->begin(),
std::find_if(s->begin(), s->end(), [](unsigned char c) {
return !std::isspace(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) { bool Trim::rtrim(std::string &s) {
s->erase( auto it = std::find_if(s.rbegin(), s.rend(), [](unsigned char c) {
std::find_if(s->rbegin(), s->rend(), [](unsigned char c) {
return !std::isspace(c); return !std::isspace(c);
}).base(), }).base();
s->end()
);
return s; const bool changed = it != s.end();
s.erase(it, s.end());
return changed;
} }
std::string *Trim::trim(std::string *s) { bool Trim::trim(std::string &s) {
return ltrim(rtrim(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 { bool Trim::transform(std::string &value, const Transaction *trans) const {
std::string ret(value); return trim(value);
this->trim(&ret);
const auto changed = ret != value;
value = ret;
return changed;
} }

View File

@ -26,9 +26,11 @@ class Trim : public Transformation {
bool transform(std::string &value, const Transaction *trans) const override; bool transform(std::string &value, const Transaction *trans) const override;
static std::string *ltrim(std::string *s); protected:
static std::string *rtrim(std::string *s);
static std::string *trim(std::string *s); static bool ltrim(std::string &s);
static bool rtrim(std::string &s);
static bool trim(std::string &s);
}; };
} // namespace modsecurity::actions::transformations } // namespace modsecurity::actions::transformations

View File

@ -26,11 +26,7 @@ TrimLeft::TrimLeft(const std::string &action)
} }
bool TrimLeft::transform(std::string &value, const Transaction *trans) const { bool TrimLeft::transform(std::string &value, const Transaction *trans) const {
std::string ret(value); return ltrim(value);
this->ltrim(&ret);
const auto changed = ret != value;
value = ret;
return changed;
} }

View File

@ -25,11 +25,7 @@ TrimRight::TrimRight(const std::string &action)
} }
bool TrimRight::transform(std::string &value, const Transaction *trans) const { bool TrimRight::transform(std::string &value, const Transaction *trans) const {
std::string ret(value); return rtrim(value);
this->rtrim(&ret);
const auto changed = ret != value;
value = ret;
return changed;
} }