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
This commit is contained in:
Eduardo Arias 2024-04-28 21:45:34 -03:00
parent 7bdc3c825c
commit bb07de9ad7
2 changed files with 24 additions and 32 deletions

View File

@ -20,12 +20,9 @@
#include <stdint.h>
#include <inttypes.h>
#include <algorithm>
#include <random>
#include <memory>
#include <functional>
#include <string>
#include <iostream>
#include <sstream>
#include <cstring>
@ -148,32 +145,6 @@ std::string toHexIfNeeded(const std::string &str, bool escape_spec) {
}
std::string tolower(std::string str) {
std::string value;
value.resize(str.length());
std::transform(str.begin(),
str.end(),
value.begin(),
::tolower);
return value;
}
std::string toupper(std::string str) {
std::string value;
value.resize(str.length());
std::transform(str.begin(),
str.end(),
value.begin(),
::toupper);
return value;
}
std::vector<std::string> ssplit(std::string str, char delimiter) {
std::vector<std::string> internal;
std::stringstream ss(str); // Turn the string into a stream.

View File

@ -14,9 +14,10 @@
*/
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#ifndef SRC_UTILS_STRING_H_
#define SRC_UTILS_STRING_H_
@ -62,8 +63,6 @@ std::string limitTo(int amount, const std::string &str);
std::string removeBracketsIfNeeded(std::string a);
std::string string_to_hex(const std::string& input);
std::string toHexIfNeeded(const std::string &str, bool escape_spec = false);
std::string tolower(std::string str);
std::string toupper(std::string str);
std::vector<std::string> ssplit(std::string str, char delimiter);
std::pair<std::string, std::string> ssplit_pair(const std::string& str, char delimiter);
std::vector<std::string> split(std::string str, char delimiter);
@ -77,6 +76,28 @@ unsigned char x2c(const unsigned char *what);
unsigned char xsingle2c(const unsigned char *what);
unsigned char *c2x(unsigned what, unsigned char *where);
template<typename Operation>
inline std::string toCaseHelper(std::string str, Operation op) {
std::transform(str.begin(),
str.end(),
str.begin(),
op);
return str;
}
inline std::string tolower(std::string str) { // cppcheck-suppress passedByValue ; copied value is used for in-place transformation
return toCaseHelper(str, ::tolower);
}
inline std::string toupper(std::string str) { // cppcheck-suppress passedByValue ; copied value is used for in-place transformation
return toCaseHelper(str, ::toupper);
}
} // namespace string
} // namespace utils
} // namespace modsecurity