Commit 29ab55a80e27af3c2aa4e6cfaae91f66d468754e

Thomas de Grivel 2025-03-31T16:35:22

buf_inspect_integer_hexadecimal

diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index 063cd6e..4d0ba80 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -2454,8 +2454,13 @@ sw buf_inspect_ident_sym_size (s_pretty *pretty, const s_sym *sym)
 
 sw buf_inspect_integer (s_buf *buf, const s_integer *x)
 {
+  return buf_inspect_integer_decimal(buf, x);
+}
+
+sw buf_inspect_integer_decimal (s_buf *buf, const s_integer *x)
+{
   s_buf buf_tmp;
-  mp_digit d;
+  mp_digit digit;
   sw result = 0;
   size_t maxlen;
   u8 p;
@@ -2477,9 +2482,9 @@ sw buf_inspect_integer (s_buf *buf, const s_integer *x)
   }
   buf_init_alloc(&buf_tmp, maxlen);
   while (! MP_IS_ZERO(&t)) {
-    if (mp_div_d(&t, radix, &t, &d) != MP_OKAY)
+    if (mp_div_d(&t, radix, &t, &digit) != MP_OKAY)
       goto error;
-    p = '0' + d;
+    p = '0' + digit;
     if (p > '9')
       goto error;
     buf_write_character_utf8(&buf_tmp, p);
@@ -2495,6 +2500,51 @@ sw buf_inspect_integer (s_buf *buf, const s_integer *x)
   return -1;
 }
 
+sw buf_inspect_integer_hexadecimal (s_buf *buf, const s_integer *x)
+{
+  s_buf buf_tmp;
+  mp_digit digit;
+  sw result = 0;
+  size_t maxlen;
+  u8 p;
+  const mp_digit radix = 16;
+  s32 size = 0;
+  mp_int t;
+  if (MP_IS_ZERO(&x->mp_int))
+    return buf_write_character_utf8(buf, '0');
+  if (mp_radix_size(&x->mp_int, radix, &size) != MP_OKAY)
+    return -1;
+  maxlen = size;
+  if (mp_init_copy(&t, &x->mp_int) != MP_OKAY)
+    return -1;
+  if (t.sign == MP_NEG) {
+    t.sign = MP_ZPOS;
+    maxlen--;
+    buf_write_character_utf8(buf, '-');
+    result++;
+  }
+  buf_init_alloc(&buf_tmp, maxlen);
+  while (! MP_IS_ZERO(&t)) {
+    if (mp_div_d(&t, radix, &t, &digit) != MP_OKAY ||
+        digit > radix)
+      goto error;
+    if (digit <= 9)
+      p = '0' + digit;
+    else
+      p = 'a' + digit - 10;
+    buf_write_character_utf8(&buf_tmp, p);
+    result++;
+  }
+  buf_xfer_reverse(&buf_tmp, buf);
+  mp_clear(&t);
+  buf_clean(&buf_tmp);
+  return result;
+ error:
+  mp_clear(&t);
+  buf_clean(&buf_tmp);
+  return -1;
+}
+
 sw buf_inspect_integer_size (s_pretty *pretty, const s_integer *x)
 {
   const mp_digit radix = 10;
diff --git a/libkc3/buf_inspect.h b/libkc3/buf_inspect.h
index eb4b329..968b90c 100644
--- a/libkc3/buf_inspect.h
+++ b/libkc3/buf_inspect.h
@@ -142,6 +142,8 @@ sw buf_inspect_ident_sym_reserved_size (s_pretty *pretty,
                                         const s_sym *sym);
 sw buf_inspect_ident_sym_size (s_pretty *pretty, const s_sym *sym);
 sw buf_inspect_integer (s_buf *buf, const s_integer *x);
+sw buf_inspect_integer_decimal (s_buf *buf, const s_integer *x);
+sw buf_inspect_integer_hexadecimal (s_buf *buf, const s_integer *x);
 sw buf_inspect_integer_size (s_pretty *pretty, const s_integer *x);
 sw buf_inspect_list (s_buf *buf, const s_list * const *list);
 sw buf_inspect_list_paren (s_buf *buf, const s_list * const *list);
diff --git a/libkc3/buf_parse.c b/libkc3/buf_parse.c
index 065adad..713db83 100644
--- a/libkc3/buf_parse.c
+++ b/libkc3/buf_parse.c
@@ -3221,6 +3221,9 @@ sw buf_parse_ptr (s_buf *buf, u_ptr_w *dest)
     goto restore;
   result += r;
   if (! integer_is_zero(&i)) {
+    err_write_1("buf_parse_ptr: (Ptr) 0x");
+    err_inspect_integer_hexadecimal(&i);
+    err_write_1("\n");
     integer_clean(&i);
     goto restore;
   }
diff --git a/libkc3/io.c b/libkc3/io.c
index 06a8e37..33b0c38 100644
--- a/libkc3/io.c
+++ b/libkc3/io.c
@@ -221,55 +221,58 @@ sw io_write_u8 (u8 x)
   return r;
 }
 
-DEF_ERR_IO_INSPECT(array,           const s_array *)
-DEF_ERR_IO_INSPECT(block,           const s_block *)
-DEF_ERR_IO_INSPECT(call,            const s_call *)
-DEF_ERR_IO_INSPECT(character,       const character *)
-DEF_ERR_IO_INSPECT(f32,             const f32 *)
-DEF_ERR_IO_INSPECT(fact,            const s_fact *)
-DEF_ERR_IO_INSPECT(facts_spec,      const p_facts_spec)
-DEF_ERR_IO_INSPECT(fn,              const s_fn *)
-DEF_ERR_IO_INSPECT(fn_clause,       const s_fn_clause *)
-DEF_ERR_IO_INSPECT(fn_pattern,      const s_list *)
-DEF_ERR_IO_INSPECT(frame,           const s_frame *)
-DEF_ERR_IO_INSPECT(ident,           const s_ident *)
-DEF_ERR_IO_INSPECT(map,             const s_map *)
-DEF_ERR_IO_INSPECT(pointer,         const void *)
-DEF_ERR_IO_INSPECT(ptr,             const u_ptr_w *)
-DEF_ERR_IO_INSPECT(s8,              const s8 *)
-DEF_ERR_IO_INSPECT(s8_decimal,      const s8 *)
-DEF_ERR_IO_INSPECT(s8_hexadecimal,  const s8 *)
-DEF_ERR_IO_INSPECT(s16,             const s16 *)
-DEF_ERR_IO_INSPECT(s16_decimal,     const s16 *)
-DEF_ERR_IO_INSPECT(s16_hexadecimal, const s16 *)
-DEF_ERR_IO_INSPECT(s32,             const s32 *)
-DEF_ERR_IO_INSPECT(s32_decimal,     const s32 *)
-DEF_ERR_IO_INSPECT(s32_hexadecimal, const s32 *)
-DEF_ERR_IO_INSPECT(s64,             const s64 *)
-DEF_ERR_IO_INSPECT(s64_decimal,     const s64 *)
-DEF_ERR_IO_INSPECT(s64_hexadecimal, const s64 *)
-DEF_ERR_IO_INSPECT(stacktrace,      const s_list *)
-DEF_ERR_IO_INSPECT(str,             const s_str *)
-DEF_ERR_IO_INSPECT(struct,          const s_struct *)
-DEF_ERR_IO_INSPECT(struct_type,     const s_struct_type *)
-DEF_ERR_IO_INSPECT(sw,              const sw *)
-DEF_ERR_IO_INSPECT(sw_decimal,      const sw *)
-DEF_ERR_IO_INSPECT(sw_hexadecimal,  const sw *)
-DEF_ERR_IO_INSPECT(sym,             const s_sym * const *)
-DEF_ERR_IO_INSPECT(tag,             const s_tag *)
-DEF_ERR_IO_INSPECT(tuple,           const s_tuple *)
-DEF_ERR_IO_INSPECT(u8,              const u8 *)
-DEF_ERR_IO_INSPECT(u8_decimal,      const u8 *)
-DEF_ERR_IO_INSPECT(u8_hexadecimal,  const u8 *)
-DEF_ERR_IO_INSPECT(u16,             const u16 *)
-DEF_ERR_IO_INSPECT(u16_decimal,     const u16 *)
-DEF_ERR_IO_INSPECT(u16_hexadecimal, const u16 *)
-DEF_ERR_IO_INSPECT(u32,             const u32 *)
-DEF_ERR_IO_INSPECT(u32_decimal,     const u32 *)
-DEF_ERR_IO_INSPECT(u32_hexadecimal, const u32 *)
-DEF_ERR_IO_INSPECT(u64,             const u64 *)
-DEF_ERR_IO_INSPECT(u64_decimal,     const u64 *)
-DEF_ERR_IO_INSPECT(u64_hexadecimal, const u64 *)
-DEF_ERR_IO_INSPECT(uw,              const uw *)
-DEF_ERR_IO_INSPECT(uw_decimal,      const uw *)
-DEF_ERR_IO_INSPECT(uw_hexadecimal,  const uw *)
+DEF_ERR_IO_INSPECT(array,               const s_array *)
+DEF_ERR_IO_INSPECT(block,               const s_block *)
+DEF_ERR_IO_INSPECT(call,                const s_call *)
+DEF_ERR_IO_INSPECT(character,           const character *)
+DEF_ERR_IO_INSPECT(f32,                 const f32 *)
+DEF_ERR_IO_INSPECT(fact,                const s_fact *)
+DEF_ERR_IO_INSPECT(facts_spec,          const p_facts_spec)
+DEF_ERR_IO_INSPECT(fn,                  const s_fn *)
+DEF_ERR_IO_INSPECT(fn_clause,           const s_fn_clause *)
+DEF_ERR_IO_INSPECT(fn_pattern,          const s_list *)
+DEF_ERR_IO_INSPECT(frame,               const s_frame *)
+DEF_ERR_IO_INSPECT(ident,               const s_ident *)
+DEF_ERR_IO_INSPECT(integer,             const s_integer *)
+DEF_ERR_IO_INSPECT(integer_decimal,     const s_integer *)
+DEF_ERR_IO_INSPECT(integer_hexadecimal, const s_integer *)
+DEF_ERR_IO_INSPECT(map,                 const s_map *)
+DEF_ERR_IO_INSPECT(pointer,             const void *)
+DEF_ERR_IO_INSPECT(ptr,                 const u_ptr_w *)
+DEF_ERR_IO_INSPECT(s8,                  const s8 *)
+DEF_ERR_IO_INSPECT(s8_decimal,          const s8 *)
+DEF_ERR_IO_INSPECT(s8_hexadecimal,      const s8 *)
+DEF_ERR_IO_INSPECT(s16,                 const s16 *)
+DEF_ERR_IO_INSPECT(s16_decimal,         const s16 *)
+DEF_ERR_IO_INSPECT(s16_hexadecimal,     const s16 *)
+DEF_ERR_IO_INSPECT(s32,                 const s32 *)
+DEF_ERR_IO_INSPECT(s32_decimal,         const s32 *)
+DEF_ERR_IO_INSPECT(s32_hexadecimal,     const s32 *)
+DEF_ERR_IO_INSPECT(s64,                 const s64 *)
+DEF_ERR_IO_INSPECT(s64_decimal,         const s64 *)
+DEF_ERR_IO_INSPECT(s64_hexadecimal,     const s64 *)
+DEF_ERR_IO_INSPECT(stacktrace,          const s_list *)
+DEF_ERR_IO_INSPECT(str,                 const s_str *)
+DEF_ERR_IO_INSPECT(struct,              const s_struct *)
+DEF_ERR_IO_INSPECT(struct_type,         const s_struct_type *)
+DEF_ERR_IO_INSPECT(sw,                  const sw *)
+DEF_ERR_IO_INSPECT(sw_decimal,          const sw *)
+DEF_ERR_IO_INSPECT(sw_hexadecimal,      const sw *)
+DEF_ERR_IO_INSPECT(sym,                 const s_sym * const *)
+DEF_ERR_IO_INSPECT(tag,                 const s_tag *)
+DEF_ERR_IO_INSPECT(tuple,               const s_tuple *)
+DEF_ERR_IO_INSPECT(u8,                  const u8 *)
+DEF_ERR_IO_INSPECT(u8_decimal,          const u8 *)
+DEF_ERR_IO_INSPECT(u8_hexadecimal,      const u8 *)
+DEF_ERR_IO_INSPECT(u16,                 const u16 *)
+DEF_ERR_IO_INSPECT(u16_decimal,         const u16 *)
+DEF_ERR_IO_INSPECT(u16_hexadecimal,     const u16 *)
+DEF_ERR_IO_INSPECT(u32,                 const u32 *)
+DEF_ERR_IO_INSPECT(u32_decimal,         const u32 *)
+DEF_ERR_IO_INSPECT(u32_hexadecimal,     const u32 *)
+DEF_ERR_IO_INSPECT(u64,                 const u64 *)
+DEF_ERR_IO_INSPECT(u64_decimal,         const u64 *)
+DEF_ERR_IO_INSPECT(u64_hexadecimal,     const u64 *)
+DEF_ERR_IO_INSPECT(uw,                  const uw *)
+DEF_ERR_IO_INSPECT(uw_decimal,          const uw *)
+DEF_ERR_IO_INSPECT(uw_hexadecimal,      const uw *)
diff --git a/libkc3/io.h b/libkc3/io.h
index a850e62..4fefb0d 100644
--- a/libkc3/io.h
+++ b/libkc3/io.h
@@ -45,64 +45,66 @@ sw io_write_1 (const char *x);
 sw io_write_str (const s_str *x);
 sw io_write_u8 (u8 x);
 
-PROTOTYPES_ERR_IO_INSPECT(array,           const s_array *);
-PROTOTYPES_ERR_IO_INSPECT(block,           const s_block *);
-PROTOTYPES_ERR_IO_INSPECT(bool,            const bool *);
-PROTOTYPES_ERR_IO_INSPECT(buf,             const s_buf *);
-PROTOTYPES_ERR_IO_INSPECT(call,            const s_call *);
-PROTOTYPES_ERR_IO_INSPECT(cfn,             const s_cfn *);
-PROTOTYPES_ERR_IO_INSPECT(character,       const character *);
-PROTOTYPES_ERR_IO_INSPECT(f32,             const f32 *);
-PROTOTYPES_ERR_IO_INSPECT(f64,             const f64 *);
-PROTOTYPES_ERR_IO_INSPECT(fact,            const s_fact *);
-PROTOTYPES_ERR_IO_INSPECT(facts_spec,      const p_facts_spec);
-PROTOTYPES_ERR_IO_INSPECT(fn,              const s_fn *);
-PROTOTYPES_ERR_IO_INSPECT(fn_clause,       const s_fn_clause *);
-PROTOTYPES_ERR_IO_INSPECT(fn_pattern,      const s_list *);
-PROTOTYPES_ERR_IO_INSPECT(frame,           const s_frame *);
-PROTOTYPES_ERR_IO_INSPECT(ident,           const s_ident *);
-PROTOTYPES_ERR_IO_INSPECT(integer,         const s_integer *);
-PROTOTYPES_ERR_IO_INSPECT(list,            const s_list *);
-PROTOTYPES_ERR_IO_INSPECT(map,             const s_map *);
-PROTOTYPES_ERR_IO_INSPECT(pointer,         const void *);
-PROTOTYPES_ERR_IO_INSPECT(ptr,             const u_ptr_w *);
-PROTOTYPES_ERR_IO_INSPECT(s8,              const s8 *);
-PROTOTYPES_ERR_IO_INSPECT(s8_decimal,      const s8 *);
-PROTOTYPES_ERR_IO_INSPECT(s8_hexadecimal,  const s8 *);
-PROTOTYPES_ERR_IO_INSPECT(s16,             const s16 *);
-PROTOTYPES_ERR_IO_INSPECT(s16_decimal,     const s16 *);
-PROTOTYPES_ERR_IO_INSPECT(s16_hexadecimal, const s16 *);
-PROTOTYPES_ERR_IO_INSPECT(s32,             const s32 *);
-PROTOTYPES_ERR_IO_INSPECT(s32_decimal,     const s32 *);
-PROTOTYPES_ERR_IO_INSPECT(s32_hexadecimal, const s32 *);
-PROTOTYPES_ERR_IO_INSPECT(s64,             const s64 *);
-PROTOTYPES_ERR_IO_INSPECT(s64_decimal,     const s64 *);
-PROTOTYPES_ERR_IO_INSPECT(s64_hexadecimal, const s64 *);
-PROTOTYPES_ERR_IO_INSPECT(stacktrace,      const s_list *);
-PROTOTYPES_ERR_IO_INSPECT(str,             const s_str *);
-PROTOTYPES_ERR_IO_INSPECT(struct,          const s_struct *);
-PROTOTYPES_ERR_IO_INSPECT(struct_type,     const s_struct_type *);
-PROTOTYPES_ERR_IO_INSPECT(sw,              const sw *);
-PROTOTYPES_ERR_IO_INSPECT(sw_decimal,      const sw *);
-PROTOTYPES_ERR_IO_INSPECT(sw_hexadecimal,  const sw *);
-PROTOTYPES_ERR_IO_INSPECT(sym,             const s_sym * const *);
-PROTOTYPES_ERR_IO_INSPECT(tag,             const s_tag *);
-PROTOTYPES_ERR_IO_INSPECT(tag_type,        e_tag_type);
-PROTOTYPES_ERR_IO_INSPECT(tuple,           const s_tuple *);
-PROTOTYPES_ERR_IO_INSPECT(u8,              const u8 *);
-PROTOTYPES_ERR_IO_INSPECT(u8_decimal,      const u8 *);
-PROTOTYPES_ERR_IO_INSPECT(u8_hexadecimal,  const u8 *);
-PROTOTYPES_ERR_IO_INSPECT(u16,             const u16 *);
-PROTOTYPES_ERR_IO_INSPECT(u16_decimal,     const u16 *);
-PROTOTYPES_ERR_IO_INSPECT(u16_hexadecimal, const u16 *);
-PROTOTYPES_ERR_IO_INSPECT(u32,             const u32 *);
-PROTOTYPES_ERR_IO_INSPECT(u32_decimal,     const u32 *);
-PROTOTYPES_ERR_IO_INSPECT(u32_hexadecimal, const u32 *);
-PROTOTYPES_ERR_IO_INSPECT(u64,             const u64 *);
-PROTOTYPES_ERR_IO_INSPECT(u64_decimal,     const u64 *);
-PROTOTYPES_ERR_IO_INSPECT(u64_hexadecimal, const u64 *);
-PROTOTYPES_ERR_IO_INSPECT(uw,              const uw *);
-PROTOTYPES_ERR_IO_INSPECT(uw_decimal,      const uw *);
-PROTOTYPES_ERR_IO_INSPECT(uw_hexadecimal,  const uw *);
+PROTOTYPES_ERR_IO_INSPECT(array,               const s_array *);
+PROTOTYPES_ERR_IO_INSPECT(block,               const s_block *);
+PROTOTYPES_ERR_IO_INSPECT(bool,                const bool *);
+PROTOTYPES_ERR_IO_INSPECT(buf,                 const s_buf *);
+PROTOTYPES_ERR_IO_INSPECT(call,                const s_call *);
+PROTOTYPES_ERR_IO_INSPECT(cfn,                 const s_cfn *);
+PROTOTYPES_ERR_IO_INSPECT(character,           const character *);
+PROTOTYPES_ERR_IO_INSPECT(f32,                 const f32 *);
+PROTOTYPES_ERR_IO_INSPECT(f64,                 const f64 *);
+PROTOTYPES_ERR_IO_INSPECT(fact,                const s_fact *);
+PROTOTYPES_ERR_IO_INSPECT(facts_spec,          const p_facts_spec);
+PROTOTYPES_ERR_IO_INSPECT(fn,                  const s_fn *);
+PROTOTYPES_ERR_IO_INSPECT(fn_clause,           const s_fn_clause *);
+PROTOTYPES_ERR_IO_INSPECT(fn_pattern,          const s_list *);
+PROTOTYPES_ERR_IO_INSPECT(frame,               const s_frame *);
+PROTOTYPES_ERR_IO_INSPECT(ident,               const s_ident *);
+PROTOTYPES_ERR_IO_INSPECT(integer,             const s_integer *);
+PROTOTYPES_ERR_IO_INSPECT(integer_decimal,     const s_integer *);
+PROTOTYPES_ERR_IO_INSPECT(integer_hexadecimal, const s_integer *);
+PROTOTYPES_ERR_IO_INSPECT(list,                const s_list *);
+PROTOTYPES_ERR_IO_INSPECT(map,                 const s_map *);
+PROTOTYPES_ERR_IO_INSPECT(pointer,             const void *);
+PROTOTYPES_ERR_IO_INSPECT(ptr,                 const u_ptr_w *);
+PROTOTYPES_ERR_IO_INSPECT(s8,                  const s8 *);
+PROTOTYPES_ERR_IO_INSPECT(s8_decimal,          const s8 *);
+PROTOTYPES_ERR_IO_INSPECT(s8_hexadecimal,      const s8 *);
+PROTOTYPES_ERR_IO_INSPECT(s16,                 const s16 *);
+PROTOTYPES_ERR_IO_INSPECT(s16_decimal,         const s16 *);
+PROTOTYPES_ERR_IO_INSPECT(s16_hexadecimal,     const s16 *);
+PROTOTYPES_ERR_IO_INSPECT(s32,                 const s32 *);
+PROTOTYPES_ERR_IO_INSPECT(s32_decimal,         const s32 *);
+PROTOTYPES_ERR_IO_INSPECT(s32_hexadecimal,     const s32 *);
+PROTOTYPES_ERR_IO_INSPECT(s64,                 const s64 *);
+PROTOTYPES_ERR_IO_INSPECT(s64_decimal,         const s64 *);
+PROTOTYPES_ERR_IO_INSPECT(s64_hexadecimal,     const s64 *);
+PROTOTYPES_ERR_IO_INSPECT(stacktrace,          const s_list *);
+PROTOTYPES_ERR_IO_INSPECT(str,                 const s_str *);
+PROTOTYPES_ERR_IO_INSPECT(struct,              const s_struct *);
+PROTOTYPES_ERR_IO_INSPECT(struct_type,         const s_struct_type *);
+PROTOTYPES_ERR_IO_INSPECT(sw,                  const sw *);
+PROTOTYPES_ERR_IO_INSPECT(sw_decimal,          const sw *);
+PROTOTYPES_ERR_IO_INSPECT(sw_hexadecimal,      const sw *);
+PROTOTYPES_ERR_IO_INSPECT(sym,                 const s_sym * const *);
+PROTOTYPES_ERR_IO_INSPECT(tag,                 const s_tag *);
+PROTOTYPES_ERR_IO_INSPECT(tag_type,            e_tag_type);
+PROTOTYPES_ERR_IO_INSPECT(tuple,               const s_tuple *);
+PROTOTYPES_ERR_IO_INSPECT(u8,                  const u8 *);
+PROTOTYPES_ERR_IO_INSPECT(u8_decimal,          const u8 *);
+PROTOTYPES_ERR_IO_INSPECT(u8_hexadecimal,      const u8 *);
+PROTOTYPES_ERR_IO_INSPECT(u16,                 const u16 *);
+PROTOTYPES_ERR_IO_INSPECT(u16_decimal,         const u16 *);
+PROTOTYPES_ERR_IO_INSPECT(u16_hexadecimal,     const u16 *);
+PROTOTYPES_ERR_IO_INSPECT(u32,                 const u32 *);
+PROTOTYPES_ERR_IO_INSPECT(u32_decimal,         const u32 *);
+PROTOTYPES_ERR_IO_INSPECT(u32_hexadecimal,     const u32 *);
+PROTOTYPES_ERR_IO_INSPECT(u64,                 const u64 *);
+PROTOTYPES_ERR_IO_INSPECT(u64_decimal,         const u64 *);
+PROTOTYPES_ERR_IO_INSPECT(u64_hexadecimal,     const u64 *);
+PROTOTYPES_ERR_IO_INSPECT(uw,                  const uw *);
+PROTOTYPES_ERR_IO_INSPECT(uw_decimal,          const uw *);
+PROTOTYPES_ERR_IO_INSPECT(uw_hexadecimal,      const uw *);
 
 #endif /* LIBKC3_IO_H */