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

[SDK Library] Consider supporting parsing binary integers #45063

Open
parlough opened this issue Feb 20, 2021 · 1 comment
Open

[SDK Library] Consider supporting parsing binary integers #45063

parlough opened this issue Feb 20, 2021 · 1 comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug

Comments

@parlough
Copy link
Member

parlough commented Feb 20, 2021

For some content I'm working on it would be nice if int.parse and int.tryParse natively supported parsing binary integers prefixed with 0b similar to how it handles hexadecimal 0x ones currently.

Implementing this work now would also simplify the implementation of binary integer literals in the future as included in the considered small and useful features list documented in the language repo. The CFE/analyzer currently use the method for their conversion of the literals to consistent decimal values.

Analyzer:
https://cs.opensource.google/dart/sdk/+/master:pkg/analyzer/tool/summary/mini_ast.dart;l=478
https://cs.opensource.google/dart/sdk/+/master:pkg/analyzer/lib/src/fasta/ast_builder.dart;l=2972

CFE:
https://cs.opensource.google/dart/sdk/+/master:pkg/front_end/lib/src/fasta/kernel/body_builder.dart;l=2444

@parlough parlough changed the title [SDK] Consider supporting parsing binary integers [SDK Library] Consider supporting parsing binary integers Feb 20, 2021
@lrhn lrhn added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug labels Feb 21, 2021
@lrhn
Copy link
Member

lrhn commented Mar 1, 2021

I'm not particularly happy with supporting 0xF00 as input to int.parse now. I'd have preferred to not support that, and only support inputs which can come from int.toString(), along with int.parseRadix to match toRadixString and perhaps int.parseLiteral to handle things that have alternative representations like 0x or 0b prefixes.

Doing everything in one function makes that function slower and more complicated, and every caller needs to pay for that, whether they use it or not.
Maybe we should just add int.parseRadix(String source, int radix, [int start = 0, int end]) which can parse a substring at any radix, then you can do if (string.startsWith("0b")) return int.parseRadix(string, 2, 2);. Having to create a substring to pass it to int.parse is also an overhead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants