Add from_bytes_radix function #469
Labels
ACP-accepted
API Change Proposal is accepted (seconded with no objections)
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
T-libs-api
Proposal
Problem statement
If you have bytes (
&[u8]
) that contain ASCII coded number you can NOT parse it directly with current stdlib.You need to use
str::from_utf8
before callingfrom_str_radix
.This is a useless conversion, because
from_str_radix
should check if input contains only digits (0-9, a-z, A-Z),meaning it automatically validates if input is a valid utf-8 again.
And, in fact, internally
from_str_radix
works with bytes: https://github.com/rust-lang/rust/blob/80d0d927d5069b67cc08c0c65b48e7b6e0cdeeb5/library/core/src/num/mod.rs#L1477So from ergonomic point of view you do the useless thing (call
std::str::from_utf8
and handle its errors),and from the speed optimization point of view you also have to do the useless thing.
Motivating examples or use cases
Need to convert
&[u8]
to i16,i32,etc. frequently occurs inparsers of ASCII based protocols and file formats (like nmea or arinc-424 and so on). In these formats and protocols you need to convert ASCII digits to numbers.
For example, in rust nmea parser there is a conversion to utf-8 before start of any other parsing: https://github.com/AeroRust/nmea/blob/832895945a82f5248473d0809dca46d805541132/src/parse.rs#L187
However, NMEA is not a UTF-8-based protocol, it is just too complex to call
str::from_utf8
for any byte rangethat are coded numbers, so it is simpler to convert all input to utf-8 and only then parse it.
Solution sketch
Add
<NumberType>::from_bytes_radix(src: &[u8], radix: u32) -> Result<NumberType, ParseIntError>
.This function almost already exists, the main part of
from_str_radix
deals with bytes, not utf-8 characters.Links and related work
#287 suggests the similar thing, but much wider with an introduction of a new trait,
I suggest to make just one more function.
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: