guile-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Guile-commits] 35/69: Implement integer-to-string with new integer libr


From: Andy Wingo
Subject: [Guile-commits] 35/69: Implement integer-to-string with new integer library
Date: Fri, 7 Jan 2022 08:27:11 -0500 (EST)

wingo pushed a commit to branch wip-inline-digits
in repository guile.

commit 67f782b969972d510b2f3f69df4b0ff94a64fe52
Author: Andy Wingo <wingo@pobox.com>
AuthorDate: Tue Jan 4 10:59:01 2022 +0100

    Implement integer-to-string with new integer library
    
    * libguile/integers.c (scm_integer_to_string_i)
    (scm_integer_to_string_z): New internal functions.
    * libguile/integers.h: Declare the new internal functions.
    * libguile/numbers.c (scm_number_to_string): Use new internal
    functions.
---
 libguile/integers.c | 26 ++++++++++++++++++++++++++
 libguile/integers.h |  3 +++
 libguile/numbers.c  | 27 ++++++---------------------
 3 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/libguile/integers.c b/libguile/integers.c
index fafef184f..d955ec4bf 100644
--- a/libguile/integers.c
+++ b/libguile/integers.c
@@ -25,10 +25,12 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 #include <verify.h>
 
 #include "boolean.h"
 #include "numbers.h"
+#include "strings.h"
 
 #include "integers.h"
 
@@ -2327,3 +2329,27 @@ scm_integer_length_z (struct scm_bignum *n)
   scm_remember_upto_here_1 (n);
   return scm_from_size_t (size);
 }
+
+SCM
+scm_integer_to_string_i (scm_t_inum n, int base)
+{
+  // FIXME: Use mpn_get_str instead.
+  char num_buf [SCM_INTBUFLEN];
+  size_t length = scm_iint2str (n, base, num_buf);
+  return scm_from_latin1_stringn (num_buf, length);
+}
+
+SCM
+scm_integer_to_string_z (struct scm_bignum *n, int base)
+{
+  mpz_t zn;
+  alias_bignum_to_mpz (n, zn);
+  char *str = mpz_get_str (NULL, base, zn);
+  scm_remember_upto_here_1 (n);
+  size_t len = strlen (str);
+  void (*freefunc) (void *, size_t);
+  mp_get_memory_functions (NULL, NULL, &freefunc);
+  SCM ret = scm_from_latin1_stringn (str, len);
+  freefunc (str, len + 1);
+  return ret;
+}
diff --git a/libguile/integers.h b/libguile/integers.h
index 5bc2c7650..8ac4ca55f 100644
--- a/libguile/integers.h
+++ b/libguile/integers.h
@@ -144,6 +144,9 @@ SCM_INTERNAL SCM scm_integer_logcount_z (struct scm_bignum 
*n);
 SCM_INTERNAL SCM scm_integer_length_i (scm_t_inum n);
 SCM_INTERNAL SCM scm_integer_length_z (struct scm_bignum *n);
 
+SCM_INTERNAL SCM scm_integer_to_string_i (scm_t_inum n, int base);
+SCM_INTERNAL SCM scm_integer_to_string_z (struct scm_bignum *n, int base);
+
 
 
 #endif  /* SCM_INTEGERS_H */
diff --git a/libguile/numbers.c b/libguile/numbers.c
index efc9ed06c..1b6d29efa 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -3754,29 +3754,14 @@ SCM_DEFINE (scm_number_to_string, "number->string", 1, 
1, 0,
     base = scm_to_signed_integer (radix, 2, 36);
 
   if (SCM_I_INUMP (n))
-    {
-      char num_buf [SCM_INTBUFLEN];
-      size_t length = scm_iint2str (SCM_I_INUM (n), base, num_buf);
-      return scm_from_latin1_stringn (num_buf, length);
-    }
+    return scm_integer_to_string_i (SCM_I_INUM (n), base);
   else if (SCM_BIGP (n))
-    {
-      char *str = mpz_get_str (NULL, base, SCM_I_BIG_MPZ (n));
-      size_t len = strlen (str);
-      void (*freefunc) (void *, size_t);
-      SCM ret;
-      mp_get_memory_functions (NULL, NULL, &freefunc);
-      scm_remember_upto_here_1 (n);
-      ret = scm_from_latin1_stringn (str, len);
-      freefunc (str, len + 1);
-      return ret;
-    }
+    return scm_integer_to_string_z (scm_bignum (n), base);
   else if (SCM_FRACTIONP (n))
-    {
-      return scm_string_append (scm_list_3 (scm_number_to_string 
(SCM_FRACTION_NUMERATOR (n), radix),
-                                           scm_from_latin1_string ("/"),
-                                           scm_number_to_string 
(SCM_FRACTION_DENOMINATOR (n), radix)));
-    }
+    return scm_string_append
+      (scm_list_3 (scm_number_to_string (SCM_FRACTION_NUMERATOR (n), radix),
+                   scm_from_latin1_string ("/"),
+                   scm_number_to_string (SCM_FRACTION_DENOMINATOR (n), 
radix)));
   else if (SCM_INEXACTP (n))
     {
       char num_buf [FLOBUFLEN];



reply via email to

[Prev in Thread] Current Thread [Next in Thread]