Commit 0966d20998c4e30acbf4650727f2f3fbda2fac5e

Thomas de Grivel 2024-07-30T18:13:28

s,u_init_1 and s,u_init_str

diff --git a/libkc3/s.c.in b/libkc3/s.c.in
index c0eeca4..f459a97 100644
--- a/libkc3/s.c.in
+++ b/libkc3/s.c.in
@@ -22,6 +22,13 @@
 #include "ratio.h"
 #include "s_bits$.h"
 
+s_bits$ * s_bits$_init_1 (s_bits$ *s, const char *p)
+{
+  s_str str;
+  str_init_1(&str, NULL, p);
+  return s_bits$_init_str(s, &str);
+}
+
 s_bits$ * s_bits$_init_cast
 (s_bits$ *s, const s_sym * const *type, const s_tag *tag)
 {
@@ -99,6 +106,20 @@ s_bits$ * s_bits$_init_copy (s_bits$ *s, const s_bits$ *src)
   return s;
 }
 
+s_bits$ * s_bits$_init_str (s_bits$ *s, const s_str *str)
+{
+  s_buf buf;
+  s_bits$ tmp = 0;
+  buf_init(&buf, false, str.size, str.ptr.pchar);
+  buf.wpos = str.size;
+  if ((r = buf_parse_s_bits$(&buf, &tmp)) <= 0) {
+    err_puts("s_bits$_init_str: buf_parse_s_bits$");
+    assert(! "s_bits$_init_str: buf_parse_s_bits$");
+    return NULL;
+  }
+  *s = tmp;
+  return s;
+}
 
 s_bits$ * s_bits$_random (s_bits$ *s)
 {
diff --git a/libkc3/s.h.in b/libkc3/s.h.in
index a297da3..cc33940 100644
--- a/libkc3/s.h.in
+++ b/libkc3/s.h.in
@@ -16,9 +16,11 @@
 
 #include "types.h"
 
+s_bits$ * s_bits$_init_1 (s_bits$ *s, const char *p);
 s_bits$ * s_bits$_init_cast
 (s_bits$ *s, const s_sym * const *type, const s_tag *tag);
 s_bits$ * s_bits$_init_copy (s_bits$ *s, const s_bits$ *src);
+s_bits$ * s_bits$_init_str (s_bits$ *s, const s_str *str);
 s_bits$ * s_bits$_random (s_bits$ *s);
 u_bits$ * s_bits$_random_uniform (s_bits$ *s, s_bits$ min, s_bits$ max);
 s_tag *   s_bits$_sqrt (const s_bits$ x, s_tag *dest);
diff --git a/libkc3/u.c.in b/libkc3/u.c.in
index 43f24b9..d656fc7 100644
--- a/libkc3/u.c.in
+++ b/libkc3/u.c.in
@@ -21,6 +21,13 @@
 #include "tag.h"
 #include "u_bits$.h"
 
+u_bits$ * u_bits$_init_1 (u_bits$ *u, const char *p)
+{
+  s_str str;
+  str_init_1(&str, NULL, p);
+  return u_bits$_init_str(u, &str);
+}
+
 u_bits$ * u_bits$_init_cast
 (u_bits$ *u, const s_sym * const *type, const s_tag *tag)
 {
@@ -98,6 +105,21 @@ u_bits$ * u_bits$_init_copy (u_bits$ *u, const u_bits$ *src)
   return u;
 }
 
+u_bits$ * u_bits$_init_str (u_bits$ *u, const s_str *str)
+{
+  s_buf buf;
+  u_bits$ tmp = 0;
+  buf_init(&buf, false, str.size, str.ptr.pchar);
+  buf.wpos = str.size;
+  if ((r = buf_parse_u_bits$(&buf, &tmp)) <= 0) {
+    err_puts("u_bits$_init_str: buf_parse_u_bits$");
+    assert(! "u_bits$_init_str: buf_parse_u_bits$");
+    return NULL;
+  }
+  *u = tmp;
+  return u;
+}
+
 u_bits$ * u_bits$_random (u_bits$ *u)
 {
   arc4random_buf(u, sizeof(u_bits$));
diff --git a/libkc3/u.h.in b/libkc3/u.h.in
index 449ae40..784c0c5 100644
--- a/libkc3/u.h.in
+++ b/libkc3/u.h.in
@@ -16,10 +16,12 @@
 
 #include "types.h"
 
+u_bits$ * u_bits$_init_1 (u_bits$ *u, const char *p);
 u_bits$ * u_bits$_init_cast
 (u_bits$ *u, const s_sym * const *type, const s_tag *tag);
 
 u_bits$ * u_bits$_init_copy (u_bits$ *u, const u_bits$ *src);
+u_bits$ * u_bits$_init_str (u_bits$ *u, const s_str *str);
 u_bits$ * u_bits$_random (u_bits$ *u);
 u_bits$ * u_bits$_random_uniform (u_bits$ *u, u_bits$ max);
 s_tag *   u_bits$_sqrt (const u_bits$ x, s_tag *dest);