-
-
Notifications
You must be signed in to change notification settings - Fork 144
[PHP 8.2] Add ini_parse_quantity
function
#411
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
Conversation
Polyfill taken from [PHP.Watch - PHP 8.2: New `ini_parse_quantity` function](https://php.watch/versions/8.2/ini_parse_quantity#polyfill) Tries to mimic the native warnings with `E_USER_WARNING` errors, and overflow/underflow conditions by inspecting the int to float type change.
Tried adding the following tests, but they don't seem to work for some reason:
|
// Removing whitespace characters. | ||
$shorthand = preg_replace('/\s/', '', $shorthand); | ||
|
||
$suffix = strtoupper(substr($shorthand, -1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case 'K':case 'k':
better than strtoupper()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was hesitant about it because it would require adding duplicate case
lines for all three current suffixes.
} | ||
|
||
// If there is no suffix, return the integer value with the sign. | ||
if (preg_match('/^\d+$/', $shorthand, $matches)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$value = (float) $shorthand;
if ($value == $shorthand) {
return $value;
}
$suffix = substr($shorthand, -1);
you can try add anchors |
case 'M': | ||
$multiplier *= 1024 * 1024; | ||
break; | ||
case 'G': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is missing support for setting the base (using o
, x
or b
characters). See: https://github.com/php/php-src/blob/master/Zend/zend_ini.c#L550
Please add them also in the test cases. Also, what about edge cases, like combining all multipliers at once, like 123KMG
? You can look for inspiration at https://github.com/php/php-src/tree/master/Zend/tests/zend_ini
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ayesh I've tried to implement above on my fork - please have a look and maybe you can get some inspiration from there: https://github.com/IonBazan/polyfill/tree/feature/parse_ini_quantity
It's almost working - only the error message seems to be slightly different than on PHP 8.2.
If it wasn't for the error-messages, this would be a trivial function to polyfill :-) Here is a simple, brute-force test script I wrote while trying to create my own implementation.
|
What's the status on this? |
The C implementation is here: Would anyone be interested in porting this to PHP? |
I'm closing to signal that someone else should take over. PR welcome. |
I have done this and have a working solution. (I need it for one of my own projects.) Using the brute-force test technique above, it gives the same output and errors as the core function for all strings up to 7 characters.
Would you want me to create a PR now (with this rather hideous code) or wait until I have time to clean it up (which might not be soon)? |
I've submitted PR #439 |
…in PHP (fisharebest) This PR was squashed before being merged into the 1.x branch. Discussion ---------- Add polyfill for ini_parse_quantity based on the C code in PHP See #411 for a previous attempt to polyfill this function. The `ini_parse_quantity()` is difficult to polyfill as the error handling is non-trivial, and several invalid inputs are treated as valid. This polyfill is a simple/direct implementation of the C code from PHP. As such it uses `goto`s and pointer-based string access. I am fully aware that this would fail most code reviews :-) However it works and I hope it will be useful until a cleaner implementation can be provided. The unit tests provide 100% code coverage. I have also included (but commented out) a brute-force test which takes many hours to run. I found it was very helpful in finding edge cases. It may be useful for anyone who tries to refactor the code. The two overflow tests will probably fail on a 32bit build of PHP, however the code itself should work. I just haven't been able to work out the expected values. Commits ------- d07580f Add polyfill for ini_parse_quantity based on the C code in PHP
Polyfill taken from PHP.Watch - PHP 8.2: New
ini_parse_quantity
functionTries to mimic the native warnings with
E_USER_WARNING
errors, and overflow/underflow conditions by inspecting the int to float type change.