From dd2c70e878e4bc7fcf26f33523a45f0d854a29e4 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Sun, 24 Aug 2025 10:40:56 +0200 Subject: code simplification & macros --- src/json.c | 9 ++++----- src/str.c | 13 ------------- src/vec.c | 2 -- 3 files changed, 4 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/json.c b/src/json.c index 2b78e90..8531d48 100644 --- a/src/json.c +++ b/src/json.c @@ -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; } diff --git a/src/str.c b/src/str.c index 7efc684..3523d98 100644 --- a/src/str.c +++ b/src/str.c @@ -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); } diff --git a/src/vec.c b/src/vec.c index e64db9b..05ed70f 100644 --- a/src/vec.c +++ b/src/vec.c @@ -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; } -- cgit v1.3