mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-20 11:40:18 +02:00
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.
This commit is contained in:
parent
fc4228c196
commit
fc36cd610d
3 changed files with 35 additions and 21 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue