From 577fe25f20c2e04b5e6444fbbbbcbc0e93943d7d Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Mon, 10 Feb 2025 22:11:54 +0100 Subject: refactor(json): Input char* --- json.c | 44 +++++++++++++++++++++----------------------- json.h | 2 +- main.c | 8 ++++---- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/json.c b/json.c index 4a49bbf..6948edf 100644 --- a/json.c +++ b/json.c @@ -5,7 +5,6 @@ #include #include #include -#include #include "string.h" @@ -14,11 +13,11 @@ typedef struct { uintptr_t pos; } parse_integer_result; -parse_integer_result parse_integer(Arena *a, Str src, uintptr_t pos) { +parse_integer_result parse_integer(Arena *a, char *src, uintptr_t pos) { int *ints = new (a, int, 16); // TODO: reuse in parsing int i = 0; - while (isdigit(src.data[pos + i])) { - int val = (src.data[pos + i] - '0'); + while (isdigit(src[pos + i])) { + int val = (src[pos + i] - '0'); ints[i] = val; i++; } @@ -30,9 +29,9 @@ parse_integer_result parse_integer(Arena *a, Str src, uintptr_t pos) { return (parse_integer_result){result, pos + i}; } -uintptr_t absorb_whitespaces(Str src, uintptr_t pos) { +uintptr_t absorb_whitespaces(char *src, uintptr_t pos) { int i = 0; - while (isspace(src.data[pos + i])) { + while (isspace(src[pos + i])) { i++; } return pos + i; @@ -43,9 +42,9 @@ typedef struct { uintptr_t pos; } parse_array_result; -parse_array_result parse_array(Arena *a, Str src, uintptr_t start_pos) { - printf("json.parse_array: parsing array %s\n", src.data + start_pos); - assert(src.data[start_pos] == '['); +parse_array_result parse_array(Arena *a, char *src, uintptr_t start_pos) { + printf("json.parse_array: parsing array %s\n", src + start_pos); + assert(src[start_pos] == '['); uintptr_t pos = start_pos + 1; Vec *items = Vec_new(a, JSON, 0); for (;;) { @@ -55,13 +54,13 @@ parse_array_result parse_array(Arena *a, Str src, uintptr_t start_pos) { items = Vec_push(a, items, &result.value); pos = result.pos; pos = absorb_whitespaces(src, pos); - if (src.data[pos] == ',') { + if (src[pos] == ',') { pos++; } else { break; } }; - assert(src.data[pos] == ']'); + assert(src[pos] == ']'); return (parse_array_result){items, pos + 1}; } @@ -71,36 +70,35 @@ typedef struct { uintptr_t pos; } parse_string_result; -parse_string_result parse_string(Arena *a, Str src, uintptr_t pos) { - printf("json.parse_string: %s\n", src.data + pos); - assert(src.data[pos] == '"'); +parse_string_result parse_string(Arena *a, char *src, uintptr_t pos) { + printf("json.parse_string: %s\n", src + pos); + assert(src[pos] == '"'); int i = 1; - while (src.data[pos + i] != '"' && src.data[pos + i - 1] != '\\') { + while (src[pos + i] != '"' && src[pos + i - 1] != '\\') { i++; - assert(i + pos <= src.len); } - assert(src.data[pos + i]); - Str s = Str_copy(a, &src.data[pos + 1], i - 1); + assert(src[pos + i]); + Str s = Str_copy(a, &src[pos + 1], i - 1); return (parse_string_result){s, pos + i}; } -JSON JSON_parse(Arena *a, Str src) { +JSON JSON_parse(Arena *a, char *src) { JSON json = {0}; int pos = 0; - printf("json.parse: src=%s\n", src.data); - if (isdigit(src.data[pos])) { + printf("json.parse: src=%s\n", src); + if (isdigit(src[pos])) { parse_integer_result result = parse_integer(a, src, 0); json.integer = &result.value; pos = result.pos; } - if (src.data[pos] == '[') { + if (src[pos] == '[') { parse_array_result result = parse_array(a, src, pos); json.array = result.items; pos = result.pos; } - if (src.data[pos] == '"') { + if (src[pos] == '"') { parse_string_result result = parse_string(a, src, pos); json.string = &result.string; pos = result.pos; diff --git a/json.h b/json.h index 588cb6b..072f97d 100644 --- a/json.h +++ b/json.h @@ -9,5 +9,5 @@ typedef struct { Vec *array; Str *string; } JSON; -JSON JSON_parse(Arena *a, Str src); +JSON JSON_parse(Arena *a, char *src); #endif diff --git a/main.c b/main.c index 145d768..ca21ea8 100644 --- a/main.c +++ b/main.c @@ -28,10 +28,10 @@ int main() { assert(Str_equals(s, Str_copy(&perm, s.data, s.len))); // Testing JSON printf("Arena: mem=%d B\n", Arena_get_used(perm)); - JSON json_int = JSON_parse(&perm, S("123")); + JSON json_int = JSON_parse(&perm, "123"); assert(*json_int.integer == 123); printf("Arena: mem=%d B\n", Arena_get_used(perm)); - JSON json_int_array = JSON_parse(&perm, S("[1,2,3]")); + JSON json_int_array = JSON_parse(&perm, "[1,2,3]"); assert(json_int_array.integer == NULL); assert(json_int_array.array->len == 3); int sum = 0; @@ -40,9 +40,9 @@ int main() { } assert(sum == 6); printf("Arena: mem=%d B\n", Arena_get_used(perm)); - assert(*Vec_get(JSON_parse(&perm, S("[1, 2, 3]")).array, int, 1) == 2); + assert(*Vec_get(JSON_parse(&perm, "[1, 2, 3]").array, int, 1) == 2); printf("Arena: mem=%d B\n", Arena_get_used(perm)); - JSON js = JSON_parse(&perm, S("\"abc\"")); + JSON js = JSON_parse(&perm, "\"abc\""); assert(Str_equals(*js.string, S("abc")) == 1); printf("Arena: mem=%d B\n", Arena_get_used(perm)); return 0; -- cgit v1.3