diff options
Diffstat (limited to 'ext/bcmath/bcmath.c')
-rw-r--r-- | ext/bcmath/bcmath.c | 159 |
1 files changed, 88 insertions, 71 deletions
diff --git a/ext/bcmath/bcmath.c b/ext/bcmath/bcmath.c index 61ee95b211..b7afaf04d3 100644 --- a/ext/bcmath/bcmath.c +++ b/ext/bcmath/bcmath.c @@ -156,22 +156,24 @@ static void php_str2num(bc_num *num, char *str) PHP_FUNCTION(bcadd) { zend_string *left, *right; - zend_long scale_param = 0; + zend_long scale_param; + zend_bool scale_param_is_null = 1; bc_num first, second, result; - int scale = BCG(bc_precision); + int scale; ZEND_PARSE_PARAMETERS_START(2, 3) Z_PARAM_STR(left) Z_PARAM_STR(right) Z_PARAM_OPTIONAL - Z_PARAM_LONG(scale_param) + Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null) ZEND_PARSE_PARAMETERS_END(); - if (ZEND_NUM_ARGS() == 3) { - if (scale_param < 0 || scale_param > INT_MAX) { - zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); - RETURN_THROWS(); - } + if (scale_param_is_null) { + scale = BCG(bc_precision); + } else if (scale_param < 0 || scale_param > INT_MAX) { + zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); + RETURN_THROWS(); + } else { scale = (int) scale_param; } @@ -195,22 +197,24 @@ PHP_FUNCTION(bcadd) PHP_FUNCTION(bcsub) { zend_string *left, *right; - zend_long scale_param = 0; + zend_long scale_param; + zend_bool scale_param_is_null = 1; bc_num first, second, result; - int scale = BCG(bc_precision); + int scale; ZEND_PARSE_PARAMETERS_START(2, 3) Z_PARAM_STR(left) Z_PARAM_STR(right) Z_PARAM_OPTIONAL - Z_PARAM_LONG(scale_param) + Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null) ZEND_PARSE_PARAMETERS_END(); - if (ZEND_NUM_ARGS() == 3) { - if (scale_param < 0 || scale_param > INT_MAX) { - zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); - RETURN_THROWS(); - } + if (scale_param_is_null) { + scale = BCG(bc_precision); + } else if (scale_param < 0 || scale_param > INT_MAX) { + zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); + RETURN_THROWS(); + } else { scale = (int) scale_param; } @@ -234,22 +238,24 @@ PHP_FUNCTION(bcsub) PHP_FUNCTION(bcmul) { zend_string *left, *right; - zend_long scale_param = 0; + zend_long scale_param; + zend_bool scale_param_is_null = 1; bc_num first, second, result; - int scale = BCG(bc_precision); + int scale; ZEND_PARSE_PARAMETERS_START(2, 3) Z_PARAM_STR(left) Z_PARAM_STR(right) Z_PARAM_OPTIONAL - Z_PARAM_LONG(scale_param) + Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null) ZEND_PARSE_PARAMETERS_END(); - if (ZEND_NUM_ARGS() == 3) { - if (scale_param < 0 || scale_param > INT_MAX) { - zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); - RETURN_THROWS(); - } + if (scale_param_is_null) { + scale = BCG(bc_precision); + } else if (scale_param < 0 || scale_param > INT_MAX) { + zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); + RETURN_THROWS(); + } else { scale = (int) scale_param; } @@ -273,7 +279,8 @@ PHP_FUNCTION(bcmul) PHP_FUNCTION(bcdiv) { zend_string *left, *right; - zend_long scale_param = 0; + zend_long scale_param; + zend_bool scale_param_is_null = 1; bc_num first, second, result; int scale = BCG(bc_precision); @@ -281,14 +288,15 @@ PHP_FUNCTION(bcdiv) Z_PARAM_STR(left) Z_PARAM_STR(right) Z_PARAM_OPTIONAL - Z_PARAM_LONG(scale_param) + Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null) ZEND_PARSE_PARAMETERS_END(); - if (ZEND_NUM_ARGS() == 3) { - if (scale_param < 0 || scale_param > INT_MAX) { - zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); - RETURN_THROWS(); - } + if (scale_param_is_null) { + scale = BCG(bc_precision); + } else if (scale_param < 0 || scale_param > INT_MAX) { + zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); + RETURN_THROWS(); + } else { scale = (int) scale_param; } @@ -319,7 +327,8 @@ PHP_FUNCTION(bcdiv) PHP_FUNCTION(bcmod) { zend_string *left, *right; - zend_long scale_param = 0; + zend_long scale_param; + zend_bool scale_param_is_null = 1; bc_num first, second, result; int scale = BCG(bc_precision); @@ -327,14 +336,15 @@ PHP_FUNCTION(bcmod) Z_PARAM_STR(left) Z_PARAM_STR(right) Z_PARAM_OPTIONAL - Z_PARAM_LONG(scale_param) + Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null) ZEND_PARSE_PARAMETERS_END(); - if (ZEND_NUM_ARGS() == 3) { - if (scale_param < 0 || scale_param > INT_MAX) { - zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); - RETURN_THROWS(); - } + if (scale_param_is_null) { + scale = BCG(bc_precision); + } else if (scale_param < 0 || scale_param > INT_MAX) { + zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); + RETURN_THROWS(); + } else { scale = (int) scale_param; } @@ -356,7 +366,6 @@ PHP_FUNCTION(bcmod) bc_free_num(&first); bc_free_num(&second); bc_free_num(&result); - return; } /* }}} */ @@ -365,7 +374,8 @@ PHP_FUNCTION(bcmod) PHP_FUNCTION(bcpowmod) { zend_string *left, *right, *modulus; - zend_long scale_param = 0; + zend_long scale_param; + zend_bool scale_param_is_null = 1; bc_num first, second, mod, result; int scale = BCG(bc_precision); @@ -374,14 +384,15 @@ PHP_FUNCTION(bcpowmod) Z_PARAM_STR(right) Z_PARAM_STR(modulus) Z_PARAM_OPTIONAL - Z_PARAM_LONG(scale_param) + Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null) ZEND_PARSE_PARAMETERS_END(); - if (ZEND_NUM_ARGS() == 4) { - if (scale_param < 0 || scale_param > INT_MAX) { - zend_argument_value_error(4, "must be between 0 and %d", INT_MAX); - RETURN_THROWS(); - } + if (scale_param_is_null) { + scale = BCG(bc_precision); + } else if (scale_param < 0 || scale_param > INT_MAX) { + zend_argument_value_error(4, "must be between 0 and %d", INT_MAX); + RETURN_THROWS(); + } else { scale = (int) scale_param; } @@ -403,7 +414,6 @@ PHP_FUNCTION(bcpowmod) bc_free_num(&second); bc_free_num(&mod); bc_free_num(&result); - return; } /* }}} */ @@ -412,7 +422,8 @@ PHP_FUNCTION(bcpowmod) PHP_FUNCTION(bcpow) { zend_string *left, *right; - zend_long scale_param = 0; + zend_long scale_param; + zend_bool scale_param_is_null = 1; bc_num first, second, result; int scale = BCG(bc_precision); @@ -420,14 +431,15 @@ PHP_FUNCTION(bcpow) Z_PARAM_STR(left) Z_PARAM_STR(right) Z_PARAM_OPTIONAL - Z_PARAM_LONG(scale_param) + Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null) ZEND_PARSE_PARAMETERS_END(); - if (ZEND_NUM_ARGS() == 3) { - if (scale_param < 0 || scale_param > INT_MAX) { - zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); - RETURN_THROWS(); - } + if (scale_param_is_null) { + scale = BCG(bc_precision); + } else if (scale_param < 0 || scale_param > INT_MAX) { + zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); + RETURN_THROWS(); + } else { scale = (int) scale_param; } @@ -442,7 +454,6 @@ PHP_FUNCTION(bcpow) bc_free_num(&first); bc_free_num(&second); bc_free_num(&result); - return; } /* }}} */ @@ -451,21 +462,23 @@ PHP_FUNCTION(bcpow) PHP_FUNCTION(bcsqrt) { zend_string *left; - zend_long scale_param = 0; + zend_long scale_param; + zend_bool scale_param_is_null = 1; bc_num result; int scale = BCG(bc_precision); ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_STR(left) Z_PARAM_OPTIONAL - Z_PARAM_LONG(scale_param) + Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null) ZEND_PARSE_PARAMETERS_END(); - if (ZEND_NUM_ARGS() == 2) { - if (scale_param < 0 || scale_param > INT_MAX) { - zend_argument_value_error(2, "must be between 0 and %d", INT_MAX); - RETURN_THROWS(); - } + if (scale_param_is_null) { + scale = BCG(bc_precision); + } else if (scale_param < 0 || scale_param > INT_MAX) { + zend_argument_value_error(2, "must be between 0 and %d", INT_MAX); + RETURN_THROWS(); + } else { scale = (int) scale_param; } @@ -488,7 +501,8 @@ PHP_FUNCTION(bcsqrt) PHP_FUNCTION(bccomp) { zend_string *left, *right; - zend_long scale_param = 0; + zend_long scale_param; + zend_bool scale_param_is_null = 1; bc_num first, second; int scale = BCG(bc_precision); @@ -496,14 +510,15 @@ PHP_FUNCTION(bccomp) Z_PARAM_STR(left) Z_PARAM_STR(right) Z_PARAM_OPTIONAL - Z_PARAM_LONG(scale_param) + Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null) ZEND_PARSE_PARAMETERS_END(); - if (ZEND_NUM_ARGS() == 3) { - if (scale_param < 0 || scale_param > INT_MAX) { - zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); - RETURN_THROWS(); - } + if (scale_param_is_null) { + scale = BCG(bc_precision); + } else if (scale_param < 0 || scale_param > INT_MAX) { + zend_argument_value_error(3, "must be between 0 and %d", INT_MAX); + RETURN_THROWS(); + } else { scale = (int) scale_param; } @@ -529,19 +544,21 @@ PHP_FUNCTION(bccomp) PHP_FUNCTION(bcscale) { zend_long old_scale, new_scale; + zend_bool new_scale_is_null = 1; ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL - Z_PARAM_LONG(new_scale) + Z_PARAM_LONG_OR_NULL(new_scale, new_scale_is_null) ZEND_PARSE_PARAMETERS_END(); old_scale = BCG(bc_precision); - if (ZEND_NUM_ARGS() == 1) { + if (!new_scale_is_null) { if (new_scale < 0 || new_scale > INT_MAX) { zend_argument_value_error(1, "must be between 0 and %d", INT_MAX); RETURN_THROWS(); } + BCG(bc_precision) = (int) new_scale; } |