File tree Expand file tree Collapse file tree 4 files changed +31
-34
lines changed
src/actions/transformations Expand file tree Collapse file tree 4 files changed +31
-34
lines changed Original file line number Diff line number Diff line change 15
15
16
16
#include " trim.h"
17
17
18
+ #include < algorithm>
19
+
18
20
19
21
namespace modsecurity ::actions::transformations {
20
22
21
23
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) {
26
26
return !std::isspace (c);
27
- })
28
- );
27
+ });
28
+
29
+ const bool changed = it != s.begin ();
30
+
31
+ s.erase (s.begin (), it);
29
32
30
- return s ;
33
+ return changed ;
31
34
}
32
35
33
36
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) {
37
39
return !std::isspace (c);
38
- }).base (),
39
- s-> end ()
40
- );
40
+ }).base ();
41
+
42
+ const bool changed = it != s. end ( );
41
43
42
- return s;
44
+ s.erase (it, s.end ());
45
+
46
+ return changed;
43
47
}
44
48
45
49
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;
48
55
}
49
56
50
57
@@ -55,11 +62,7 @@ Trim::Trim(const std::string &action)
55
62
56
63
57
64
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);
63
66
}
64
67
65
68
Original file line number Diff line number Diff line change @@ -26,9 +26,11 @@ class Trim : public Transformation {
26
26
27
27
bool transform (std::string &value, const Transaction *trans) const override ;
28
28
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);
32
34
};
33
35
34
36
} // namespace modsecurity::actions::transformations
Original file line number Diff line number Diff line change @@ -26,11 +26,7 @@ TrimLeft::TrimLeft(const std::string &action)
26
26
}
27
27
28
28
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);
34
30
}
35
31
36
32
Original file line number Diff line number Diff line change @@ -25,11 +25,7 @@ TrimRight::TrimRight(const std::string &action)
25
25
}
26
26
27
27
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);
33
29
}
34
30
35
31
You can’t perform that action at this time.
0 commit comments