Tests and fixes for url_encode
This commit is contained in:
47
src/noh.h
47
src/noh.h
@@ -156,9 +156,21 @@ typedef enum {
|
||||
NOH_ERROR,
|
||||
} Noh_Log_Level;
|
||||
|
||||
// Options that can alter the behavior of noh_log.
|
||||
typedef struct {
|
||||
// Whether to write a line feed at the end of a log message. Default is true.
|
||||
bool write_lf;
|
||||
|
||||
// Whether to write a line feed at the end of a log message. Default is true.
|
||||
bool write_prefix;
|
||||
} Noh_Log_Options;
|
||||
|
||||
// Writes a formatted log message to stderr with the provided log level.
|
||||
void noh_log(Noh_Log_Level level, const char *fmt, ...);
|
||||
|
||||
// Returns a pointer to the noh_log options.
|
||||
Noh_Log_Options *noh_log_get_opts();
|
||||
|
||||
///////////////////////// Dynamic array /////////////////////////
|
||||
|
||||
#define NOH_DA_INIT_CAP 256
|
||||
@@ -609,27 +621,36 @@ void noh_time_add(struct timespec *time, int seconds, long milliseconds) {
|
||||
|
||||
///////////////////////// Logging /////////////////////////
|
||||
|
||||
Noh_Log_Options *noh_log_get_opts() {
|
||||
static Noh_Log_Options opts = { .write_lf = true, .write_prefix = true, };
|
||||
return &opts;
|
||||
}
|
||||
|
||||
void noh_log(Noh_Log_Level level, const char *fmt, ...)
|
||||
{
|
||||
switch (level) {
|
||||
case NOH_INFO:
|
||||
fprintf(stderr, "[INFO] ");
|
||||
break;
|
||||
case NOH_WARNING:
|
||||
fprintf(stderr, "[WARNING] ");
|
||||
break;
|
||||
case NOH_ERROR:
|
||||
fprintf(stderr, "[ERROR] ");
|
||||
break;
|
||||
default:
|
||||
noh_assert(false && "Invalid log level");
|
||||
Noh_Log_Options *opts = noh_log_get_opts();
|
||||
|
||||
if (opts->write_prefix) {
|
||||
switch (level) {
|
||||
case NOH_INFO:
|
||||
fprintf(stderr, "[INFO] ");
|
||||
break;
|
||||
case NOH_WARNING:
|
||||
fprintf(stderr, "[WARNING] ");
|
||||
break;
|
||||
case NOH_ERROR:
|
||||
fprintf(stderr, "[ERROR] ");
|
||||
break;
|
||||
default:
|
||||
noh_assert(false && "Invalid log level");
|
||||
}
|
||||
}
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
fprintf(stderr, "\n");
|
||||
if (opts->write_lf) fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
///////////////////////// Arena /////////////////////////
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
static inline bool byte_is_url_safe(char c) {
|
||||
return (c >= 'A' && c <= 'Z')
|
||||
|| (c >= 'a' && c <= 'z')
|
||||
|| (c >= '0' && c <+ '9')
|
||||
|| (c >= '0' && c <= '9')
|
||||
|| (c >= '(' && c <= '*')
|
||||
|| c == '!' || c == '-'
|
||||
|| c == '.' || c == '_';
|
||||
@@ -15,12 +15,12 @@ static bool needs_encoding(Noh_String string, int *unsafe_count) {
|
||||
*unsafe_count = 0;
|
||||
bool safe = true ;
|
||||
for (size_t i = 0; i < string.count; i++) {
|
||||
bool needs_encode = byte_is_url_safe(string.elems[i]);
|
||||
bool needs_encode = !byte_is_url_safe(string.elems[i]);
|
||||
if (needs_encode || string.elems[i] == ' ') safe = false;
|
||||
if (needs_encode) (*unsafe_count)++;
|
||||
}
|
||||
|
||||
return safe;
|
||||
return !safe;
|
||||
}
|
||||
|
||||
static inline char to_char_lower(int value) {
|
||||
@@ -55,6 +55,8 @@ void url_encode(Noh_String *string) {
|
||||
noh_da_append(&result, '%');
|
||||
noh_da_append(&result, to_char_lower(cur >> 4));
|
||||
noh_da_append(&result, to_char_lower(cur));
|
||||
} else {
|
||||
noh_da_append(&result, cur);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user