Skip to content

Commit

Permalink
Optimize StringUtil::url_encode()/url_decode().
Browse files Browse the repository at this point in the history
  • Loading branch information
Barenboim committed Nov 23, 2024
1 parent f272c0e commit 38d64ca
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
77 changes: 38 additions & 39 deletions src/util/StringUtil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
limitations under the License.
Authors: Wu Jiaxu ([email protected])
Xie Han ([email protected])
*/

#include <ctype.h>
Expand All @@ -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)
Expand All @@ -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 == '+')
Expand All @@ -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)
Expand All @@ -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++;
Expand All @@ -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++;
Expand Down
1 change: 1 addition & 0 deletions src/util/StringUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#ifndef _STRINGUTIL_H_
#define _STRINGUTIL_H_

#include <string>
#include <vector>

Expand Down

0 comments on commit 38d64ca

Please sign in to comment.