Subject: | Base specification for Math::GMP::new() |
I am missing at least one (for me) important feature: the ability to specify a base when creating a new Math::GMP object. Our values are represented in base 36, which the gpm library is perfectly able to handle, but there is no way to specify it from Perl.
I tried to do the base36 to Math::GPM conversion in Perl, but that was painfully slow with a 200 char long value. Something on the order of 0.1 second. So I would like to suggest to add an optional "base" parameter to Math::GMP::new(), which just passes the value along to mpz_init_set_str(). This enables very, very fast conversion of all kinds of values (especially base36) to a Math::GMP object.
I have implemented this feature locally, but thought that it might be a valuable addition to the official version of Math::GMP. Of course this would also mean that I wouldn't have to maintain my own patches to Math::GMP :-)
Unfortunately I am no expert at XS coding, so I just created a new version of new_from_scalar(s) called new_from_scalar_with_base(s, b) and then handled the test on a "base" parameter in Math::GMP::new().
If you are interested, I have created the (very small) required diffs against Math-GMP-2.03
diff GMP.xs.orig GMP.xs
73a74,84
Show quoted text
> mpz_t *
> new_from_scalar_with_base(s, b)
> char * s
> int b
>
> CODE:
> RETVAL = malloc (sizeof(mpz_t));
> mpz_init_set_str(*RETVAL, s, b);
> OUTPUT:
> RETVAL
>
diff GMP.pm.orig GMP.pm
99c99,100
< my $ival = shift;
---
Show quoted text> my $ival = shift || 0;
> my $base = shift;
103c104,108
< $ival = 0 if $ival =~ /[^\d\-xA-Fa-f]/ || !$ival;
---
Show quoted text> my $ret;
> if ($base) {
> $ret = Math::GMP::new_from_scalar_with_base($ival, $base);
> } else {
> $ival = 0 if $ival =~ /[^\d\-xA-Fa-f]/;
104a110,111
Show quoted text> $ret = Math::GMP::new_from_scalar($ival);
> }
106,107d112
< my $ret = Math::GMP::new_from_scalar($ival);
<