Repository navigation
fix: parse bare decimal byte counts without a unit as bytes - #223
Open
amalsaidov wants to merge 1 commit into
Open
amalsaidov wants to merge 1 commit into
amalsaidov wants to merge 1 commit into
Conversation
A bare decimal such as "500.0" or "1.5" failed to parse, returning the
confusing error "couldn't parse \"\" into a known SI unit". This happened
because, unlike bare integers (handled by the u64 fast path), a decimal with
an empty unit suffix fell through to "".parse::<Unit>(), which errors.
Treat an empty suffix as plain bytes (Unit::Byte), so bare decimals parse the
same as bare integers ("500") and explicit "500.0 B". This also makes a bare
integer that overflows u64 saturate, matching the existing behaviour of the
same value with a "B" suffix.
Add a regression test covering bare decimals, truncation, and overflow.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A bare decimal with no unit fails to parse today, even though a bare integer does:
The error message —
couldn't parse "" into a known SI unit— shows the cause: for a decimal,from_strtakes the float path and then calls"".parse::<Unit>()on the (empty) suffix, which errors. Bare integers avoid this because they are handled by theu64fast path at the top offrom_str. So"500"works,"500.0"and"500.0 B"both resolve to500, but"500.0"alone errors.Fix
Treat an empty unit suffix as plain bytes (
Unit::Byte). A bare decimal then parses the same as a bare integer and as the explicit"… B"form:As a side effect this also makes a bare integer that overflows
u64saturate tou64::MAX, matching the behaviour the same value already has with an explicitBsuffix ("99999999999999999999B").Malformed inputs are unaffected: the suffix is only treated as bytes when it is empty. Inputs like
"","1.3.4","1 000 B", or"1.3 ... B"still error (covered by the existingwhen_errtest).Tests
Adds
bare_decimal_without_unit_is_bytescovering bare decimals, truncation, trailing-dot, and overflow. It fails before this change and passes after.Checks run locally:
cargo test --all-featuresand--no-default-features, doc-tests, andcargo fmt --check— all green.