-
Notifications
You must be signed in to change notification settings - Fork 35
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
abuse bitwise ops to check for num vs string to avoid B.pm #18
Conversation
B.pm is a rather large module to load, and numbers and strings can be detected by abusing the behavior of bitwise operators. Using a bitwise operator is faster as well. A string & "" results in "" A number & "" results in 0 (with a warning) Some variables won't be fully caught by this check alone. A string like "0.0" that has been used in a numeric context will pass the check as if it was a number. We detect this by making sure the string is the same as stringifing the numeric form. "nan" and "inf" that have been used as numbers will also pass that check, so we detect them by making sure multiplying by 0 gives 0.
This code also isn't effected by the deprecation added in perl 5.23.6 of using bitwise ops on code points >0xFF. Because one of the strings we are &ing is an empty string, processing stops before it gets to any of the actual characters in the value we're checking. This avoids tripping the deprecation warning, so it should also avoid a future promotion of that deprecation to an exception. |
- we need to modify this part anyway because inf/nan is not valid input from the start, and their representations vary depending on the OS
Thanks for the cool patch. I tweaked it a bit to address the inf/nan issue later, in a different issue (#14, and makamaka/JSON#21 ). |
…master - merged only t/117_numbers.t as the issue has already been addressed by #18
It seems that in perl 5.28, it will be a fatal error if an operand contains a character whose ordinal value is above 0xFF. https://perl5.git.perl.org/perl.git/commitdiff/fac71630f045e7ab325b37548d59b6558735e5c3 In fact, with the current blead, the hack
@haarg What should we do? |
That seems like a bug in blead. This PR may have to be reverted, but the current change in blead is taking something that previously didn't warn and makes it fatal, which doesn't follow the deprecation policy. |
Indeed.
|
@haarg, thanks for starting a thread in p5p ML. |
It seems that the behavior was restored for a while. Thanks, @haarg ! |
B.pm is a rather large module to load, and numbers and strings can be
detected by abusing the behavior of bitwise operators. Using a bitwise
operator is faster as well.
A string & "" results in ""
A number & "" results in 0 (with a warning)
Some variables won't be fully caught by this check alone. A string like
"0.0" that has been used in a numeric context will pass the check as if
it was a number. We detect this by making sure the string is the same
as stringifing the numeric form.
"nan" and "inf" that have been used as numbers will also pass that
check, so we detect them by making sure multiplying by 0 gives 0.