diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-08-24 10:40:56 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-08-27 11:46:25 +0200 |
| commit | dd2c70e878e4bc7fcf26f33523a45f0d854a29e4 (patch) | |
| tree | 28e0d7c8e3f213bc77c82ffbbb0bb2ba5ff5f253 | |
| parent | 086cb73ef88a4962345b8284e47cea52da428135 (diff) | |
code simplification & macros
| -rw-r--r-- | include/str.h | 8 | ||||
| -rw-r--r-- | include/vec.h | 3 | ||||
| -rw-r--r-- | src/json.c | 9 | ||||
| -rw-r--r-- | src/str.c | 13 | ||||
| -rw-r--r-- | src/vec.c | 2 |
5 files changed, 11 insertions, 24 deletions
diff --git a/include/str.h b/include/str.h index 781d910..153eebc 100644 --- a/include/str.h +++ b/include/str.h @@ -16,11 +16,15 @@ typedef struct { byte *data; size len; } StrView; -i32 Str_equals(Str a, Str b); u64 Str_hash64(Str s); Str Str_copy(Arena *a, byte *data, size len); i32 Str_fprint(FILE *w, Str s); StrView Str_view(byte *data, size len); -i32 StrView_fprint(FILE *w, StrView s); Str StrView_copy(Arena *a, StrView s); + +// Macros working with both types +// Returns 1 if strings are equal, 0 otherwise +#define Str_equals(a, b) \ + ((a).len == (b).len) && (!(a).len || !memcmp((a).data, (b).data, (a).len)) +#define Str_fprint(w, s) fprintf(w, "%.*s", (i32)(s).len, (s).data) diff --git a/include/vec.h b/include/vec.h index bf6729b..8ff0332 100644 --- a/include/vec.h +++ b/include/vec.h @@ -15,5 +15,4 @@ typedef struct { #define Vec_new(a, T, cap) _Vec_new(a, sizeof(T), cap, _Alignof(T)) Vec _Vec_new(Arena *a, size tsize, size cap, size align); void Vec_push(Arena *a, Vec *v, void *data); -#define Vec_get(v, T, p) (T *)_Vec_get(v, p) -void *_Vec_get(Vec v, size pos); +#define Vec_get(v, T, pos) (T *)(v.data + v.tsize * pos) @@ -74,7 +74,7 @@ i32 JSON_fprint(FILE *w, JSON json) { switch (json.type) { case JSON_STRING: fprintf(w, "\""); - StrView_fprint(w, json.as.string); + Str_fprint(w, json.as.string); return fprintf(w, "\""); case JSON_INTEGER: return fprintf(w, "%d", json.as.integer); @@ -98,7 +98,7 @@ i32 JSON_fprint(FILE *w, JSON json) { for (size i = 0; i < json.as.object.len; i++) { StrView key = *Vec_get(json.as.object.keys, StrView, i); fprintf(w, "\""); - StrView_fprint(w, key); + Str_fprint(w, key); fprintf(w, "\": "); JSON value = *Vec_get(json.as.object.values, JSON, i); JSON_fprint(w, value); @@ -281,9 +281,8 @@ JSON JSON_parse(Arena *a, byte *src, uptr len) { JSON *JSONObject_get(JSONObject obj, Str key) { for (size i = 0; i < obj.len; i++) { - if (Str_equals(*Vec_get(obj.keys, Str, i), key)) { - return Vec_get(obj.values, JSON, i); - } + StrView tmp_key = *Vec_get(obj.keys, StrView, i); + if (Str_equals(tmp_key, key)) return Vec_get(obj.values, JSON, i); } return 0; } @@ -5,11 +5,6 @@ #include "../include/memory.h" #include "../include/str.h" -// Returns 1 if strings are equal, 0 otherwise -i32 Str_equals(Str a, Str b) { - return (a.len == b.len) && (!a.len || !memcmp(a.data, b.data, a.len)); -} - u64 Str_hash64(Str s) { u64 h = 0x100; for (size i = 0; i < s.len; i++) { @@ -27,10 +22,6 @@ Str Str_copy(Arena *a, byte *data, size len) { return str; } -i32 Str_fprint(FILE *w, Str s) { - return fprintf(w, "%.*s", (i32)s.len, s.data); -} - StrView Str_view(byte *data, size len) { StrView str = {0}; str.len = len; @@ -38,8 +29,4 @@ StrView Str_view(byte *data, size len) { return str; } -i32 StrView_fprint(FILE *w, StrView s) { - return fprintf(w, "%.*s", (i32)s.len, s.data); -} - Str StrView_copy(Arena *a, StrView s) { return Str_copy(a, s.data, s.len); } @@ -25,5 +25,3 @@ void Vec_push(Arena *a, Vec *v, void *data) { memcpy(v->data + v->len * v->tsize, data, v->tsize); v->len++; } - -void *_Vec_get(Vec v, size pos) { return v.data + v.tsize * pos; } |
