FIX: UrlEncode Function Error

JIRA: STUDIO-10247
Change-Id: I2df384661c6d445bd3fe1d690928c101543ae2bc
This commit is contained in:
zorro.zhang 2025-02-10 21:52:39 +08:00 committed by lane.wei
parent c486b42e9c
commit 07475fab75
1 changed files with 12 additions and 13 deletions

View File

@ -998,22 +998,21 @@ unsigned char FromHex(unsigned char x)
return y; return y;
} }
std::string UrlEncode(const std::string &str) std::string UrlEncode( const std::string &input )
{ {
std::string strTemp = ""; std::ostringstream escaped;
size_t length = str.length(); escaped.fill('0');
for (size_t i = 0; i < length; i++) { escaped << std::hex;
if (isalnum((unsigned char) str[i]) || (str[i] == '-') || (str[i] == '_') || (str[i] == '.') || (str[i] == '~')) for (char c : input) {
strTemp += str[i]; // 如果字符是字母、数字、'-'、'.'、'_' 或 '~',则直接添加到结果中
else if (str[i] == ' ') if (std::isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') {
strTemp += "+"; escaped << c;
else { } else {
strTemp += '%'; // 对于其他字符,将其转换为 %XX 的形式
strTemp += ToHex((unsigned char) str[i] >> 4); escaped << '%' << std::setw(2) << static_cast<int>(static_cast<unsigned char>(c));
strTemp += ToHex((unsigned char) str[i] % 16);
} }
} }
return strTemp; return escaped.str();
} }
std::string UrlDecode(const std::string &str) std::string UrlDecode(const std::string &str)