Commit 90f3fbb05f3ae06fd9a259c1f71dd02ca6f9ce1f

Baptiste 2024-08-14T13:33:21

json compiled

diff --git a/json/json.c b/json/json.c
index faf90be..7e5e445 100644
--- a/json/json.c
+++ b/json/json.c
@@ -12,127 +12,77 @@
  */
 #include <libkc3/kc3.h>
 #include "json.h"
-#include "buf.h"
-#include "buf_parse.h"
-#include "err.h"
-#include "tag.h"
 
-s_tag * json_buf_parse_map (const s_buf *buf, s_tag *dest) {
+/*s_tag * json_buf_parse_null (const s_buf *buf, s_tag *dest) {
+  character c;
+  sw r;
   s_buf_save save;
   assert(buf);
   assert(dest);
-  dest->type = TAG_MAP
-  dest->data.map = map_new();
   buf_save_init(buf, &save);
-  if (! buf_parse_map(buf, dest->data.map)) {
-    buf_restore_rpos(buf, &save);
-    return NULL;
+  if ((r = buf_read_1(buf, "void")) <= 0)
+    goto clean;
+  if (buf_peek_character_utf8(buf, &c) > 0 &&
+      ! ident_character_is_reserved(c)) {
+    r = 0;
+    goto restore;
   }
-  return dest;
-}
+  goto clean;
+ restore:
+  buf_save_restore_rpos(buf, &save);
+ clean:
+  buf_save_clean(buf, &save);
+  return r;
+}*/
 
-s_tag * json_buf_parse_list (const s_buf *buf, s_tag *dest) {
+s_tag * json_buf_parse_numbers (s_buf *buf, s_tag *dest)
+{
   s_buf_save save;
   assert(buf);
   assert(dest);
-  dest->type = TAG_LIST;
-  dest->data.list = list_new();
   buf_save_init(buf, &save);
-  if (! buf_parse_list(buf, dest->data.list)) {
-    buf_restore_rpos(buf, &save);
+  if (! buf_parse_tag_number(buf, dest)) {
+    buf_save_restore_rpos(buf, &save);
     return NULL;
   }
   return dest;
 }
 
-s_tag * json_buf_parse_str (const s_buf *buf, s_tag *dest) {
+s_tag * json_buf_parse (s_buf *buf, s_tag *dest)
+{
+  character c;
+  sw r;
   s_buf_save save;
+  s_tag tmp;
   assert(buf);
   assert(dest);
-  dest->type = TAG_STR;
-  dest->data.str = str_new();
   buf_save_init(buf, &save);
-  if (! buf_parse_str(buf, dest->data.str)) {
-    buf_restore_rpos(buf, &save);
-    return NULL;
-  }
-  return dest;
-}
-
-s_tag * json_buf_parse_numbers (const s_buf *buf, s_tag *dest) {
-  assert(buf);
-  assert(dest);
-  if (! buf_parse_tag_number(buf, dest)) {
-    buf_restore_rpos(buf, &save);
+  if ((r =buf_peek_character_utf8(buf, &c)) < 0)
     return NULL;
-  }
-  return dest;
-}
-
-s_tag * json_buf_parse_boolean (const s_buf *buf, s_tag *dest) {
-  assert(buf);
-  assert(dest);
-  if (buf_peek(buf) == 't') {
-    dest->type = TAG_BOOL;
-    dest->data.boolean = true;
-    buf_skip(buf, 4);
-  } else {
-    dest->type = TAG_BOOL;
-    dest->data.boolean = false;
-    buf_skip(buf, 5);
-  }
-  return dest;
-}
-
-static bool parse_json(const s_buf *buf, s_tag *dest) {
-  assert(buf);
-  assert(dest);
-  switch (buf_peek(buf)) {
-    case '{':
-      json_buf_parse_map(buf, dest);
-      if (buf_peek(buf) != '}') {
-        errputs("json_parse: expected '}'");
-        return false;
-      }
-      break;
-    case '[':
-      json_buf_parse_list(buf, dest);
-        if (buf_peek(buf) != ']') {
-          errputs("json_parse: expected ']'");
-          return false;
-       }
-      break;
+  switch (c) {
     case '"':
-      json_buf_parse_str(buf, dest);
-      if (buf_peek(buf) != '"') {
-        errputs("json_parse: expected '\"'");
-        return false;
-      }
+      buf_parse_tag_str(buf, &tmp);
       break;
-    case '0' ... '9':
-      json_buf_parse_numbers(buf, dest);
+    case '0':
+    case '1':
+    case '2':
+    case '3':
+    case '4':
+    case '5':
+    case '6':
+    case '7':
+    case '8':
+    case '9':
+    case '-':
+      json_buf_parse_numbers(buf, &tmp);
       break;
     case 't':
     case 'f':
-      json_buf_parse_boolean(buf, dest);
-      break;
-    case 'n':
-      dest->type = TAG_NULL;
+      buf_parse_tag_bool(buf, &tmp);
       break;
     default:
-      return false;
-  }
-  return true;
-}
-
-s_tag *json_parse(const s_buf *buf, s_tag *dest) {
-  assert(buf);
-  assert(dest);
-  s_buf_save save;
-  buf_save_init(buf, &save);
-  if (! parse_json(buf, dest)) {
-    buf_restore_rpos(buf, &save);
-    return NULL;
+      return NULL;
   }
+  *dest = tmp;
   return dest;
 }
diff --git a/libkc3/map.h b/libkc3/map.h
index 2b77b7f..a1bc363 100644
--- a/libkc3/map.h
+++ b/libkc3/map.h
@@ -17,7 +17,7 @@
 
 /* Stack allocation compatible functions, call map_clean after use. */
 void    map_clean (s_map *map);
-s_map * map_init (s_map *map, uw size);
+s_map * map_init (s_map *map, uw count);
 s_map * map_init_1 (s_map *map, const char *p);
 s_map * map_init_cast (s_map *map, const s_tag *tag);
 s_map * map_init_copy (s_map *map, const s_map *src);
diff --git a/test/json/basics.diff b/test/json/basics.diff
new file mode 100644
index 0000000..d9a6cbc
--- /dev/null
+++ b/test/json/basics.diff
@@ -0,0 +1,4 @@
+--- basics.ret.expected	2024-08-13 18:25:48.016470147 +0200
++++ basics.ret	2024-08-14 13:27:51.097516149 +0200
+@@ -0,0 +1 @@
++0
diff --git a/test/json/basics.out b/test/json/basics.out
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/json/basics.out
diff --git a/test/json/basics.out.expected b/test/json/basics.out.expected
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/json/basics.out.expected
diff --git a/test/json/basics.ret b/test/json/basics.ret
new file mode 100644
index 0000000..573541a
--- /dev/null
+++ b/test/json/basics.ret
@@ -0,0 +1 @@
+0
diff --git a/test/json/basics.ret.expected b/test/json/basics.ret.expected
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/json/basics.ret.expected