Skip to content

Commit 77adb57

Browse files
committed
Avoid std::string copy in ssplit argument
- Other minor changes reported by sonarcloud
1 parent cc0f893 commit 77adb57

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/request_body_processor/multipart.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ int Multipart::process_part_header(std::string *error, int offset) {
855855
}
856856

857857
new_value = std::string(data);
858-
utils::string::chomp(&new_value);
858+
utils::string::chomp(new_value);
859859

860860
/* update the header value in the table */
861861
header_value = m_mpp->m_headers.at(
@@ -924,7 +924,7 @@ int Multipart::process_part_header(std::string *error, int offset) {
924924
i++;
925925
}
926926
header_value = std::string(data);
927-
utils::string::chomp(&header_value);
927+
utils::string::chomp(header_value);
928928

929929
/* error if the name already exists */
930930
if (m_mpp->m_headers.count(header_name) > 0) {

src/utils/string.h

+16-16
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ const char HEX2DEC[256] = {
5959
};
6060

6161

62-
inline std::string ascTime(time_t *t) {
62+
inline std::string ascTime(const time_t *t) {
6363
std::string ts = std::ctime(t);
6464
ts.pop_back();
6565
return ts;
6666
}
6767

6868

6969
inline std::string dash_if_empty(const std::string *str) {
70-
if (str == NULL || str->empty()) {
70+
if (str == nullptr || str->empty()) {
7171
return "-";
7272
}
7373

@@ -107,7 +107,7 @@ inline std::string toHexIfNeeded(const std::string &str, bool escape_spec = fals
107107
}
108108

109109

110-
inline std::vector<std::string> ssplit(std::string str, char delimiter) {
110+
inline std::vector<std::string> ssplit(const std::string &str, char delimiter) {
111111
std::vector<std::string> internal;
112112
std::stringstream ss(str); // Turn the string into a stream.
113113
std::string tok;
@@ -134,21 +134,21 @@ inline std::pair<std::string, std::string> ssplit_pair(const std::string& str, c
134134
}
135135

136136

137-
inline std::vector<std::string> split(std::string str, char delimiter) {
137+
inline std::vector<std::string> split(const std::string &str, char delimiter) {
138138
std::vector<std::string> internal = ssplit(str, delimiter);
139139

140-
if (internal.size() == 0) {
140+
if (internal.empty()) {
141141
internal.push_back(str);
142142
}
143143

144144
return internal;
145145
}
146146

147147

148-
inline void chomp(std::string *str) {
149-
std::string::size_type pos = str->find_last_not_of("\n\r");
148+
inline void chomp(std::string &str) {
149+
std::string::size_type pos = str.find_last_not_of("\n\r");
150150
if (pos != std::string::npos) {
151-
str->erase(pos+1, str->length()-pos-1);
151+
str.erase(pos+1, str.length()-pos-1);
152152
}
153153
}
154154

@@ -194,24 +194,24 @@ inline std::string parserSanitizer(std::string a) {
194194
}
195195

196196

197-
inline unsigned char x2c(const unsigned char *what) {
197+
/**
198+
* Converts a single hexadecimal digit into a decimal value.
199+
*/
200+
inline unsigned char xsingle2c(const unsigned char *what) {
198201
unsigned char digit;
199202

200203
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
201-
digit *= 16;
202-
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
203204

204205
return digit;
205206
}
206207

207208

208-
/**
209-
* Converts a single hexadecimal digit into a decimal value.
210-
*/
211-
inline unsigned char xsingle2c(const unsigned char *what) {
209+
inline unsigned char x2c(const unsigned char *what) {
212210
unsigned char digit;
213211

214-
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
212+
digit = xsingle2c(what);
213+
digit *= 16;
214+
digit += xsingle2c(what+1);
215215

216216
return digit;
217217
}

0 commit comments

Comments
 (0)