Tests and fixes for url_decode

This commit is contained in:
2026-06-17 17:34:07 +02:00
parent 434dca7fe7
commit 0208a7549c
2 changed files with 29 additions and 14 deletions

View File

@@ -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)

View File

@@ -3,7 +3,7 @@
#include "path.h"
#include "noh.h"
void check_string(size_t *good, size_t *bad, const char* input, const char *expected) {
void check_string(void (*fut)(Noh_String *), size_t *good, size_t *bad, const char* input, const char *expected) {
Noh_String test = noh_string_from_cstr(input);
Noh_String_View expected_sv = noh_sv_from_cstr(expected);
@@ -14,14 +14,14 @@ void check_string(size_t *good, size_t *bad, const char* input, const char *expe
opts->write_lf = true;
opts->write_prefix = false;
url_encode(&test);
fut(&test);
bool correct = noh_sv_eq(noh_sv_from_string(test), expected_sv);
if (!correct) {
noh_log(NOH_ERROR, " Incorrect.");
opts->write_prefix = true;
noh_log(NOH_ERROR, "Expected: "Nsv_Fmt, Nsv_Arg(expected_sv));
noh_log(NOH_ERROR, "Got : "Nsv_Fmt, Nsv_Arg(test));
noh_log(NOH_ERROR, "Expected: \""Nsv_Fmt"\"", Nsv_Arg(expected_sv));
noh_log(NOH_ERROR, "Got : \""Nsv_Fmt"\"", Nsv_Arg(test));
(*bad)++;
} else {
noh_log(NOH_INFO, " Ok.");
@@ -34,14 +34,29 @@ int main() {
size_t good = 0;
size_t bad = 0;
check_string(&good, &bad,
noh_log(NOH_INFO, "");
noh_log(NOH_INFO, "url_encode:");
check_string(url_encode, &good, &bad,
"abcdefghijklmnopqrstuvwxyz1023456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()*!-._",
"abcdefghijklmnopqrstuvwxyz1023456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()*!-._");
check_string(&good, &bad,
check_string(url_encode, &good, &bad,
"~!@#$%^&*()_+-=",
"%7e!%40%23%24%25%5e%26*()_%2b-%3d");
noh_log(NOH_INFO, "");
noh_log(NOH_INFO, "url_decode:");
check_string(url_decode, &good, &bad,
"abcdefghijklmnopqrstuvwxyz1023456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()*!-._",
"abcdefghijklmnopqrstuvwxyz1023456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()*!-._");
check_string(url_decode, &good, &bad,
"%7e!%40%23%24%25%5e%26*()_%2b-%3d",
"~!@#$%^&*()_+-=");
noh_log(NOH_INFO, "");
if (bad == 0) {
noh_log(NOH_INFO, "%zu/%zu tests passed.", good, good);
return 0;