diff options
author | Xinchen Hui <laruence@gmail.com> | 2014-05-05 14:26:23 +0800 |
---|---|---|
committer | Xinchen Hui <laruence@gmail.com> | 2014-05-05 14:26:23 +0800 |
commit | fee9d4cafd02cb37177179b216fb21baf7041d30 (patch) | |
tree | 28fea6dd63ecd9429d295435c716e798e204189e /ext/bcmath | |
parent | 7e3989f3014eba41fcdc44535723833029dc1ecf (diff) | |
download | php-git-fee9d4cafd02cb37177179b216fb21baf7041d30.tar.gz |
Refactor bcmath
Diffstat (limited to 'ext/bcmath')
-rw-r--r-- | ext/bcmath/bcmath.c | 35 | ||||
-rw-r--r-- | ext/bcmath/libbcmath/src/bcmath.h | 2 | ||||
-rw-r--r-- | ext/bcmath/libbcmath/src/num2str.c | 58 |
3 files changed, 40 insertions, 55 deletions
diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index 9369897c7f..4b8c7c5f18 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -230,9 +230,7 @@ PHP_FUNCTION(bcadd) result->n_scale = scale; } - Z_STRVAL_P(return_value) = bc_num2str(result); - Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); - Z_TYPE_P(return_value) = IS_STRING; + RETVAL_STR(bc_num2str(result)); bc_free_num(&first); bc_free_num(&second); bc_free_num(&result); @@ -269,9 +267,7 @@ PHP_FUNCTION(bcsub) result->n_scale = scale; } - Z_STRVAL_P(return_value) = bc_num2str(result); - Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); - Z_TYPE_P(return_value) = IS_STRING; + RETVAL_STR(bc_num2str(result)); bc_free_num(&first); bc_free_num(&second); bc_free_num(&result); @@ -308,9 +304,7 @@ PHP_FUNCTION(bcmul) result->n_scale = scale; } - Z_STRVAL_P(return_value) = bc_num2str(result); - Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); - Z_TYPE_P(return_value) = IS_STRING; + RETVAL_STR(bc_num2str(result)); bc_free_num(&first); bc_free_num(&second); bc_free_num(&result); @@ -347,9 +341,7 @@ PHP_FUNCTION(bcdiv) if (result->n_scale > scale) { result->n_scale = scale; } - Z_STRVAL_P(return_value) = bc_num2str(result); - Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); - Z_TYPE_P(return_value) = IS_STRING; + RETVAL_STR(bc_num2str(result)); break; case -1: /* division by zero */ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Division by zero"); @@ -383,9 +375,7 @@ PHP_FUNCTION(bcmod) switch (bc_modulo(first, second, &result, 0 TSRMLS_CC)) { case 0: - Z_STRVAL_P(return_value) = bc_num2str(result); - Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); - Z_TYPE_P(return_value) = IS_STRING; + RETVAL_STR(bc_num2str(result)); break; case -1: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Division by zero"); @@ -427,9 +417,7 @@ PHP_FUNCTION(bcpowmod) if (result->n_scale > scale) { result->n_scale = scale; } - Z_STRVAL_P(return_value) = bc_num2str(result); - Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); - Z_TYPE_P(return_value) = IS_STRING; + RETVAL_STR(bc_num2str(result)); } else { RETVAL_FALSE; } @@ -471,9 +459,7 @@ PHP_FUNCTION(bcpow) result->n_scale = scale; } - Z_STRVAL_P(return_value) = bc_num2str(result); - Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); - Z_TYPE_P(return_value) = IS_STRING; + RETVAL_STR(bc_num2str(result)); bc_free_num(&first); bc_free_num(&second); bc_free_num(&result); @@ -506,9 +492,7 @@ PHP_FUNCTION(bcsqrt) if (result->n_scale > scale) { result->n_scale = scale; } - Z_STRVAL_P(return_value) = bc_num2str(result); - Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); - Z_TYPE_P(return_value) = IS_STRING; + RETVAL_STR(bc_num2str(result)); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Square root of negative number"); } @@ -541,8 +525,7 @@ PHP_FUNCTION(bccomp) bc_str2num(&first, left, scale TSRMLS_CC); bc_str2num(&second, right, scale TSRMLS_CC); - Z_LVAL_P(return_value) = bc_compare(first, second); - Z_TYPE_P(return_value) = IS_LONG; + RETVAL_LONG(bc_compare(first, second)); bc_free_num(&first); bc_free_num(&second); diff --git a/ext/bcmath/libbcmath/src/bcmath.h b/ext/bcmath/libbcmath/src/bcmath.h index 3d7c478648..1e75cbdca3 100644 --- a/ext/bcmath/libbcmath/src/bcmath.h +++ b/ext/bcmath/libbcmath/src/bcmath.h @@ -111,7 +111,7 @@ _PROTOTYPE(void bc_init_num, (bc_num *num TSRMLS_DC)); _PROTOTYPE(void bc_str2num, (bc_num *num, char *str, int scale TSRMLS_DC)); -_PROTOTYPE(char *bc_num2str, (bc_num num)); +_PROTOTYPE(zend_string *bc_num2str, (bc_num num)); _PROTOTYPE(void bc_int2num, (bc_num *num, int val)); diff --git a/ext/bcmath/libbcmath/src/num2str.c b/ext/bcmath/libbcmath/src/num2str.c index 14c57726fe..47990d2e92 100644 --- a/ext/bcmath/libbcmath/src/num2str.c +++ b/ext/bcmath/libbcmath/src/num2str.c @@ -40,40 +40,42 @@ /* Convert a numbers to a string. Base 10 only.*/ -char +zend_string *bc_num2str (num) bc_num num; { - char *str, *sptr; - char *nptr; - int index, signch; + zend_string *str; + char *sptr; + char *nptr; + int index, signch; - /* Allocate the string memory. */ - signch = ( num->n_sign == PLUS ? 0 : 1 ); /* Number of sign chars. */ - if (num->n_scale > 0) - str = (char *) safe_emalloc (1, num->n_len + num->n_scale, 2 + signch); - else - str = (char *) safe_emalloc (1, num->n_len, 1 + signch); - if (str == NULL) bc_out_of_memory(); + /* Allocate the string memory. */ + signch = ( num->n_sign == PLUS ? 0 : 1 ); /* Number of sign chars. */ + if (num->n_scale > 0) + str = STR_ALLOC(num->n_len + num->n_scale + signch + 1, 0); + else + str = STR_ALLOC(num->n_len + signch, 0); + if (str == NULL) bc_out_of_memory(); - /* The negative sign if needed. */ - sptr = str; - if (signch) *sptr++ = '-'; + /* The negative sign if needed. */ + sptr = str->val; + if (signch) *sptr++ = '-'; - /* Load the whole number. */ - nptr = num->n_value; - for (index=num->n_len; index>0; index--) - *sptr++ = BCD_CHAR(*nptr++); + /* Load the whole number. */ + nptr = num->n_value; + for (index=num->n_len; index>0; index--) + *sptr++ = BCD_CHAR(*nptr++); - /* Now the fraction. */ - if (num->n_scale > 0) - { - *sptr++ = '.'; - for (index=0; index<num->n_scale; index++) - *sptr++ = BCD_CHAR(*nptr++); - } + /* Now the fraction. */ + if (num->n_scale > 0) + { + *sptr++ = '.'; + for (index=0; index<num->n_scale; index++) + *sptr++ = BCD_CHAR(*nptr++); + } - /* Terminate the string and return it! */ - *sptr = '\0'; - return (str); + /* Terminate the string and return it! */ + *sptr = '\0'; + str->len = sptr - (char *)str->val; + return str; } |