Skip to content

Commit f8f3c47

Browse files
committed
Perform Trim, TrimLeft & TrimRight transformations in-place
1 parent 23fcbde commit f8f3c47

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

src/actions/transformations/trim.cc

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,37 @@
1919
namespace modsecurity::actions::transformations {
2020

2121

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) {
22+
bool Trim::ltrim(std::string &s) {
23+
auto it = std::find_if(s.begin(), s.end(), [](unsigned char c) {
2624
return !std::isspace(c);
27-
})
28-
);
25+
});
2926

30-
return s;
27+
const bool changed = it != s.begin();
28+
29+
s.erase(s.begin(), it);
30+
31+
return changed;
3132
}
3233

3334

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

42-
return s;
42+
s.erase(it, s.end());
43+
44+
return changed;
4345
}
4446

4547

46-
std::string *Trim::trim(std::string *s) {
47-
return ltrim(rtrim(s));
48+
bool Trim::trim(std::string &s) {
49+
bool changed = false;
50+
changed |= rtrim(s);
51+
changed |= ltrim(s);
52+
return changed;
4853
}
4954

5055

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

5661

5762
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;
63+
return trim(value);
6364
}
6465

6566

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)