Skip to content

Commit

Permalink
Add ini_parse_quantity function to convert ini quantities shorthand n…
Browse files Browse the repository at this point in the history
…otation to int (#8454)
  • Loading branch information
dmsnell authored Jul 10, 2022
1 parent 61ad0d9 commit 492af9f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
20 changes: 20 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "ext/session/php_session.h"
#include "zend_exceptions.h"
#include "zend_attributes.h"
#include "zend_ini.h"
#include "zend_operators.h"
#include "ext/standard/php_dns.h"
#include "ext/standard/php_uuencode.h"
Expand Down Expand Up @@ -1971,6 +1972,25 @@ PHP_FUNCTION(highlight_string)
}
/* }}} */

/* {{{ Get interpreted size from the ini shorthand syntax */
PHP_FUNCTION(ini_parse_quantity)
{
zend_string *shorthand;
zend_string *errstr;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(shorthand)
ZEND_PARSE_PARAMETERS_END();

RETVAL_LONG(zend_ini_parse_quantity(shorthand, &errstr));

if (errstr) {
zend_error(E_WARNING, "%s", ZSTR_VAL(errstr));
zend_string_release(errstr);
}
}
/* }}} */

/* {{{ Get a configuration option */
PHP_FUNCTION(ini_get)
{
Expand Down
2 changes: 2 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ function ini_alter(string $option, string|int|float|bool|null $value): string|fa

function ini_restore(string $option): void {}

function ini_parse_quantity(string $shorthand): int {}

/** @refcount 1 */
function set_include_path(string $include_path): string|false {}

Expand Down
8 changes: 7 additions & 1 deletion ext/standard/basic_functions_arginfo.h

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

36 changes: 36 additions & 0 deletions tests/basic/ini_parse_quantity_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
ini_parse_quantity() function - basic test for ini_parse_quantity()
--INI--
error_reporting = E_ALL ^ E_WARNING
--FILE--
<?php
foreach (array(
'-1',
'-0x412',
'0',
'1',
'1b',
'1k',
'1m',
'1g',
'1gb',
'14.2mb',
'14.2bm',
'boat'
) as $input) {
echo ini_parse_quantity( $input ) . PHP_EOL;
}
?>
--EXPECT--
-1
-1042
0
1
1
1024
1048576
1073741824
1
14
14680064
0
14 changes: 14 additions & 0 deletions tests/basic/ini_parse_quantity_warnings.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
ini_parse_quantity() function - warns when given inappropriate values
--INI--
error_reporting = E_ALL
--FILE--
<?php
ini_parse_quantity('-1');
ini_parse_quantity('1mb');
ini_parse_quantity('256 then skip a few then g')
?>
--EXPECTF--
Warning: Invalid quantity "1mb": unknown multipler "b", interpreting as "1" for backwards compatibility in %sini_parse_quantity_warnings.php on line 3

Warning: Invalid quantity "256 then skip a few then g", interpreting as "256 g" for backwards compatibility in %sini_parse_quantity_warnings.php on line 4

0 comments on commit 492af9f

Please # to comment.