Skip to content

Commit bb07de9

Browse files
committed
toupper/tolower is already receiving a copy, so it doesn't need to create a new one to transform it
- Make functions inline to improve performance - Introduced helper method toCaseHelper to remove code duplication
1 parent 7bdc3c8 commit bb07de9

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

src/utils/string.cc

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@
2020
#include <stdint.h>
2121
#include <inttypes.h>
2222

23-
#include <algorithm>
2423
#include <random>
2524
#include <memory>
26-
#include <functional>
2725
#include <string>
28-
#include <iostream>
2926
#include <sstream>
3027
#include <cstring>
3128

@@ -148,32 +145,6 @@ std::string toHexIfNeeded(const std::string &str, bool escape_spec) {
148145
}
149146

150147

151-
std::string tolower(std::string str) {
152-
std::string value;
153-
value.resize(str.length());
154-
155-
std::transform(str.begin(),
156-
str.end(),
157-
value.begin(),
158-
::tolower);
159-
160-
return value;
161-
}
162-
163-
164-
std::string toupper(std::string str) {
165-
std::string value;
166-
value.resize(str.length());
167-
168-
std::transform(str.begin(),
169-
str.end(),
170-
value.begin(),
171-
::toupper);
172-
173-
return value;
174-
}
175-
176-
177148
std::vector<std::string> ssplit(std::string str, char delimiter) {
178149
std::vector<std::string> internal;
179150
std::stringstream ss(str); // Turn the string into a stream.

src/utils/string.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
*/
1515

1616
#include <ctime>
17-
#include <iostream>
1817
#include <string>
1918
#include <vector>
19+
#include <algorithm>
20+
#include <utility>
2021

2122
#ifndef SRC_UTILS_STRING_H_
2223
#define SRC_UTILS_STRING_H_
@@ -62,8 +63,6 @@ std::string limitTo(int amount, const std::string &str);
6263
std::string removeBracketsIfNeeded(std::string a);
6364
std::string string_to_hex(const std::string& input);
6465
std::string toHexIfNeeded(const std::string &str, bool escape_spec = false);
65-
std::string tolower(std::string str);
66-
std::string toupper(std::string str);
6766
std::vector<std::string> ssplit(std::string str, char delimiter);
6867
std::pair<std::string, std::string> ssplit_pair(const std::string& str, char delimiter);
6968
std::vector<std::string> split(std::string str, char delimiter);
@@ -77,6 +76,28 @@ unsigned char x2c(const unsigned char *what);
7776
unsigned char xsingle2c(const unsigned char *what);
7877
unsigned char *c2x(unsigned what, unsigned char *where);
7978

79+
80+
template<typename Operation>
81+
inline std::string toCaseHelper(std::string str, Operation op) {
82+
std::transform(str.begin(),
83+
str.end(),
84+
str.begin(),
85+
op);
86+
87+
return str;
88+
}
89+
90+
91+
inline std::string tolower(std::string str) { // cppcheck-suppress passedByValue ; copied value is used for in-place transformation
92+
return toCaseHelper(str, ::tolower);
93+
}
94+
95+
96+
inline std::string toupper(std::string str) { // cppcheck-suppress passedByValue ; copied value is used for in-place transformation
97+
return toCaseHelper(str, ::toupper);
98+
}
99+
100+
80101
} // namespace string
81102
} // namespace utils
82103
} // namespace modsecurity

0 commit comments

Comments
 (0)