Skip to content

Add hex/binary/octal support to String.toInt() - #1808

Open
HT154 wants to merge 1 commit into
apple:mainfrom
HT154:string-to-int-radix
Open

Add hex/binary/octal support to String.toInt()#1808
HT154 wants to merge 1 commit into
apple:mainfrom
HT154:string-to-int-radix

Conversation

@HT154

@HT154 HT154 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

This also eliminates some duplicated number handling between AstBuilder and StringNodes

@HT154
HT154 force-pushed the string-to-int-radix branch from 47a7e2a to e82d90f Compare August 3, 2026 18:17
@HT154 HT154 mentioned this pull request Aug 3, 2026
@HT154
HT154 force-pushed the string-to-int-radix branch 2 times, most recently from 614500f to 9a3d323 Compare August 3, 2026 21:34
@HT154
HT154 force-pushed the string-to-int-radix branch from 9a3d323 to 2fbc56f Compare August 3, 2026 22:54
protected long eval(String self) {
try {
return Long.parseLong(removeUnderlinesFromNumber(self));
var negate = self.charAt(0) == '-';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants