Skip to content

Implement GMP::__construct #10225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions ext/gmp/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,26 @@ static inline void _gmp_unary_opl(INTERNAL_FUNCTION_PARAMETERS, gmp_unary_opl_t
}
/* }}} */

static bool gmp_verify_base(zend_long base, uint32_t arg_num)
{
if (base && (base < 2 || base > GMP_MAX_BASE)) {
zend_argument_value_error(arg_num, "must be between 2 and %d", GMP_MAX_BASE);
return false;
}

return true;
}

static zend_result gmp_initialize_number(mpz_ptr gmp_number, const zend_string *arg_str, zend_long arg_l, zend_long base)
{
if (arg_str) {
return convert_zstr_to_gmp(gmp_number, arg_str, base, 1);
}

mpz_set_si(gmp_number, arg_l);
return SUCCESS;
}

/* {{{ Initializes GMP number */
ZEND_FUNCTION(gmp_init)
{
Expand All @@ -876,18 +896,14 @@ ZEND_FUNCTION(gmp_init)
Z_PARAM_LONG(base)
ZEND_PARSE_PARAMETERS_END();

if (base && (base < 2 || base > GMP_MAX_BASE)) {
zend_argument_value_error(2, "must be between 2 and %d", GMP_MAX_BASE);
if (!gmp_verify_base(base, 2)) {
RETURN_THROWS();
}

INIT_GMP_RETVAL(gmp_number);
if (arg_str) {
if (convert_zstr_to_gmp(gmp_number, arg_str, base, 1) == FAILURE) {
RETURN_THROWS();
}
} else {
mpz_set_si(gmp_number, arg_l);

if (gmp_initialize_number(gmp_number, arg_str, arg_l, base) == FAILURE) {
RETURN_THROWS();
}
}
/* }}} */
Expand Down Expand Up @@ -2021,6 +2037,30 @@ ZEND_FUNCTION(gmp_scan1)
}
/* }}} */

ZEND_METHOD(GMP, __construct)
{
zend_string *arg_str = NULL;
zend_long arg_l = 0;
zend_long base = 0;

ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_LONG(arg_str, arg_l)
Z_PARAM_LONG(base)
ZEND_PARSE_PARAMETERS_END();

if (!gmp_verify_base(base, 2)) {
RETURN_THROWS();
}

return_value = ZEND_THIS;
mpz_ptr gmp_number = GET_GMP_FROM_ZVAL(ZEND_THIS);

if (gmp_initialize_number(gmp_number, arg_str, arg_l, base) == FAILURE) {
RETURN_THROWS();
}
}

ZEND_METHOD(GMP, __serialize)
{
ZEND_PARSE_PARAMETERS_NONE();
Expand Down
2 changes: 2 additions & 0 deletions ext/gmp/gmp.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@

class GMP
{
public function __construct(int|string $num = 0, int $base = 0) {}

public function __serialize(): array {}

public function __unserialize(array $data): void {}
Expand Down
9 changes: 8 additions & 1 deletion ext/gmp/gmp_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions ext/gmp/tests/construct.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--TEST--
Constructor for GMP
--EXTENSIONS--
gmp
--FILE--
<?php
var_dump(new GMP);
var_dump(new GMP(0));
var_dump(new GMP(123));
var_dump(new GMP("0xAA"));
var_dump(new GMP("12", 4));
try {
var_dump(new GMP("12", 999));
} catch (ValueError $e) {
echo $e->getMessage() . "\n";
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the sake of completeness, add a test with a non-numeric string as the number (e.g. 'hello', or '')

try {
var_dump(new GMP("", 10));
} catch (ValueError $e) {
echo $e->getMessage() . "\n";
}
try {
var_dump(new GMP("hello"));
} catch (ValueError $e) {
echo $e->getMessage() . "\n";
}
?>
--EXPECT--
object(GMP)#1 (1) {
["num"]=>
string(1) "0"
}
object(GMP)#1 (1) {
["num"]=>
string(1) "0"
}
object(GMP)#1 (1) {
["num"]=>
string(3) "123"
}
object(GMP)#1 (1) {
["num"]=>
string(3) "170"
}
object(GMP)#1 (1) {
["num"]=>
string(1) "6"
}
GMP::__construct(): Argument #2 ($base) must be between 2 and 62
GMP::__construct(): Argument #1 ($num) is not an integer string
GMP::__construct(): Argument #1 ($num) is not an integer string