今天在整理我的 wiki 的時候才發現以前寫的筆記有不少東西已經不見了,怕再消失,所以把 source 也丟一份在這邊:
static char*
urlencode(char *s, int len, int *new_len)
{
unsigned char *from, *start, *end, *to;
unsigned c;
unsigned char hexchars[] = "0123456789ABCDEF";
from = s;
end = s + len;
start = to = (unsigned char *) malloc (3 * len + 1);
while (from < end)
{
c = *from;
if (c == ' ')
{
*to = '+';
to ++;
}
else if (
(c < '0' && c != '-' && c != '.') ||
(c < 'A' && c > '9') ||
(c > 'Z' && c < 'a' && c != '_') ||
(c > 'z'))
{
to[0] = '%';
to[1] = hexchars[c >> 4];
to[2] = hexchars[c & 15];
to += 3;
}
else
{
*to++ = c;
}
from ++;
}
*to = 0;
if (new_len)
{
*new_len = to - start;
}
return (unsigned char *) start;
}
這個 code 我有再依自已習慣的 code style 整理過。
話說,從退伍到現在,自已的 code style 變化不少。
沒有留言:
張貼留言