Commit 0fa2bfffcc0c9cf2fc83d97a6e52ef444facdb0b

Thomas de Grivel 2024-03-07T08:20:09

remove buf_to_ratio, fix ratio_init_integer

diff --git a/libc3/buf.c b/libc3/buf.c
index 98b3a41..36e848b 100644
--- a/libc3/buf.c
+++ b/libc3/buf.c
@@ -891,18 +891,6 @@ s_str * buf_to_str (const s_buf *buf, s_str *str)
   return str_init(str, p_free, buf->size, buf->ptr.p);
 }
 
-s_ratio * buf_to_ratio (const s_buf *buf, s_ratio *ratio)
-{
-  assert(buf);
-  assert(ratio);
-  if (buf->size < 2 * sizeof(s_integer)) {
-    return NULL;
-  }
-  s_integer *numerator = (s_integer *)buf->ptr.p;
-  s_integer *denominator = ((s_integer *)buf->ptr.p) + 1;
-  return ratio_init_integer(ratio, numerator, denominator);
-}
-
 sw buf_u8_to_hex (s_buf *buf, const u8 *x)
 {
   u8 digit;
diff --git a/libc3/ratio.c b/libc3/ratio.c
index 51b91e1..b162fe9 100644
--- a/libc3/ratio.c
+++ b/libc3/ratio.c
@@ -162,20 +162,14 @@ s_ratio * ratio_init_copy (s_ratio *dest, const s_ratio *src)
   return dest;
 }
 
-s_ratio * ratio_init_integer (s_ratio *r, s_integer *numerator, s_integer *denominator)
+s_ratio * ratio_init_integer (s_ratio *r, const s_integer *src)
 {
   s_ratio tmp = {0};
-  assert(numerator);
-  assert(denominator);
   assert(r);
-  if (! integer_is_positive(denominator)) {
-    err_puts("ratio_init_integer: invalid denominator");
-    assert(! "ratio_init_integer: invalid denominator");
-    return NULL;
-  }
-  if (! integer_init_copy(&tmp.numerator, numerator))
+  assert(src);
+  if (! integer_init_copy(&tmp.numerator, src))
     return NULL;
-  if (! integer_init_copy(&tmp.denominator, denominator)) {
+  if (! integer_init_u8(&tmp.denominator, 1)) {
     integer_clean(&tmp.numerator);
     return NULL;
   }
diff --git a/libc3/ratio.h b/libc3/ratio.h
index 88a08b2..6ca3822 100644
--- a/libc3/ratio.h
+++ b/libc3/ratio.h
@@ -20,17 +20,30 @@
 /* Stack-allocation compatible functions, call ratio_clean after use. */
 void      ratio_clean (s_ratio *r);
 s_ratio * ratio_init (s_ratio *dest);
-s_ratio * ratio_init_integer (s_ratio *r, s_integer *numerator,
-                              s_integer *denominator);
+s_ratio * ratio_init_1 (s_ratio *r, const char *p);
+s_ratio * ratio_init_cast (s_ratio *r, const s_tag *src);
+s_ratio * ratio_init_copy (s_ratio *r, const s_ratio *src);
+s_ratio * ratio_init_f32 (s_ratio *r, f32 src);
+s_ratio * ratio_init_f64 (s_ratio *r, f64 src);
+s_ratio * ratio_init_f128 (s_ratio *r, f128 src);
+s_ratio * ratio_init_integer (s_ratio *r, const s_integer *src);
+s_ratio * ratio_init_ratio (s_ratio *r, const s_ratio *src);
+s_ratio * ratio_init_s8 (s_ratio *r, s8 src);
+s_ratio * ratio_init_s16 (s_ratio *r, s16 src);
+s_ratio * ratio_init_s32 (s_ratio *r, s32 src);
+s_ratio * ratio_init_s64 (s_ratio *r, s64 src);
+s_ratio * ratio_init_sw (s_ratio *r, sw src);
+s_ratio * ratio_init_u8 (s_ratio *r, u8 src);
+s_ratio * ratio_init_u16 (s_ratio *r, u16 src);
+s_ratio * ratio_init_u32 (s_ratio *r, u32 src);
+s_ratio * ratio_init_u64 (s_ratio *r, u64 src);
+s_ratio * ratio_init_uw (s_ratio *r, uw src);
 s_ratio * ratio_init_zero (s_ratio *r);
-s_ratio * ratio_init_1 (s_ratio *q, const char *p);
-s_ratio * ratio_init_copy (s_ratio *a, const s_ratio *x);
-s_ratio * ratio_init_f32 (s_ratio *a, f32 x);
-s_ratio * ratio_init_f64 (s_ratio *a, f64 x);
 
-/* Constructors, call ratio_delete after use. */
+/* Heap-allocation functions, call ratio_delete after use. */
+void      ratio_delete (s_ratio *r);
 s_ratio * ratio_new (void);
-s_ratio * ratio_new_copy (const s_ratio *a);
+s_ratio * ratio_new_copy (const s_ratio *src);
 
 /* Observers. */
 s_str * ratio_inspect (const s_ratio *src, s_str *dest);