mirror of
https://github.com/php/php-src.git
synced 2024-11-23 18:04:36 +08:00
- New parameter parsing API
This commit is contained in:
parent
24f01f7ab0
commit
7939a8347e
@ -214,38 +214,31 @@ static void php_str2num(bc_num *num, char *str TSRMLS_DC)
|
||||
Returns the sum of two arbitrary precision numbers */
|
||||
PHP_FUNCTION(bcadd)
|
||||
{
|
||||
zval **left, **right, **scale_param;
|
||||
char *left, *right;
|
||||
long scale_param;
|
||||
bc_num first, second, result;
|
||||
int scale = BCG(bc_precision);
|
||||
int left_len, right_len;
|
||||
int scale = BCG(bc_precision), argc = ZEND_NUM_ARGS();
|
||||
|
||||
switch (ZEND_NUM_ARGS()) {
|
||||
case 2:
|
||||
if (zend_get_parameters_ex(2, &left, &right) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (zend_get_parameters_ex(3, &left, &right, &scale_param) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
convert_to_long_ex(scale_param);
|
||||
scale = (int) ((int)Z_LVAL_PP(scale_param) < 0) ? 0 : Z_LVAL_PP(scale_param);
|
||||
break;
|
||||
default:
|
||||
WRONG_PARAM_COUNT;
|
||||
break;
|
||||
if (zend_parse_parameters(argc TSRMLS_CC, "ss|l", &left, &left_len, &right, &right_len, &scale_param) == FAILURE) {
|
||||
return;
|
||||
}
|
||||
convert_to_string_ex(left);
|
||||
convert_to_string_ex(right);
|
||||
|
||||
if (argc == 3) {
|
||||
scale = (int) ((int)scale_param < 0) ? 0 : scale_param;
|
||||
}
|
||||
|
||||
bc_init_num(&first TSRMLS_CC);
|
||||
bc_init_num(&second TSRMLS_CC);
|
||||
bc_init_num(&result TSRMLS_CC);
|
||||
php_str2num(&first, Z_STRVAL_PP(left) TSRMLS_CC);
|
||||
php_str2num(&second, Z_STRVAL_PP(right) TSRMLS_CC);
|
||||
php_str2num(&first, left TSRMLS_CC);
|
||||
php_str2num(&second, right TSRMLS_CC);
|
||||
bc_add (first, second, &result, scale);
|
||||
|
||||
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;
|
||||
@ -260,38 +253,31 @@ PHP_FUNCTION(bcadd)
|
||||
Returns the difference between two arbitrary precision numbers */
|
||||
PHP_FUNCTION(bcsub)
|
||||
{
|
||||
zval **left, **right, **scale_param;
|
||||
char *left, *right;
|
||||
int left_len, right_len;
|
||||
long scale_param;
|
||||
bc_num first, second, result;
|
||||
int scale = BCG(bc_precision);
|
||||
int scale = BCG(bc_precision), argc = ZEND_NUM_ARGS();
|
||||
|
||||
switch (ZEND_NUM_ARGS()) {
|
||||
case 2:
|
||||
if (zend_get_parameters_ex(2, &left, &right) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (zend_get_parameters_ex(3, &left, &right, &scale_param) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
convert_to_long_ex(scale_param);
|
||||
scale = (int) ((int)Z_LVAL_PP(scale_param) < 0) ? 0 : Z_LVAL_PP(scale_param);
|
||||
break;
|
||||
default:
|
||||
WRONG_PARAM_COUNT;
|
||||
break;
|
||||
if (zend_parse_parameters(argc TSRMLS_CC, "ss|l", &left, &left_len, &right, &right_len, &scale_param) == FAILURE) {
|
||||
return;
|
||||
}
|
||||
convert_to_string_ex(left);
|
||||
convert_to_string_ex(right);
|
||||
|
||||
if (argc == 3) {
|
||||
scale = (int) ((int)scale_param < 0) ? 0 : scale_param;
|
||||
}
|
||||
|
||||
bc_init_num(&first TSRMLS_CC);
|
||||
bc_init_num(&second TSRMLS_CC);
|
||||
bc_init_num(&result TSRMLS_CC);
|
||||
php_str2num(&first, Z_STRVAL_PP(left) TSRMLS_CC);
|
||||
php_str2num(&second, Z_STRVAL_PP(right) TSRMLS_CC);
|
||||
php_str2num(&first, left TSRMLS_CC);
|
||||
php_str2num(&second, right TSRMLS_CC);
|
||||
bc_sub (first, second, &result, scale);
|
||||
|
||||
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;
|
||||
@ -306,38 +292,31 @@ PHP_FUNCTION(bcsub)
|
||||
Returns the multiplication of two arbitrary precision numbers */
|
||||
PHP_FUNCTION(bcmul)
|
||||
{
|
||||
zval **left, **right, **scale_param;
|
||||
char *left, *right;
|
||||
int left_len, right_len;
|
||||
long scale_param;
|
||||
bc_num first, second, result;
|
||||
int scale = BCG(bc_precision);
|
||||
int scale = BCG(bc_precision), argc = ZEND_NUM_ARGS();
|
||||
|
||||
switch (ZEND_NUM_ARGS()) {
|
||||
case 2:
|
||||
if (zend_get_parameters_ex(2, &left, &right) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (zend_get_parameters_ex(3, &left, &right, &scale_param) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
convert_to_long_ex(scale_param);
|
||||
scale = (int) ((int)Z_LVAL_PP(scale_param) < 0) ? 0 : Z_LVAL_PP(scale_param);
|
||||
break;
|
||||
default:
|
||||
WRONG_PARAM_COUNT;
|
||||
break;
|
||||
if (zend_parse_parameters(argc TSRMLS_CC, "ss|l", &left, &left_len, &right, &right_len, &scale_param) == FAILURE) {
|
||||
return;
|
||||
}
|
||||
convert_to_string_ex(left);
|
||||
convert_to_string_ex(right);
|
||||
|
||||
if (argc == 3) {
|
||||
scale = (int) ((int)scale_param < 0) ? 0 : scale_param;
|
||||
}
|
||||
|
||||
bc_init_num(&first TSRMLS_CC);
|
||||
bc_init_num(&second TSRMLS_CC);
|
||||
bc_init_num(&result TSRMLS_CC);
|
||||
php_str2num(&first, Z_STRVAL_PP(left) TSRMLS_CC);
|
||||
php_str2num(&second, Z_STRVAL_PP(right) TSRMLS_CC);
|
||||
php_str2num(&first, left TSRMLS_CC);
|
||||
php_str2num(&second, right TSRMLS_CC);
|
||||
bc_multiply (first, second, &result, scale TSRMLS_CC);
|
||||
|
||||
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;
|
||||
@ -352,34 +331,26 @@ PHP_FUNCTION(bcmul)
|
||||
Returns the quotient of two arbitrary precision numbers (division) */
|
||||
PHP_FUNCTION(bcdiv)
|
||||
{
|
||||
zval **left, **right, **scale_param;
|
||||
char *left, *right;
|
||||
int left_len, right_len;
|
||||
long scale_param;
|
||||
bc_num first, second, result;
|
||||
int scale = BCG(bc_precision);
|
||||
int scale = BCG(bc_precision), argc = ZEND_NUM_ARGS();
|
||||
|
||||
switch (ZEND_NUM_ARGS()) {
|
||||
case 2:
|
||||
if (zend_get_parameters_ex(2, &left, &right) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (zend_get_parameters_ex(3, &left, &right, &scale_param) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
convert_to_long_ex(scale_param);
|
||||
scale = (int) ((int)Z_LVAL_PP(scale_param) < 0) ? 0 : Z_LVAL_PP(scale_param);
|
||||
break;
|
||||
default:
|
||||
WRONG_PARAM_COUNT;
|
||||
break;
|
||||
if (zend_parse_parameters(argc TSRMLS_CC, "ss|l", &left, &left_len, &right, &right_len, &scale_param) == FAILURE) {
|
||||
return;
|
||||
}
|
||||
convert_to_string_ex(left);
|
||||
convert_to_string_ex(right);
|
||||
|
||||
if (argc == 3) {
|
||||
scale = (int) ((int)scale_param < 0) ? 0 : scale_param;
|
||||
}
|
||||
|
||||
bc_init_num(&first TSRMLS_CC);
|
||||
bc_init_num(&second TSRMLS_CC);
|
||||
bc_init_num(&result TSRMLS_CC);
|
||||
php_str2num(&first, Z_STRVAL_PP(left) TSRMLS_CC);
|
||||
php_str2num(&second, Z_STRVAL_PP(right) TSRMLS_CC);
|
||||
php_str2num(&first, left TSRMLS_CC);
|
||||
php_str2num(&second, right TSRMLS_CC);
|
||||
|
||||
switch (bc_divide(first, second, &result, scale TSRMLS_CC)) {
|
||||
case 0: /* OK */
|
||||
if (result->n_scale > scale) {
|
||||
@ -393,6 +364,7 @@ PHP_FUNCTION(bcdiv)
|
||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Division by zero");
|
||||
break;
|
||||
}
|
||||
|
||||
bc_free_num(&first);
|
||||
bc_free_num(&second);
|
||||
bc_free_num(&result);
|
||||
@ -404,26 +376,20 @@ PHP_FUNCTION(bcdiv)
|
||||
Returns the modulus of the two arbitrary precision operands */
|
||||
PHP_FUNCTION(bcmod)
|
||||
{
|
||||
zval **left, **right;
|
||||
char *left, *right;
|
||||
int left_len, right_len;
|
||||
bc_num first, second, result;
|
||||
|
||||
switch (ZEND_NUM_ARGS()) {
|
||||
case 2:
|
||||
if (zend_get_parameters_ex(2, &left, &right) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
WRONG_PARAM_COUNT;
|
||||
break;
|
||||
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &left, &left_len, &right, &right_len) == FAILURE) {
|
||||
return;
|
||||
}
|
||||
convert_to_string_ex(left);
|
||||
convert_to_string_ex(right);
|
||||
|
||||
bc_init_num(&first TSRMLS_CC);
|
||||
bc_init_num(&second TSRMLS_CC);
|
||||
bc_init_num(&result TSRMLS_CC);
|
||||
bc_str2num(&first, Z_STRVAL_PP(left), 0 TSRMLS_CC);
|
||||
bc_str2num(&second, Z_STRVAL_PP(right), 0 TSRMLS_CC);
|
||||
bc_str2num(&first, left, 0 TSRMLS_CC);
|
||||
bc_str2num(&second, right, 0 TSRMLS_CC);
|
||||
|
||||
switch (bc_modulo(first, second, &result, 0 TSRMLS_CC)) {
|
||||
case 0:
|
||||
Z_STRVAL_P(return_value) = bc_num2str(result);
|
||||
@ -434,6 +400,7 @@ PHP_FUNCTION(bcmod)
|
||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Division by zero");
|
||||
break;
|
||||
}
|
||||
|
||||
bc_free_num(&first);
|
||||
bc_free_num(&second);
|
||||
bc_free_num(&result);
|
||||
@ -488,38 +455,31 @@ PHP_FUNCTION(bcpowmod)
|
||||
Returns the value of an arbitrary precision number raised to the power of another */
|
||||
PHP_FUNCTION(bcpow)
|
||||
{
|
||||
zval **left, **right, **scale_param;
|
||||
char *left, *right;
|
||||
int left_len, right_len;
|
||||
long scale_param;
|
||||
bc_num first, second, result;
|
||||
int scale = BCG(bc_precision);
|
||||
int scale = BCG(bc_precision), argc = ZEND_NUM_ARGS();
|
||||
|
||||
switch (ZEND_NUM_ARGS()) {
|
||||
case 2:
|
||||
if (zend_get_parameters_ex(2, &left, &right) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (zend_get_parameters_ex(3, &left, &right, &scale_param) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
convert_to_long_ex(scale_param);
|
||||
scale = (int) ((int)Z_LVAL_PP(scale_param) < 0) ? 0 : Z_LVAL_PP(scale_param);
|
||||
break;
|
||||
default:
|
||||
WRONG_PARAM_COUNT;
|
||||
break;
|
||||
if (zend_parse_parameters(argc TSRMLS_CC, "ss|l", &left, &left_len, &right, &right_len, &scale_param) == FAILURE) {
|
||||
return;
|
||||
}
|
||||
convert_to_string_ex(left);
|
||||
convert_to_string_ex(right);
|
||||
|
||||
if (argc == 3) {
|
||||
scale = (int) ((int)scale_param < 0) ? 0 : scale_param;
|
||||
}
|
||||
|
||||
bc_init_num(&first TSRMLS_CC);
|
||||
bc_init_num(&second TSRMLS_CC);
|
||||
bc_init_num(&result TSRMLS_CC);
|
||||
php_str2num(&first, Z_STRVAL_PP(left) TSRMLS_CC);
|
||||
php_str2num(&second, Z_STRVAL_PP(right) TSRMLS_CC);
|
||||
php_str2num(&first, left TSRMLS_CC);
|
||||
php_str2num(&second, right TSRMLS_CC);
|
||||
bc_raise (first, second, &result, scale TSRMLS_CC);
|
||||
|
||||
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;
|
||||
@ -534,30 +494,23 @@ PHP_FUNCTION(bcpow)
|
||||
Returns the square root of an arbitray precision number */
|
||||
PHP_FUNCTION(bcsqrt)
|
||||
{
|
||||
zval **left, **scale_param;
|
||||
char *left;
|
||||
int left_len;
|
||||
long scale_param;
|
||||
bc_num result;
|
||||
int scale = BCG(bc_precision);
|
||||
int scale = BCG(bc_precision), argc = ZEND_NUM_ARGS();
|
||||
|
||||
switch (ZEND_NUM_ARGS()) {
|
||||
case 1:
|
||||
if (zend_get_parameters_ex(1, &left) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if (zend_get_parameters_ex(2, &left, &scale_param) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
convert_to_long_ex(scale_param);
|
||||
scale = (int) ((int)Z_LVAL_PP(scale_param) < 0) ? 0 : Z_LVAL_PP(scale_param);
|
||||
break;
|
||||
default:
|
||||
WRONG_PARAM_COUNT;
|
||||
break;
|
||||
if (zend_parse_parameters(argc TSRMLS_CC, "s|l", &left, &left_len, &scale_param) == FAILURE) {
|
||||
return;
|
||||
}
|
||||
convert_to_string_ex(left);
|
||||
|
||||
if (argc == 2) {
|
||||
scale = (int) ((int)scale_param < 0) ? 0 : scale_param;
|
||||
}
|
||||
|
||||
bc_init_num(&result TSRMLS_CC);
|
||||
php_str2num(&result, Z_STRVAL_PP(left) TSRMLS_CC);
|
||||
php_str2num(&result, left TSRMLS_CC);
|
||||
|
||||
if (bc_sqrt (&result, scale TSRMLS_CC) != 0) {
|
||||
if (result->n_scale > scale) {
|
||||
result->n_scale = scale;
|
||||
@ -568,6 +521,7 @@ PHP_FUNCTION(bcsqrt)
|
||||
} else {
|
||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Square root of negative number");
|
||||
}
|
||||
|
||||
bc_free_num(&result);
|
||||
return;
|
||||
}
|
||||
@ -577,35 +531,25 @@ PHP_FUNCTION(bcsqrt)
|
||||
Compares two arbitrary precision numbers */
|
||||
PHP_FUNCTION(bccomp)
|
||||
{
|
||||
zval **left, **right, **scale_param;
|
||||
char *left, *right;
|
||||
int left_len, right_len;
|
||||
long scale_param;
|
||||
bc_num first, second;
|
||||
int scale = BCG(bc_precision);
|
||||
int scale = BCG(bc_precision), argc = ZEND_NUM_ARGS();
|
||||
|
||||
switch (ZEND_NUM_ARGS()) {
|
||||
case 2:
|
||||
if (zend_get_parameters_ex(2, &left, &right) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (zend_get_parameters_ex(3, &left, &right, &scale_param) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
convert_to_long_ex(scale_param);
|
||||
scale = (int) ((int)Z_LVAL_PP(scale_param) < 0) ? 0 : Z_LVAL_PP(scale_param);
|
||||
break;
|
||||
default:
|
||||
WRONG_PARAM_COUNT;
|
||||
break;
|
||||
if (zend_parse_parameters(argc TSRMLS_CC, "ss|l", &left, &left_len, &right, &right_len, &scale_param) == FAILURE) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (argc == 3) {
|
||||
scale = (int) ((int)scale_param < 0) ? 0 : scale_param;
|
||||
}
|
||||
|
||||
convert_to_string_ex(left);
|
||||
convert_to_string_ex(right);
|
||||
bc_init_num(&first TSRMLS_CC);
|
||||
bc_init_num(&second TSRMLS_CC);
|
||||
|
||||
bc_str2num(&first, Z_STRVAL_PP(left), scale TSRMLS_CC);
|
||||
bc_str2num(&second, Z_STRVAL_PP(right), scale TSRMLS_CC);
|
||||
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;
|
||||
|
||||
@ -619,14 +563,13 @@ PHP_FUNCTION(bccomp)
|
||||
Sets default scale parameter for all bc math functions */
|
||||
PHP_FUNCTION(bcscale)
|
||||
{
|
||||
zval **new_scale;
|
||||
long new_scale;
|
||||
|
||||
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &new_scale) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &new_scale) == FAILURE) {
|
||||
return;
|
||||
}
|
||||
|
||||
convert_to_long_ex(new_scale);
|
||||
BCG(bc_precision) = (Z_LVAL_PP(new_scale) < 0) ? 0 : Z_LVAL_PP(new_scale);
|
||||
|
||||
BCG(bc_precision) = (new_scale < 0) ? 0 : new_scale;
|
||||
|
||||
RETURN_TRUE;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user