/* {{{ php_str2num
Convert to bc_num detecting scale */
-static void php_str2num(bc_num *num, char *str)
+static zend_result php_str2num(bc_num *num, char *str)
{
char *p;
if (!(p = strchr(str, '.'))) {
if (!bc_str2num(num, str, 0)) {
- php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed");
+ return FAILURE;
}
- return;
+
+ return SUCCESS;
}
if (!bc_str2num(num, str, strlen(p+1))) {
- php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed");
+ return FAILURE;
}
+
+ return SUCCESS;
}
/* }}} */
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "is not well-formed");
+ goto cleanup;
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "is not well-formed");
+ goto cleanup;
+ }
+
bc_add (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&result);
- return;
+
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&result);
+ };
}
/* }}} */
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "is not well-formed");
+ goto cleanup;
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "is not well-formed");
+ goto cleanup;
+ }
+
bc_sub (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&result);
- return;
+
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&result);
+ };
}
/* }}} */
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "is not well-formed");
+ goto cleanup;
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "is not well-formed");
+ goto cleanup;
+ }
+
bc_multiply (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&result);
- return;
+
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&result);
+ };
}
/* }}} */
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "is not well-formed");
+ goto cleanup;
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "is not well-formed");
+ goto cleanup;
+ }
switch (bc_divide(first, second, &result, scale)) {
case 0: /* OK */
break;
}
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&result);
- return;
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&result);
+ };
}
/* }}} */
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "is not well-formed");
+ goto cleanup;
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "is not well-formed");
+ goto cleanup;
+ }
switch (bc_modulo(first, second, &result, scale)) {
case 0:
break;
}
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&result);
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&result);
+ };
}
/* }}} */
bc_init_num(&second);
bc_init_num(&mod);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
- php_str2num(&mod, ZSTR_VAL(modulus));
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "is not well-formed");
+ goto cleanup;
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "is not well-formed");
+ goto cleanup;
+ }
+
+ if (php_str2num(&mod, ZSTR_VAL(modulus)) == FAILURE) {
+ zend_argument_value_error(3, "is not well-formed");
+ goto cleanup;
+ }
if (bc_raisemod(first, second, mod, &result, scale) == SUCCESS) {
RETVAL_STR(bc_num2str_ex(result, scale));
}
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&mod);
- bc_free_num(&result);
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&mod);
+ bc_free_num(&result);
+ };
}
/* }}} */
bc_init_num(&first);
bc_init_num(&second);
bc_init_num(&result);
- php_str2num(&first, ZSTR_VAL(left));
- php_str2num(&second, ZSTR_VAL(right));
+
+ if (php_str2num(&first, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "is not well-formed");
+ goto cleanup;
+ }
+
+ if (php_str2num(&second, ZSTR_VAL(right)) == FAILURE) {
+ zend_argument_value_error(2, "is not well-formed");
+ goto cleanup;
+ }
+
bc_raise (first, second, &result, scale);
RETVAL_STR(bc_num2str_ex(result, scale));
- bc_free_num(&first);
- bc_free_num(&second);
- bc_free_num(&result);
+
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ bc_free_num(&result);
+ };
}
/* }}} */
}
bc_init_num(&result);
- php_str2num(&result, ZSTR_VAL(left));
+
+ if (php_str2num(&result, ZSTR_VAL(left)) == FAILURE) {
+ zend_argument_value_error(1, "is not well-formed");
+ goto cleanup;
+ }
if (bc_sqrt (&result, scale) != 0) {
RETVAL_STR(bc_num2str_ex(result, scale));
zend_argument_value_error(1, "must be greater than or equal to 0");
}
- bc_free_num(&result);
- return;
+ cleanup: {
+ bc_free_num(&result);
+ };
}
/* }}} */
bc_init_num(&second);
if (!bc_str2num(&first, ZSTR_VAL(left), scale)) {
- php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed");
+ zend_argument_value_error(1, "is not well-formed");
+ goto cleanup;
}
+
if (!bc_str2num(&second, ZSTR_VAL(right), scale)) {
- php_error_docref(NULL, E_WARNING, "bcmath function argument is not well-formed");
+ zend_argument_value_error(2, "is not well-formed");
+ goto cleanup;
}
+
RETVAL_LONG(bc_compare(first, second));
- bc_free_num(&first);
- bc_free_num(&second);
- return;
+ cleanup: {
+ bc_free_num(&first);
+ bc_free_num(&second);
+ };
}
/* }}} */
echo bcadd("+0", "2"), "\n";
echo bcadd("-0", "2"), "\n";
-echo bcadd(" 0", "2");
-echo bcadd("1e1", "2");
-echo bcadd("1,1", "2");
-echo bcadd("Hello", "2");
-echo bcadd("1 1", "2");
-echo "\n", "\n";
+echo "\n";
+
+try {
+ echo bcadd(" 0", "2");
+} catch (\ValueError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bcadd("1e1", "2");
+} catch (\ValueError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bcadd("1,1", "2");
+} catch (\ValueError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bcadd("Hello", "2");
+} catch (\ValueError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bcadd("1 1", "2");
+} catch (\ValueError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+echo "\n";
echo bccomp("1", "2"),"\n";
echo bccomp("1.1", "2", 2),"\n";
echo bccomp("+0", "2"), "\n";
echo bccomp("-0", "2"), "\n";
-echo bccomp(" 0", "2");
-echo bccomp("1e1", "2");
-echo bccomp("1,1", "2");
-echo bccomp("Hello", "2");
-echo bccomp("1 1", "2");
+echo "\n";
+
+try {
+ echo bccomp(" 0", "2");
+} catch (\ValueError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bccomp("1e1", "2");
+} catch (\ValueError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bccomp("1,1", "2");
+} catch (\ValueError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bccomp("Hello", "2");
+} catch (\ValueError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
+try {
+ echo bccomp("1 1", "2");
+} catch (\ValueError $e) {
+ echo $e->getMessage() . PHP_EOL;
+}
+
?>
--EXPECTF--
3
2
2
-Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d
-2
-Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d
-2
-Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d
-2
-Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d
-2
-Warning: bcadd(): bcmath function argument is not well-formed in %s on line %d
-2
+bcadd(): Argument #1 ($num1) is not well-formed
+bcadd(): Argument #1 ($num1) is not well-formed
+bcadd(): Argument #1 ($num1) is not well-formed
+bcadd(): Argument #1 ($num1) is not well-formed
+bcadd(): Argument #1 ($num1) is not well-formed
-1
-1
-1
-1
-Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d
--1
-Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d
--1
-Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d
--1
-Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d
--1
-Warning: bccomp(): bcmath function argument is not well-formed in %s on line %d
--1
\ No newline at end of file
+bccomp(): Argument #1 ($num1) is not well-formed
+bccomp(): Argument #1 ($num1) is not well-formed
+bccomp(): Argument #1 ($num1) is not well-formed
+bccomp(): Argument #1 ($num1) is not well-formed
+bccomp(): Argument #1 ($num1) is not well-formed
\ No newline at end of file