Commit 6cb555c72fdb76179591ceb00617eb02269f1cad

Thomas de Grivel 2023-06-12T12:16:13

wip buf_inspect

diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index 38fd3fb..fdebbb4 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -694,6 +694,42 @@ sw buf_inspect_fn_algo (s_buf *buf, const s_list *algo)
   return result;
 }
 
+sw buf_inspect_fn_algo_size (const s_list *algo)
+{
+  sw r;
+  sw result = 0;
+  if (! algo) {
+    r = strlen("{}");
+    result += r;
+    return result;
+  }
+  if (! list_next(algo)) {
+    r = strlen("{ ");
+    result += r;
+    if ((r = buf_inspect_tag_size(&algo->tag)) < 0)
+      return r;
+    result += r;
+    r = strlen(" }");
+    result += r;
+    return result;
+  }
+  r = strlen("{\n  ");
+  result += r;
+  while (algo) {
+    if ((r = buf_inspect_tag_size(&algo->tag)) < 0)
+      return r;
+    result += r;
+    algo = list_next(algo);
+    if (algo) {
+      r = strlen(";\n  ");
+      result += r;
+    }
+  }
+  r = strlen("\n}");
+  result += r;
+  return result;
+}
+
 sw buf_inspect_fn_clause (s_buf *buf, const s_fn *fn)
 {
   sw r;
@@ -703,7 +739,7 @@ sw buf_inspect_fn_clause (s_buf *buf, const s_fn *fn)
   if ((r = buf_inspect_fn_pattern(buf, fn->pattern)) < 0)
     return r;
   result += r;
-  if ((r = buf_write_u8(buf, ' ')) < 0)
+  if ((r = buf_write_1(buf, " ")) < 0)
     return r;
   result += r;
   if ((r = buf_inspect_fn_algo(buf, fn->algo)) < 0)
@@ -712,6 +748,22 @@ sw buf_inspect_fn_clause (s_buf *buf, const s_fn *fn)
   return result;
 }
 
+sw buf_inspect_fn_clause_size (const s_fn *fn)
+{
+  sw r;
+  sw result = 0;
+  assert(fn);
+  if ((r = buf_inspect_fn_pattern_size(fn->pattern)) < 0)
+    return r;
+  result += r;
+  r = strlen(" ");
+  result += r;
+  if ((r = buf_inspect_fn_algo_size(fn->algo)) < 0)
+    return r;
+  result += r;
+  return result;
+}
+
 sw buf_inspect_fn_pattern (s_buf *buf, const s_list *pattern)
 {
   sw r;
@@ -737,6 +789,28 @@ sw buf_inspect_fn_pattern (s_buf *buf, const s_list *pattern)
   return result;
 }
 
+sw buf_inspect_fn_pattern_size (const s_list *pattern)
+{
+  sw r;
+  sw result = 0;
+  assert(buf);
+  r = strlen("(");
+  result += r;
+  while (pattern) {
+    if ((r = buf_inspect_tag_size(&pattern->tag)) < 0)
+      return r;
+    result += r;
+    pattern = list_next(pattern);
+    if (pattern) {
+      r = strlen(", ");
+      result += r;
+    }
+  }
+  r = strlen(")");
+  result += r;
+  return result;
+}
+
 sw buf_inspect_fn_size (const s_fn *fn)
 {
   sw r;
diff --git a/libc3/buf_inspect.h b/libc3/buf_inspect.h
index 8909a82..9a0a9b4 100644
--- a/libc3/buf_inspect.h
+++ b/libc3/buf_inspect.h
@@ -77,9 +77,11 @@ sw buf_inspect_fact_size (const s_fact *fact);
 sw buf_inspect_fact_spec (s_buf *buf, p_facts_spec spec);
 sw buf_inspect_fn (s_buf *buf, const s_fn *fn);
 sw buf_inspect_fn_algo (s_buf *buf, const s_list *algo);
+sw buf_inspect_fn_algo_size (const s_list *algo);
 sw buf_inspect_fn_clause (s_buf *buf, const s_fn *fn);
 sw buf_inspect_fn_clause_size (const s_fn *fn);
 sw buf_inspect_fn_pattern (s_buf *buf, const s_list *pattern);
+sw buf_inspect_fn_pattern_size (const s_list *pattern);
 sw buf_inspect_fn_size (const s_fn *fn);
 sw buf_inspect_ident (s_buf *buf, const s_ident *ident);
 sw buf_inspect_ident_reserved (s_buf *buf, const s_ident *ident);