Skip to content
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

Extract String#to_i impl to scolapasta-int-parse #2075

Open
lopopolo opened this issue Aug 12, 2022 · 0 comments
Open

Extract String#to_i impl to scolapasta-int-parse #2075

lopopolo opened this issue Aug 12, 2022 · 0 comments
Labels
A-parser Area: Parser implementations. A-ruby-core Area: Ruby Core types. A-spec Area: ruby/spec infrastructure and completeness.

Comments

@lopopolo
Copy link
Member

Reuse the radix type. Add tests.

From what I can tell, ASCII whitespace is trimmed from front and back like Kernel#Integer.

[3.1.2] > "  \t\n\u000A\u000C            123abc                \t\n\u000A\u000C       ".to_i(16)
=> 1194684
[3.1.2] > "  \t\n\u000A\u000C            123abc                \t\n\u000A\u000C       ".to_i
=> 123

0x-style prefixes are allowed but only if they match the given base or x is in the given base's alphabet. If the given base does not match the 0x-style prefix, 0 is returned.

[3.1.2] > "0x123abc".to_i
=> 0
[3.1.2] > "0x123abc".to_i(16)
=> 1194684
[3.1.2] > "0x123abc".to_i(24)
=> 0
[3.1.2] > "0x123abc".to_i(36)
=> 71897795832

Numbers are parsed from the front and parsing stops when encountering an ASCII alphanumeric not in the given base's char set:

[3.1.2] > "123abc".to_i
=> 123
[3.1.2] > "123abc".to_i(16)
=> 1194684
@lopopolo lopopolo added A-parser Area: Parser implementations. A-ruby-core Area: Ruby Core types. A-spec Area: ruby/spec infrastructure and completeness. labels Aug 12, 2022
@lopopolo lopopolo changed the title Extract String#to_s impl to scolapasta-int-parse Extract String#to_i impl to scolapasta-int-parse Oct 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: Parser implementations. A-ruby-core Area: Ruby Core types. A-spec Area: ruby/spec infrastructure and completeness.
Development

No branches or pull requests

1 participant