-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Change String#to_i to parse octals with prefix 0o #7691
Change String#to_i to parse octals with prefix 0o #7691
Conversation
Turns out this would also break YAML parsing of integers. The tests show that the YAML parser still behaves in the old way, where a |
This is a breaking change. Previously, numbers starting with 0 would be parsed as octals. A previous change in Crystal requires octals start with 0o. Now that is not the case, and they will be parsed as base-10. The 0o prefix must be present to treat it as an octal. This addresses parsing 0 with prefix: true raising an error. Additionally, YAML parsing used the old style. This updates the spec to handle the new parsing. There is no official documentation for YAML (that I'm aware of) for octals.
Maybe a |
I agree. There's a lot of places that still use a leading zero for octal notation. Linux permissions being one of them - 0755, 0644. Don't want to break that. |
src/string.cr
Outdated
else | ||
if leading_zero_is_octal | ||
base = 8 | ||
ptr -= 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.
Why decrement the pointer?
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.
After looking closer, I don't think it is needed. It was to cover the case where there's a leading zero, but no o
right after it. It would handle strings like 0123
and 0abc
(invalid). I think I put it in because there's no prefix character (like b or x), so it needed to back up.
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.
Travis build 15307.1 failed when this was removed. Looks like it broke parsing of !!int 0
in YAML. I'll see if there's a way I can address this without decrementing the pointer, unless you're ok with putting it back in.
The YAML changes look fine. |
This allows parsing of octals in strings that use just 0 as a prefix instead of 0o. When this flag is true, the prior behavior for parsing octals is used. YAML parsing has been updated to accept 0 and 0o prefixes for octals.
Sorry, I tried to fix the conflicts after merging another PR and I forgot some stuff (I think default values). @icy-arctic-fox Do you have time to fix this? I think CI passed fine, it just had a hiccup in the previous run. |
Looks like it should be fixed. Somehow the merge dropped one default value. |
This is a breaking change.
Previously, numbers starting with 0 would be parsed as octals. Now that is not the case, and they will be parsed as base-10. The
0o
prefix must be present to treat it as an octal. This addresses parsing 0 withprefix: true
raising an error.This PR should resolve #7689
Update:
The current behavior in Crystal can be preserved by adding
leading_zero_is_octal: true
to the#to_i
calls. By default however,leading_zero_is_octal
is false and expects0o
as a prefix. The YAML parser uses this so that both formats are recognized.