aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-07-22 09:34:52 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-07-22 09:34:52 +0200
commit95018d0e410c22fc56a6518d967be25428e3a6ed (patch)
tree9bb0157a888f1c7ed1904a636d3a733d942dcccd
parent13fdee74dc28f0b068503843c6e14071a7c339f2 (diff)
feat(json): Float parsing
-rw-r--r--json.c48
-rw-r--r--json.h1
-rw-r--r--test_json.c34
-rw-r--r--testing.h10
-rw-r--r--types.h1
5 files changed, 64 insertions, 30 deletions
diff --git a/json.c b/json.c
index e782d02..736dd34 100644
--- a/json.c
+++ b/json.c
@@ -2,7 +2,6 @@
#include <assert.h>
#include <ctype.h>
-#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
@@ -10,6 +9,7 @@
#include "json.h"
#include "string.h"
+#include "types.h"
JSONObject *JSONObject_new(Arena *a) {
JSONObject *object = new(a, JSONObject, 1);
@@ -59,23 +59,34 @@ typedef struct {
parse_any_result parse_any(Arena *a, byte *src, uptr pos);
typedef struct {
- i32 *value;
+ i32 *integer;
+ f32 *floating;
uptr pos;
-} parse_integer_result;
+} parse_number_result;
-parse_integer_result parse_integer(Arena *a, byte *src, uptr start_pos) {
- byte ints[16];
- i32 i = 0;
- while (isdigit(src[start_pos + i])) {
- i32 val = (src[start_pos + i] - '0');
- ints[i] = val;
- i++;
+parse_number_result parse_number(Arena *a, byte *src, uptr start_pos) {
+ parse_number_result result = {.pos = start_pos};
+ i32 integer = 0;
+ while (isdigit(src[result.pos])) {
+ integer = integer * 10 + (src[result.pos] - '0');
+ result.pos++;
+ }
+ if (src[result.pos] != '.') {
+ result.integer = new(a, int, 1);
+ *result.integer = integer;
+ return result;
}
- i32 *result = new(a, int, 1);
- for (i32 j = 0; j < i; j++) {
- *result += ints[j] * pow(10, (i - j - 1));
+ result.pos++;
+ f32 fractional_part = 0.0;
+ f32 decimal_place = 0.1;
+ while (isdigit(src[result.pos])) {
+ fractional_part += (src[result.pos] - '0') * decimal_place;
+ decimal_place *= 0.1;
+ result.pos++;
}
- return (parse_integer_result){result, start_pos + i};
+ result.floating = new(a, f32, 1);
+ *result.floating = (f32)integer + fractional_part;
+ return result;
}
uptr absorb_whitespaces(byte *src, uptr start_pos) {
@@ -176,9 +187,12 @@ parse_any_result parse_any(Arena *a, byte *src, uptr start_pos) {
parse_any_result result = {.pos = start_pos};
result.value = new(a, JSON, 1);
if (isdigit(src[result.pos])) {
- parse_integer_result int_result = parse_integer(a, src, result.pos);
- result.value->integer = int_result.value;
- result.pos = int_result.pos;
+ parse_number_result number_result = parse_number(a, src, result.pos);
+ if (number_result.integer != 0)
+ result.value->integer = number_result.integer;
+ else if (number_result.floating != 0)
+ result.value->floating = number_result.floating;
+ result.pos = number_result.pos;
} else if (src[result.pos] == '[') {
parse_array_result array_result = parse_array(a, src, result.pos);
result.value->array = array_result.items;
diff --git a/json.h b/json.h
index 032c23c..7aba53e 100644
--- a/json.h
+++ b/json.h
@@ -12,6 +12,7 @@ typedef struct {
typedef struct {
i32 *boolean;
i32 *integer;
+ f32 *floating;
Vec *array; // Vec<JSON>
Str *string;
JSONObject *object;
diff --git a/test_json.c b/test_json.c
index 240a61e..91bae28 100644
--- a/test_json.c
+++ b/test_json.c
@@ -7,7 +7,7 @@ i32 test_json_integer() {
JSON json_int = JSON_parse(&perm, "123");
t_assert(*json_int.integer == 123);
- t_assert_equal(Arena_get_used(perm), 44);
+ t_assert_equal(Arena_get_used(perm), 52);
return 0;
}
@@ -29,7 +29,7 @@ i32 test_json_int_array() {
t_assert(ints[1] == 2);
t_assert(ints[2] == 3);
t_assert(sum == 6);
- t_assert_equal(Arena_get_used(perm), 464);
+ t_assert_equal(Arena_get_used(perm), 544);
return 0;
}
@@ -40,7 +40,7 @@ i32 test_json_empty_array() {
JSON json_empty_array = JSON_parse(&perm, "[]");
t_assert(json_empty_array.array->len == 0);
- t_assert_equal(Arena_get_used(perm), 160);
+ t_assert_equal(Arena_get_used(perm), 184);
return 0;
}
@@ -50,7 +50,6 @@ i32 test_json_nested_array() {
JSON doc = JSON_parse(&perm, "[[1,\"abc\"]]");
- t_assert_equal(Arena_get_used(perm), 427);
JSON sub_array = *Vec_get(doc.array, JSON, 0);
JSON val_1 = *Vec_get(sub_array.array, JSON, 0);
t_assert(*val_1.integer == 1);
@@ -58,7 +57,7 @@ i32 test_json_nested_array() {
Str str = *val_2.string;
Str expected = S("abc");
t_assert(Str_equals(str, expected));
- t_assert_equal(Arena_get_used(perm), 427);
+ t_assert_equal(Arena_get_used(perm), 491);
return 0;
}
@@ -68,9 +67,8 @@ i32 test_json_string() {
JSON js = JSON_parse(&perm, "\"abc\"");
- t_assert_equal(Arena_get_used(perm), 59);
t_assert(Str_equals(*js.string, S("abc")));
- t_assert_equal(Arena_get_used(perm), 59);
+ t_assert_equal(Arena_get_used(perm), 67);
return 0;
}
@@ -81,7 +79,7 @@ i32 test_json_empty_object() {
JSON doc = JSON_parse(&perm, "{}");
t_assert_equal(doc.object->len, 0);
- t_assert_equal(Arena_get_used(perm), 320);
+ t_assert_equal(Arena_get_used(perm), 344);
return 0;
}
@@ -91,12 +89,11 @@ i32 test_json_object() {
JSON doc = JSON_parse(&perm, "{\"key\": \"value\"}");
- t_assert_equal(Arena_get_used(perm), 405);
t_assert_equal(doc.object->len, 1);
t_assert(Str_equals(*Vec_get(&doc.object->keys, Str, 0), S("key")));
JSON value = *JSONObject_get(doc.object, S("key"));
t_assert(Str_equals(*value.string, S("value")));
- t_assert_equal(Arena_get_used(perm), 405);
+ t_assert_equal(Arena_get_used(perm), 437);
return 0;
}
@@ -106,12 +103,25 @@ i32 test_json_boolean() {
JSON doc = JSON_parse(&perm, "{\"a\": true, \"b\": false}");
- t_assert_equal(Arena_get_used(perm), 460);
JSON a = *JSONObject_get(doc.object, S("a"));
t_assert(*a.boolean == 1);
JSON b = *JSONObject_get(doc.object, S("b"));
t_assert(*b.boolean == 0);
- t_assert_equal(Arena_get_used(perm), 460);
+ t_assert_equal(Arena_get_used(perm), 500);
+
+ return 0;
+}
+
+i32 test_json_float() {
+ Arena perm = Arena_new(128e3);
+
+ JSON doc = JSON_parse(&perm, "[1.2, 2.3]");
+ t_assert_equal(doc.array->len, 2);
+ JSON first = *Vec_get(doc.array, JSON, 0);
+ t_assert_fequal(*first.floating, 1.2);
+ JSON second = *Vec_get(doc.array, JSON, 1);
+ t_assert_fequal(*second.floating, 2.3);
+ t_assert_equal(Arena_get_used(perm), 292);
return 0;
}
diff --git a/testing.h b/testing.h
index d7247f1..9e0c022 100644
--- a/testing.h
+++ b/testing.h
@@ -11,6 +11,14 @@
if (!(a == b)) { \
fprintf(stderr, \
"Assertion failed: %d (%s) != %d (%s) at %s:%d in %s()\n", \
- (int)a, #a, (int)b, #b, __FILE__, __LINE__, __func__); \
+ (i32)a, #a, (i32)b, #b, __FILE__, __LINE__, __func__); \
+ return 1; \
+ }
+
+#define t_assert_fequal(a, b) \
+ if ((a - b) > 1e-6 || (b - a) > 1e-6) { \
+ fprintf(stderr, \
+ "Assertion failed: %f (%s) != %f (%s) at %s:%d in %s()\n", a, \
+ #a, b, #b, __FILE__, __LINE__, __func__); \
return 1; \
}
diff --git a/types.h b/types.h
index 724e5a1..bacedf8 100644
--- a/types.h
+++ b/types.h
@@ -1,3 +1,4 @@
+#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>