Re: Re: gmp_init with a base argument

From: Date: Sun, 05 Aug 2001 11:44:06 +0000
Subject: Re: Re: gmp_init with a base argument
References: 1 2  Groups: php.dev 
Request: Send a blank email to php-dev+get-62136@lists.php.net to get a copy of this message
On Thu, 02 Aug 2001 19:16:26 +0200, "Stanislav Malyshev" <stas@zend.com> wrote: > on the quick glance your patch looks OK. OK. It seems that I don't have sufficient karma. I tried to commit the below patch with the following commit message: (gmp_init) Added extra (optional) argument to gmp_init(): a base argument which indicates the number base. E.g.: gmp_init('1010101010',2); // feed gmp a binary value. @- Added optional extra argument to gmp_init(). The extra argument @ indicates which number base gmp should use when converting a @ string to the gmp-number. (Troels) Suggested patch is attached below (the patch is against current CVS). /Troels --- php4-orig/ext/gmp/gmp.c Sun Aug 5 12:51:42 2001 +++ php4/ext/gmp/gmp.c Sun Aug 5 13:24:02 2001 @@ -178,7 +178,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);\ @@ -190,7 +190,7 @@ /* {{{ convert_to_gmp * Convert zval to be gmp number */ -static int convert_to_gmp(mpz_t * *gmpnumber, zval **val) +static int convert_to_gmp(mpz_t * *gmpnumber, zval **val, int base) { int ret = 0; @@ -207,11 +207,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: @@ -434,22 +437,30 @@ } /* }}} */ -/* 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; }

« previous php.dev (#62136) next »