printf: format %S like GNU (%g, not one decimal) - #842
Open
MsfPablo wants to merge 1 commit into
Open
Conversation
GNU find prints the sparseness ratio with %g, so a file with 8 blocks
and 2 bytes shows as 2048 and one with 6 bytes as 682.667. We printed
{:.1} instead, giving 2048.0 and 682.7, and 1.0 instead of 1 for the
zero-length case.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #842 +/- ##
==========================================
+ Coverage 91.93% 92.05% +0.12%
==========================================
Files 35 35
Lines 7251 7353 +102
Branches 378 381 +3
==========================================
+ Hits 6666 6769 +103
+ Misses 443 441 -2
- Partials 142 143 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Commit f4f13ff has test result changes: bfs testsuite: |
sylvestre
reviewed
Aug 18, 2026
| /// | ||
| /// GNU find prints `%S` with `%g`, so a ratio of exactly 2048 comes out as | ||
| /// `2048` rather than `2048.0`, and 512*8/6 as `682.667` rather than `682.7`. | ||
| #[cfg(unix)] |
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.
I was diffing our
findagainst GNU findutils 4.11 across the-printfdirectives and%Scame out differently on every file that isn't a round ratio.GNU prints the sparseness ratio with
%g; we use{:.1}:So
%gdiffers from{:.1}in three ways that all show up here: 6 significant digits rather than 1 decimal place, trailing zeros and a trailing.stripped, and a switch to exponent form outside[1e-4, 1e6).Rust has no
%g, so this adds a smallformat_ghelper and routes%Sthrough it. The zero-length and non-unix branches returned the literal"1.0"and are now"1", matching the./emptyline above.The unit test's expected values are the output of C's
printf("%g", ...)under glibc withLC_ALL=C, including the rounding-changes-the-exponent case (999999.9->1e+06) and the exponent-form case (1234567->1.23457e+06). After the changefind -printf '%S %p\n'is byte-identical to GNU on my test tree.One thing I deliberately left alone: GNU honours
LC_NUMERICfor%S(it prints1365,33in a comma-decimal locale) while we always emit.. That's a separate matter from the formatting itself, so it felt out of scope here.