Add hex/binary/octal support to String.toInt() - #1808
Open
HT154 wants to merge 1 commit into
Open
Conversation
HT154
force-pushed
the
string-to-int-radix
branch
from
August 3, 2026 18:17
47a7e2a to
e82d90f
Compare
HT154
force-pushed
the
string-to-int-radix
branch
2 times, most recently
from
August 3, 2026 21:34
614500f to
9a3d323
Compare
HT154
force-pushed
the
string-to-int-radix
branch
from
August 3, 2026 22:54
9a3d323 to
2fbc56f
Compare
bioball
requested changes
Aug 3, 2026
| protected long eval(String self) { | ||
| try { | ||
| return Long.parseLong(removeUnderlinesFromNumber(self)); | ||
| var negate = self.charAt(0) == '-'; |
Member
There was a problem hiding this comment.
This will throw if given an empty string.
Looks like we're missing this from our tests
| try { | ||
| return Long.parseLong(removeUnderlinesFromNumber(self)); | ||
| var negate = self.charAt(0) == '-'; | ||
| return VmUtils.parseInteger(negate ? self.substring(1) : self, negate, Long::parseLong); |
Member
There was a problem hiding this comment.
Long.parseLong() accepts - and + signs (e.g. Long.parseLong("-5")); so this would parse 0x-5 as -5, 0x+5 as 5, etc, whereas this isn't actually accepted by Pkl.
One solution is to just reject any sequence after 0x/0b/etc that isn't a digit. However, that's starting to feel like a lexer. Maybe we should just use our Lexer? That would keep this in sync with our grammar.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This also eliminates some duplicated number handling between
AstBuilderandStringNodes