Skip to content

Commit 720085e

Browse files
Replace stringstream with string operations in url_encode_path
- Use std::string with reserve instead of stringstream for better performance - Direct character lookup table for hex encoding instead of stream manipulators - Remove unnecessary iomanip include - More aligned with CasparCG's lean coding style
1 parent 7e4f516 commit 720085e

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/protocol/util/http_request.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <common/except.h>
44

55
#include <boost/asio.hpp>
6-
#include <iomanip>
76
#include <sstream>
87
#include <string>
98

@@ -105,23 +104,25 @@ HTTPResponse request(const std::string& host, const std::string& port, const std
105104
// Only unreserved characters (A-Za-z0-9-_.~) are not encoded.
106105
std::string url_encode_path(const std::string& path)
107106
{
108-
std::stringstream result;
109-
result.fill('0');
110-
result << std::hex;
107+
std::string result;
108+
result.reserve(path.size() * 2); // Reserve space to avoid reallocations
111109

112110
for (auto c : path) {
113111
// Treat both forward slash and backslash as path separators
114112
// Always output forward slash for HTTP URLs
115113
if (c == '/' || c == '\\') {
116-
result << '/';
114+
result += '/';
117115
} else if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
118-
result << c;
116+
result += c;
119117
} else {
120-
result << std::uppercase << '%' << std::setw(2) << int((unsigned char)c) << std::nouppercase;
118+
// Encode special character as %XX
119+
result += '%';
120+
result += "0123456789ABCDEF"[(unsigned char)c >> 4];
121+
result += "0123456789ABCDEF"[(unsigned char)c & 0x0F];
121122
}
122123
}
123124

124-
return result.str();
125+
return result;
125126
}
126127

127128
}} // namespace caspar::http

0 commit comments

Comments
 (0)