dtolnay/itoa

Support other radix

Closed this issue · 6 comments

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?

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

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

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.

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

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.

@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.