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

Support other radix #15

Closed
glandium opened this issue Mar 17, 2018 · 6 comments
Closed

Support other radix #15

glandium opened this issue Mar 17, 2018 · 6 comments

Comments

@glandium
Copy link
Contributor

I'm interested in hexadecimal output in the same vein as itoa::fmt, and to do that I've extracted the code from libcore.

The C function itoa takes an argument for the radix, so I thought it would make sense to add the code to this crate, if you're interested. The question is what should the API look like. libcore provides a few traits for Binary, Octal, and Hexa, I think it would make sense to expose similarly named traits with the same functions as in the itoa::Integer trait.

What do you think?

@dtolnay
Copy link
Owner

dtolnay commented Mar 17, 2018

Check out the implementation and discussion in rust-lang/rust#46281 -- some promising ideas there as well.

@glandium
Copy link
Contributor Author

Ultimately, I do need something that can push to a fmt::Write, though.

@glandium
Copy link
Contributor Author

Note that with specialization, there's a "cheap" thing that core::fmt could do for the Binary, Octal, LowerHex and UpperHex traits: change their method to fn fmt<W: fmt:Write>(&self, &mut W) -> fmt::Result, and fn fmt(&self, &mut fmt::Formatter) -> fmt::Result would be a specialization of that. But I'm more interested in something I can use now.
And if there was a similar Decimal trait, it could do the same thing and that would essentially be a libcore replacement for a combination of issues #5 and #3.

@Alexhuszagh
Copy link

@dtolnay I'd be happy to furnish code from rust-lexical, which supports this functionality, to do just that.

@dtolnay
Copy link
Owner

dtolnay commented Aug 7, 2019

I am leaning toward keeping itoa focused on base 10. Is rust-lexical already usable as an itoa replacement if someone needs a different radix? That would be best. Otherwise maybe one of you could maintain a fork of itoa or make a new crate that does non-base-10.

@Alexhuszagh
Copy link

@dtolnay Yes, it supports all radices (with the radix feature, only base10 by default) and has similar performance to itoa, so it's definitely a viable alternative. So if this feature isn't desired, I'd recommend just using that. If you ever want to support other radices, I'd love to give commits forward: you've contributed so much to the ecosystem, the least I can do is pay a small fraction of that back.

@dtolnay dtolnay closed this as completed Dec 12, 2021
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

No branches or pull requests

3 participants