Hello,
Because decbin is limited to 31 bit values, I thought that the gmp
extension might be expanded a bit: If you add an optional base argument
to gmp, then we actually have a <large number>-bit decbin. I think that
would be nice.
Proposed patch below.
(The patch is for php 4.0.6; I can easily make one for php-cvs if
wanted.)
diff -u php-4.0.6-orig/ext/gmp/gmp.c php-4.0.6/ext/gmp/gmp.c
--- php-4.0.6-orig/ext/gmp/gmp.c Thu May 24 14:41:52 2001
+++ php-4.0.6/ext/gmp/gmp.c Mon Jul 30 22:01:55 2001
@@ -160,7 +160,7 @@
if(Z_TYPE_PP(zval) == IS_RESOURCE) { \
ZEND_FETCH_RESOURCE(gmpnumber, mpz_t *, zval, -1, GMP_RESOURCE_NAME, le_gmp);\
} else {\
- if(convert_to_gmp(&gmpnumber,zval) == FAILURE) {\
+ if(convert_to_gmp(&gmpnumber,zval,0) == FAILURE) {\
RETURN_FALSE;\
}\
ZEND_REGISTER_RESOURCE(NULL, gmpnumber, le_gmp);\
@@ -170,8 +170,8 @@
#define INIT_GMP_NUM(gmpnumber) { gmpnumber=emalloc(sizeof(mpz_t));
mpz_init(*gmpnumber); }
#define FREE_GMP_NUM(gmpnumber) { mpz_clear(*gmpnumber); efree(gmpnumber); }
-/* Convert zval to be gmp number */
-static int convert_to_gmp(mpz_t * *gmpnumber, zval **val)
+/* Convert string to be gmp number */
+static int convert_to_gmp(mpz_t * *gmpnumber, zval **val, int base)
{
int ret = 0;
@@ -188,11 +188,14 @@
case IS_STRING:
{
char *numstr = Z_STRVAL_PP(val);
- if(numstr[0] == '0' && (numstr[1] == 'x' || numstr[1] == 'X'))
{
- ret = mpz_init_set_str(**gmpnumber, numstr+2, 16);
- } else {
- ret = mpz_init_set_str(**gmpnumber, numstr, 10);
+ if (base==0) {
+ if(numstr[0] == '0' && (numstr[1] == 'x' || numstr[1]
+== 'X')) {
+ base=16;
+ } else {
+ base=10;
+ }
}
+ ret = mpz_init_set_str(**gmpnumber, numstr, base);
}
break;
default:
@@ -391,22 +394,31 @@
}
-/* Remove the following function when you have succesfully modified config.m4
- so that your module can be compiled into PHP, it exists only for testing
- purposes. */
-
-/* {{{ proto resource gmp_init(mixed number)
+/* {{{ proto resource gmp_init(mixed number [, int base ])
Initializes GMP number */
ZEND_FUNCTION(gmp_init)
{
- zval **number_arg;
+ zval **number_arg, **base_arg;
mpz_t * gmpnumber;
+ int argc;
+ int base=0;
- if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &number_arg) == FAILURE){
+ argc = ZEND_NUM_ARGS();
+ if (argc < 1 || argc > 2 || zend_get_parameters_ex(argc, &number_arg,
+&base_arg) == FAILURE){
WRONG_PARAM_COUNT;
}
- if(convert_to_gmp(&gmpnumber,number_arg) == FAILURE) {
+ if (argc==2) {
+ convert_to_long_ex(base_arg);
+ base = Z_LVAL_PP(base_arg);
+ }
+
+ if(base < 2 || base > 36) {
+ zend_error(E_WARNING, "Bad base for conversion: %d (should be between
+2 and 36)", base);
+ RETURN_FALSE;
+ }
+
+ if(convert_to_gmp(&gmpnumber,number_arg,base) == FAILURE) {
RETURN_FALSE;
}
--
Greetings from Troels Arvin, Copenhagen, Denmark
--
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]