Re:Opera moves to WebKit
by papsosouid on Wednesday February 13, 2013 @12:18PM
(#5214279)
No, because strtol gives you the ability to see that the parse failed and where. You chose to give it NULL there, but you could choose to use the function correctly. You can't do it correctly in the javascript and PHP cases. In the PHP case, it is actually just a bug in the PHP codebase that octal digits are supported at all, and they don't use strtol correctly. Note that perl, python and ruby all support octal digits correctly, there really is no excuse for PHP and javascript not to.
[ Parent
| on HN ]
> You can't do it correctly in the javascript ... case
Sure you can. Read the Fine manual: https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...
tldr: parseInt("08", 10);
Re:Opera moves to WebKit
by papsosouid on Wednesday February 13, 2013 @12:35PM
(#5214385)
We're not talking about parsing in base 10, we're talking about parsing in base 8. I need to be able to tell if the parse succeeded or not, silently hiding errors is bad. The correct behaviour demonstrated in perl and python:
$ perl -le 'print 07'
7
$ perl -le 'print 08'
Illegal octal digit '8' at -e line 1, at end of line
Execution of -e aborted due to compilation errors.
$ python -c 'print 07'
7
$ python -c 'print 08'
File "", line 1
print 08
^
SyntaxError: invalid tokenGot to admit, I am disappointed that
> parseInt("08", 8);
returns 0 instead of NaN
Returning 0 makes perfect sense. parseInt matches an integer from the beginning of the string, so it reads the 0 and then stops at the 8 because 8 is not a valid digit in base 8.
>>> parseInt("123foobar", 10)
123