-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize StringUtil::url_encode()/url_decode().
- Loading branch information
Showing
2 changed files
with
39 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
limitations under the License. | ||
Authors: Wu Jiaxu ([email protected]) | ||
Xie Han ([email protected]) | ||
*/ | ||
|
||
#include <ctype.h> | ||
|
@@ -22,31 +23,26 @@ | |
#include <algorithm> | ||
#include "StringUtil.h" | ||
|
||
static int __htoi(unsigned char *s) | ||
static int __hex_to_int(const char s[2]) | ||
{ | ||
int value; | ||
int c; | ||
int value = 16; | ||
|
||
c = s[0]; | ||
if (isupper(c)) | ||
c = tolower(c); | ||
if (s[0] <= '9') | ||
value *= s[0] - '0'; | ||
else | ||
value *= toupper(s[0]) - 'A' + 10; | ||
|
||
value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16; | ||
if (s[1] <= '9') | ||
value += s[1] - '0'; | ||
else | ||
value += toupper(s[1]) - 'A' + 10; | ||
|
||
c = s[1]; | ||
if (isupper(c)) | ||
c = tolower(c); | ||
|
||
value += (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10); | ||
return value; | ||
} | ||
|
||
static inline char __itoh(int n) | ||
static inline char __int_to_hex(int n) | ||
{ | ||
if (n > 9) | ||
return n - 10 + 'A'; | ||
|
||
return n + '0'; | ||
return n <= 9 ? n + '0' : n - 10 + 'A'; | ||
} | ||
|
||
static size_t __url_decode(char *str) | ||
|
@@ -58,7 +54,7 @@ static size_t __url_decode(char *str) | |
{ | ||
if (*data == '%' && isxdigit(data[1]) && isxdigit(data[2])) | ||
{ | ||
*dest = __htoi((unsigned char *)data + 1); | ||
*dest = __hex_to_int(data + 1); | ||
data += 2; | ||
} | ||
else if (*data == '+') | ||
|
@@ -76,12 +72,7 @@ static size_t __url_decode(char *str) | |
|
||
void StringUtil::url_decode(std::string& str) | ||
{ | ||
if (str.empty()) | ||
return; | ||
|
||
size_t sz = __url_decode(const_cast<char *>(str.c_str())); | ||
|
||
str.resize(sz); | ||
str.resize(__url_decode(const_cast<char *>(str.c_str()))); | ||
} | ||
|
||
std::string StringUtil::url_encode(const std::string& str) | ||
|
@@ -92,18 +83,22 @@ std::string StringUtil::url_encode(const std::string& str) | |
|
||
while (cur < end) | ||
{ | ||
if (*cur == ' ') | ||
res += '+'; | ||
else if (isalnum(*cur) || *cur == '-' || *cur == '_' || *cur == '.' || | ||
*cur == '!' || *cur == '~' || *cur == '*' || *cur == '\'' || | ||
*cur == '(' || *cur == ')' || *cur == ':' || *cur == '/' || | ||
*cur == '@' || *cur == '?' || *cur == '#' || *cur == '&') | ||
if (isalnum(*cur) || *cur == '-' || *cur == '_' || *cur == '.' || | ||
*cur == '!' || *cur == '~' || *cur == '*' || *cur == '\'' || | ||
*cur == '(' || *cur == ')' || *cur == ':' || *cur == '/' || | ||
*cur == '@' || *cur == '?' || *cur == '#' || *cur == '&') | ||
{ | ||
res += *cur; | ||
} | ||
else if (*cur == ' ') | ||
{ | ||
res += '+'; | ||
} | ||
else | ||
{ | ||
res += '%'; | ||
res += __itoh(((const unsigned char)(*cur)) >> 4); | ||
res += __itoh(((const unsigned char)(*cur)) % 16); | ||
res += __int_to_hex(((const unsigned char)(*cur)) >> 4); | ||
res += __int_to_hex(((const unsigned char)(*cur)) % 16); | ||
} | ||
|
||
cur++; | ||
|
@@ -120,17 +115,21 @@ std::string StringUtil::url_encode_component(const std::string& str) | |
|
||
while (cur < end) | ||
{ | ||
if (*cur == ' ') | ||
res += '+'; | ||
else if (isalnum(*cur) || *cur == '-' || *cur == '_' || *cur == '.' || | ||
*cur == '!' || *cur == '~' || *cur == '*' || *cur == '\'' || | ||
*cur == '(' || *cur == ')') | ||
if (isalnum(*cur) || *cur == '-' || *cur == '_' || *cur == '.' || | ||
*cur == '!' || *cur == '~' || *cur == '*' || *cur == '\'' || | ||
*cur == '(' || *cur == ')') | ||
{ | ||
res += *cur; | ||
} | ||
else if (*cur == ' ') | ||
{ | ||
res += '+'; | ||
} | ||
else | ||
{ | ||
res += '%'; | ||
res += __itoh(((const unsigned char)(*cur)) >> 4); | ||
res += __itoh(((const unsigned char)(*cur)) % 16); | ||
res += __int_to_hex(((const unsigned char)(*cur)) >> 4); | ||
res += __int_to_hex(((const unsigned char)(*cur)) % 16); | ||
} | ||
|
||
cur++; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
#ifndef _STRINGUTIL_H_ | ||
#define _STRINGUTIL_H_ | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
|