aboutsummaryrefslogtreecommitdiff
path: root/src/json.c
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-08-25 18:29:13 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-08-27 11:46:25 +0200
commit14370ac9082c620eeec72e329b808ab3daa8d915 (patch)
tree8fdf651ca3e8144a757efa1e65eb265b42aee5b8 /src/json.c
parent372f52e13ad01512a35a1538a0a765dff7711095 (diff)
use std f32 parsing
Diffstat (limited to 'src/json.c')
-rw-r--r--src/json.c29
1 files changed, 8 insertions, 21 deletions
diff --git a/src/json.c b/src/json.c
index d3f510f..67bbf2e 100644
--- a/src/json.c
+++ b/src/json.c
@@ -1,9 +1,11 @@
#include <ctype.h>
#include <immintrin.h>
+#include <math.h>
#include <stdbit.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include "../include/assert.h"
@@ -122,27 +124,12 @@ parse_result parse_any(Arena *a, byte *src, uptr pos, uptr max_pos);
parse_result parse_number(byte *src, uptr start_pos) {
parse_result result = {0};
- bool is_neg = src[start_pos] == '-';
- result.pos = start_pos + is_neg;
- i32 integer = 0;
- while (isdigit(src[result.pos])) {
- integer = integer * 10 + (src[result.pos] - '0');
- result.pos++;
- }
- if (src[result.pos] != '.') {
- result.value = JSON_integer(is_neg ? -integer : integer);
- return result;
- }
- 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++;
- }
- result.value = JSON_float(is_neg ? -(f32)integer - fractional_part
- : (f32)integer + fractional_part);
+ byte *end;
+ f32 number = strtod(src + start_pos, &end);
+ assert_parse(end != src, "invalide number", src, result.pos);
+ result.pos += end - src;
+ result.value =
+ floor(number) == number ? JSON_integer(number) : JSON_float(number);
return result;
}