[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 34/85: Implement integer-to-string with new integer libr
From: |
Andy Wingo |
Subject: |
[Guile-commits] 34/85: Implement integer-to-string with new integer library |
Date: |
Thu, 13 Jan 2022 03:40:18 -0500 (EST) |
wingo pushed a commit to branch main
in repository guile.
commit fc36cd610dc1822957afc9e441d81fc8b33d7e56
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];
- [Guile-commits] 16/85: Implement round-quotient with new integer lib, (continued)
- [Guile-commits] 16/85: Implement round-quotient with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 25/85: Implement scm_logbit_p with new integer library, Andy Wingo, 2022/01/13
- [Guile-commits] 14/85: Implement centered-remainder with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 10/85: Implement truncate-quotient with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 26/85: Implement scm_lognot with new integer library, Andy Wingo, 2022/01/13
- [Guile-commits] 12/85: Implement truncate-divide with new integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 29/85: Implement scm_ash with new integer library, Andy Wingo, 2022/01/13
- [Guile-commits] 31/85: Implement scm_logcount with new integer library, Andy Wingo, 2022/01/13
- [Guile-commits] 33/85: Implement scm_integer_length with new integer library, Andy Wingo, 2022/01/13
- [Guile-commits] 35/85: Simplify scm_bigprint, Andy Wingo, 2022/01/13
- [Guile-commits] 34/85: Implement integer-to-string with new integer library,
Andy Wingo <=
- [Guile-commits] 36/85: Build scm_integer_p on scm_is_integer, not vice versa, Andy Wingo, 2022/01/13
- [Guile-commits] 37/85: Reimplement = on integer lib, clean up scm_num_eq_p, Andy Wingo, 2022/01/13
- [Guile-commits] 40/85: Simplify implementation of min, max, Andy Wingo, 2022/01/13
- [Guile-commits] 42/85: Simplify scm_difference, use integer lib, Andy Wingo, 2022/01/13
- [Guile-commits] 47/85: Fix scm_integer_to_double_z to always round; clean ups, Andy Wingo, 2022/01/13
- [Guile-commits] 50/85: Reimplement scm_{to,from}_{int32,uint32}, Andy Wingo, 2022/01/13
- [Guile-commits] 67/85: scm_to_ipv6 uses scm_to_mpz, Andy Wingo, 2022/01/13
- [Guile-commits] 68/85: Bignums avoid both custom GMP allocator and finalizers, Andy Wingo, 2022/01/13
- [Guile-commits] 69/85: take_mpz optimization, Andy Wingo, 2022/01/13
- [Guile-commits] 71/85: Re-rewrite integer-expt in C, Andy Wingo, 2022/01/13