-
-
Notifications
You must be signed in to change notification settings - Fork 58
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
A from_str(_radix) analogue #64
Comments
Yeah, I do kind of think this sort of thing is in scope. I've certainly wanted functions like this in the past as well. Adding these sorts of routines for integers seems pretty reasonable, but this does open the door for wanting analogous parsing routines for floating point. That is definitely not a road I want to go down. It looks like the lexical project is already meeting this need anyway: https://docs.rs/lexical-core/0.7.4/lexical_core/ So I'd probably want to draw a line in the sand here: integer parsing APIs are okay, but not floats. |
This feels like a good "first issue" to me. In particular, routines could be added that are implemented by first converting the input to a For anyone who wants to work on this, it would probably be good to start by posting an API proposal in this issue. The API is probably the hardest part of this issue to be honest. For example, do we want a new sub-module with a bunch of concrete functions for each integer? Do we want to define a trait that we implement for all of the integer APIs? I kind of lean towards the latter. |
API Proposal:
|
@alexjago Thanks! Nice work. Some responses:
The "bstr" in the Feel free to submit a PR and we can work through the actual code itself. |
Requiring Rust 1.55 is fine. I'm going to bump the MSRV of |
There's another API question which occurred to me around panicking and allocating. The stdlib function (and therefore this one, for now) has an That's an allocation, right? I don't think any other part of the functionality really needs to allocate. So it's probably fixable with some clever But it also might be an option to redesign the API so that out-of-range radices are a recoverable error, presumably with an additional variant of |
You're saying an assert is an alloc? No, it isn't. Let's stick with how std does things I think wherever we can. The radix is almost always going to be a static input determined by the programmer, so it seems sensible to panic for invalid radix values. |
Good to know that asserts don't alloc. (I got this misunderstanding because the And yes, I agree that sticking to how std does things makes life more straightforward for everyone. |
Well, the assert might alloc when the assert fails. Sure. But that's okay. |
Working with mostly-UTF-8 byte strings, the two biggest things my current project is missing are substring matching (which this crate provides) and integer parsing (which
std
provides only forstr
).This is easily worked around by calling
str::from_utf8
first, because if the input is not valid UTF-8 then it would never make it through afrom_str
/parse
orfrom_str_radix
anyway.But it would be convenient to have these functions in
bstr
as well, to avoid the overhead from that redundantfrom_utf8
call.The text was updated successfully, but these errors were encountered: