Skip to content

printf: format %S like GNU (%g, not one decimal) - #842

Open
MsfPablo wants to merge 1 commit into
uutils:mainfrom
MsfPablo:fix-printf-sparseness
Open

printf: format %S like GNU (%g, not one decimal)#842
MsfPablo wants to merge 1 commit into
uutils:mainfrom
MsfPablo:fix-printf-sparseness

Conversation

@MsfPablo

Copy link
Copy Markdown
Contributor

I was diffing our find against GNU findutils 4.11 across the -printf directives and %S came out differently on every file that isn't a round ratio.

GNU prints the sparseness ratio with %g; we use {:.1}:

$ LC_ALL=C gfind . -printf '%S %s %b %p\n' | sort      $ LC_ALL=C find . -printf '%S %s %b %p\n' | sort
0 224 0 .                                               0.0 224 0 .
1 0 0 ./empty                                           1.0 0 0 ./empty
682.667 6 8 ./sub/b.log                                 682.7 6 8 ./sub/b.log
1365.33 3 8 ./a.txt                                     1365.3 3 8 ./a.txt
2048 2 8 ./sub/deep/c.md                                2048.0 2 8 ./sub/deep/c.md

So %g differs 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 small format_g helper and routes %S through it. The zero-length and non-unix branches returned the literal "1.0" and are now "1", matching the ./empty line above.

The unit test's expected values are the output of C's printf("%g", ...) under glibc with LC_ALL=C, including the rounding-changes-the-exponent case (999999.9 -> 1e+06) and the exponent-form case (1234567 -> 1.23457e+06). After the change find -printf '%S %p\n' is byte-identical to GNU on my test tree.

One thing I deliberately left alone: GNU honours LC_NUMERIC for %S (it prints 1365,33 in a comma-decimal locale) while we always emit .. That's a separate matter from the formatting itself, so it felt out of scope here.

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

codecov Bot commented Aug 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.28571% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.05%. Comparing base (1f19cdd) to head (f4f13ff).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
src/find/matchers/printf.rs 94.28% 1 Missing and 1 partial ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed-hq

codspeed-hq Bot commented Aug 17, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing MsfPablo:fix-printf-sparseness (f4f13ff) with main (9e778f1)

Open in CodSpeed

@github-actions

Copy link
Copy Markdown

Commit f4f13ff has test result changes:

bfs testsuite:

Test results comparison:
  Current:   TOTAL: 312 / PASSED: 266 / FAILED: 40 / SKIPPED: 6
  Reference: TOTAL: 314 / PASSED: 266 / FAILED: 42 / SKIPPED: 6

Changes from main branch:
  TOTAL: -2
  PASSED: +0
  FAILED: -2

Test improvements (2):
  + gnu/files0_from_ok
  + gnu/okdir_path_empty

///
/// 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)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why only unix ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants