优化url编解码

This commit is contained in:
xiongziliang 2020-03-12 18:14:47 +08:00
parent e4e5400641
commit 119d90bc58

View File

@ -69,24 +69,19 @@ char StrToBin(const char *str)
}
string strCoding::UrlEncode(const string &str) {
string dd;
size_t len = str.size();
for (size_t i = 0; i < len; i++) {
if (isalnum((uint8_t)str[i])) {
char tempbuff[2];
sprintf(tempbuff, "%c", str[i]);
dd.append(tempbuff);
}
else if (isspace((uint8_t)str[i])) {
dd.append("+");
}
else {
string out;
size_t len = str.size();
for (size_t i = 0; i < len; ++i) {
char ch = str[i];
if (isalnum((uint8_t)ch)) {
out.push_back(ch);
}else {
char tempbuff[4];
sprintf(tempbuff, "%%%X%X", (uint8_t)str[i] >> 4,(uint8_t)str[i] % 16);
dd.append(tempbuff);
out.append(tempbuff);
}
}
return dd;
return out;
}
string strCoding::UrlDecode(const string &str) {
string output = "";
@ -94,16 +89,18 @@ string strCoding::UrlDecode(const string &str) {
int i = 0, len = str.length();
while (i < len) {
if (str[i] == '%') {
if(i > len - 3){
//防止内存溢出
break;
}
tmp[0] = str[i + 1];
tmp[1] = str[i + 2];
output += StrToBin(tmp);
i = i + 3;
}
else if (str[i] == '+') {
} else if (str[i] == '+') {
output += ' ';
i++;
}
else {
} else {
output += str[i];
i++;
}