Tests and fixes for url_decode
This commit is contained in:
16
src/path.c
16
src/path.c
@@ -33,8 +33,8 @@ static inline char to_char_lower(int value) {
|
||||
|
||||
static inline int from_char(char c) {
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'a' && c <= 'z') return c - 'a';
|
||||
if (c >= 'A' && c <= 'Z') return c - 'A';
|
||||
if (c >= 'a' && c <= 'z') return c - 'a' + 10;
|
||||
if (c >= 'A' && c <= 'Z') return c - 'A' + 10;
|
||||
|
||||
return 0xFF;
|
||||
}
|
||||
@@ -73,17 +73,17 @@ void url_decode(Noh_String *string) {
|
||||
while (read < string->count) {
|
||||
char c = string->elems[read++];
|
||||
if (c == '+') {
|
||||
string->elems[write++] = ' ';
|
||||
string->elems[write++] = ' ';
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '%') {
|
||||
int h1 = string->elems[read];
|
||||
int h2 = string->elems[read + 1];
|
||||
int h1 = from_char(string->elems[read]);
|
||||
int h2 = from_char(string->elems[read + 1]);
|
||||
|
||||
// If valid hex chars, add it.
|
||||
if ((h1 | h2) != 0xFF) {
|
||||
string->elems[write] = (char)((h1 << 4) | h2);
|
||||
string->elems[write++] = (char)((h1 << 4) | h2);
|
||||
read += 2;
|
||||
continue;
|
||||
}
|
||||
@@ -96,10 +96,10 @@ void url_decode(Noh_String *string) {
|
||||
}
|
||||
|
||||
// Zero-out the rest.
|
||||
string->count = read;
|
||||
for (size_t i = read; i < string->count; i++) {
|
||||
for (size_t i = write; i < string->count; i++) {
|
||||
string->elems[i] = 0;
|
||||
}
|
||||
string->count = write;
|
||||
}
|
||||
|
||||
#define build_path(url, first, ...) build_path_((url), (first), __VA_ARGS__, NULL)
|
||||
|
||||
Reference in New Issue
Block a user