Skip to content

Commit 2915ee6

Browse files
committed
Perform Trim, TrimLeft & TrimRight transformations in-place
1 parent 74d150c commit 2915ee6

File tree

4 files changed

+31
-34
lines changed

4 files changed

+31
-34
lines changed

src/actions/transformations/trim.cc

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,43 @@
1515

1616
#include "trim.h"
1717

18+
#include <algorithm>
19+
1820

1921
namespace modsecurity::actions::transformations {
2022

2123

22-
std::string *Trim::ltrim(std::string *s) {
23-
s->erase(
24-
s->begin(),
25-
std::find_if(s->begin(), s->end(), [](unsigned char c) {
24+
bool Trim::ltrim(std::string &s) {
25+
auto it = std::find_if(s.begin(), s.end(), [](unsigned char c) {
2626
return !std::isspace(c);
27-
})
28-
);
27+
});
28+
29+
const bool changed = it != s.begin();
30+
31+
s.erase(s.begin(), it);
2932

30-
return s;
33+
return changed;
3134
}
3235

3336

34-
std::string *Trim::rtrim(std::string *s) {
35-
s->erase(
36-
std::find_if(s->rbegin(), s->rend(), [](unsigned char c) {
37+
bool Trim::rtrim(std::string &s) {
38+
auto it = std::find_if(s.rbegin(), s.rend(), [](unsigned char c) {
3739
return !std::isspace(c);
38-
}).base(),
39-
s->end()
40-
);
40+
}).base();
41+
42+
const bool changed = it != s.end();
4143

42-
return s;
44+
s.erase(it, s.end());
45+
46+
return changed;
4347
}
4448

4549

46-
std::string *Trim::trim(std::string *s) {
47-
return ltrim(rtrim(s));
50+
bool Trim::trim(std::string &s) {
51+
bool changed = false;
52+
changed |= rtrim(s);
53+
changed |= ltrim(s);
54+
return changed;
4855
}
4956

5057

@@ -55,11 +62,7 @@ Trim::Trim(const std::string &action)
5562

5663

5764
bool Trim::transform(std::string &value, const Transaction *trans) const {
58-
std::string ret(value);
59-
this->trim(&ret);
60-
const auto changed = ret != value;
61-
value = ret;
62-
return changed;
65+
return trim(value);
6366
}
6467

6568

src/actions/transformations/trim.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ class Trim : public Transformation {
2626

2727
bool transform(std::string &value, const Transaction *trans) const override;
2828

29-
static std::string *ltrim(std::string *s);
30-
static std::string *rtrim(std::string *s);
31-
static std::string *trim(std::string *s);
29+
protected:
30+
31+
static bool ltrim(std::string &s);
32+
static bool rtrim(std::string &s);
33+
static bool trim(std::string &s);
3234
};
3335

3436
} // namespace modsecurity::actions::transformations

src/actions/transformations/trim_left.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ TrimLeft::TrimLeft(const std::string &action)
2626
}
2727

2828
bool TrimLeft::transform(std::string &value, const Transaction *trans) const {
29-
std::string ret(value);
30-
this->ltrim(&ret);
31-
const auto changed = ret != value;
32-
value = ret;
33-
return changed;
29+
return ltrim(value);
3430
}
3531

3632

src/actions/transformations/trim_right.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ TrimRight::TrimRight(const std::string &action)
2525
}
2626

2727
bool TrimRight::transform(std::string &value, const Transaction *trans) const {
28-
std::string ret(value);
29-
this->rtrim(&ret);
30-
const auto changed = ret != value;
31-
value = ret;
32-
return changed;
28+
return rtrim(value);
3329
}
3430

3531

0 commit comments

Comments
 (0)