Commit f225c6b76e90ee325e8ee0040d8e8b7b970bf7e7

Thomas de Grivel 2024-07-31T17:58:47

wip buf_inspect_struct

diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index d0238dd..555b3a8 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -2330,13 +2330,8 @@ sw buf_inspect_struct (s_buf *buf, const s_struct *s)
   result += r;
   if (! sym_is_module(s->type->module))
     return -1;
-  if (sym_has_reserved_characters(s->type->module)) {
-    if ((r = buf_write_str(buf, &s->type->module->str)) < 0)
-      return r;
-  }
-  else
-    if ((r = buf_write_1(buf, s->type->module->str.ptr.pchar)) < 0)
-      return r;
+  if ((r = buf_write_str(buf, &s->type->module->str)) < 0)
+    return r;
   result += r;
   if ((r = buf_write_1(buf, "{")) < 0)
     return r;
@@ -2354,11 +2349,11 @@ sw buf_inspect_struct (s_buf *buf, const s_struct *s)
         return -1;
       }
       if (sym_has_reserved_characters(k->data.sym)) {
-        if ((r = buf_write_str(buf, &k->data.sym->str)) < 0)
+        if ((r = buf_inspect_str(buf, &k->data.sym->str)) < 0)
           return r;
       }
       else
-        if ((r = buf_write_1(buf, k->data.sym->str.ptr.pchar)) < 0)
+        if ((r = buf_write_str(buf, &k->data.sym->str)) < 0)
           return r;
       result += r;
       if ((r = buf_write_1(buf, ": ")) < 0)
@@ -2404,10 +2399,7 @@ sw buf_inspect_struct_size (const s_struct *s)
   result += strlen("%");
   if (! sym_is_module(s->type->module))
     return -1;
-  if (sym_has_reserved_characters(s->type->module))
-    result += s->type->module->str.size;
-  else
-    result += strlen(s->type->module->str.ptr.pchar);
+  result += s->type->module->str.size;
   result += strlen("{");
   if (s->data || s->tag) {
     while (i < s->type->map.count) {
@@ -2421,10 +2413,13 @@ sw buf_inspect_struct_size (const s_struct *s)
         assert(k->type == TAG_SYM);
         return -1;
       }
-      if (sym_has_reserved_characters(k->data.sym))
-        result += k->data.sym->str.size;
+      if (sym_has_reserved_characters(k->data.sym)) {
+        if ((r = buf_inspect_str_size(&k->data.sym->str)) < 0)
+          return r;
+      }
       else
-        result += strlen(k->data.sym->str.ptr.pchar);
+        r = k->data.sym->str.size;
+      result += r;
       result += strlen(": ");
       if (s->data) {
         if (! tag_type(s->type->map.value + i, &type))
diff --git a/libkc3/str.c b/libkc3/str.c
index 7df45b6..bc6525b 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -72,14 +72,27 @@
   s_str * str_init_ ## name (s_str *str, const s_ ## name *x)          \
   {                                                                    \
     s_buf buf;                                                         \
+    sw r;                                                              \
     sw size;                                                           \
     size = buf_inspect_ ## name ## _size(x);                           \
     if (! size)                                                        \
       return str_init_empty(str);                                      \
-    if (size < 0)                                                      \
+    if (size < 0) {                                                    \
+      err_puts("str_init_" # name ": buf_inspect_" # name              \
+               "_size < 0");                                           \
       return NULL;                                                     \
-    buf_init_alloc(&buf, size);                                        \
-    if (buf_inspect_ ## name(&buf, x) < 0) {                           \
+    }                                                                  \
+    if (! buf_init_alloc(&buf, size)) {                                \
+      err_puts("str_init_" # name ": buf_init_alloc");                 \
+      return NULL;                                                     \
+    }                                                                  \
+    if ((r = buf_inspect_ ## name(&buf, x)) < 0) {                     \
+      err_puts("str_init_" # name ": buf_inspect_" # name);            \
+      buf_clean(&buf);                                                 \
+      return NULL;                                                     \
+    }                                                                  \
+    if (r != size) {                                                   \
+      err_puts("str_init_" # name ": buf_inspect_" # name);            \
       buf_clean(&buf);                                                 \
       return NULL;                                                     \
     }                                                                  \
diff --git a/test/ikc3/str.kc3 b/test/ikc3/str.kc3
index ab8a962..7c313ee 100644
--- a/test/ikc3/str.kc3
+++ b/test/ikc3/str.kc3
@@ -48,3 +48,9 @@ def
 """
 quote "1 + 2 = #{1 + 2}"
 "1 + 2 = #{1 + 2}"
+quote "#{%{a: 1, b: 2}}"
+"#{%{a: 1, b: 2}}"
+quote "#{%KC3.Operator{}}"
+"#{%KC3.Operator{}}"
+quote "#{%KC3.Operator{sym: :-}}"
+"#{%KC3.Operator{sym: :-}}"
diff --git a/test/ikc3/str.out.expected b/test/ikc3/str.out.expected
index 4611598..6541d3b 100644
--- a/test/ikc3/str.out.expected
+++ b/test/ikc3/str.out.expected
@@ -33,3 +33,9 @@
 "abc\ndef"
 "1 + 2 = #{1 + 2}"
 "1 + 2 = 3"
+"#{%{a: 1, b: 2}}"
+"%{a: 1, b: 2}"
+"#{%KC3.Operator{}}"
+"%KC3.Operator{sym: :+, symbol_value: ?, operator_precedence: (U8) 0, operator_associativity: :left}"
+"#{%KC3.Operator{sym: :-}}"
+"%KC3.Operator{sym: :-, symbol_value: ?, operator_precedence: (U8) 0, operator_associativity: :left}"