From bb07de9ad7e574edc24496c184730ca15fffe94e Mon Sep 17 00:00:00 2001 From: Eduardo Arias Date: Sun, 28 Apr 2024 21:45:34 -0300 Subject: [PATCH] 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 --- src/utils/string.cc | 29 ----------------------------- src/utils/string.h | 27 ++++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/src/utils/string.cc b/src/utils/string.cc index 276fd31f..195d5a1b 100644 --- a/src/utils/string.cc +++ b/src/utils/string.cc @@ -20,12 +20,9 @@ #include #include -#include #include #include -#include #include -#include #include #include @@ -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 ssplit(std::string str, char delimiter) { std::vector internal; std::stringstream ss(str); // Turn the string into a stream. diff --git a/src/utils/string.h b/src/utils/string.h index 1cd63296..54136e49 100644 --- a/src/utils/string.h +++ b/src/utils/string.h @@ -14,9 +14,10 @@ */ #include -#include #include #include +#include +#include #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 ssplit(std::string str, char delimiter); std::pair ssplit_pair(const std::string& str, char delimiter); std::vector 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 +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