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

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.

@codecov

codecov Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.24390% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.10%. Comparing base (b861b03) to head (1901f04).

Files with missing lines Patch % Lines
src/find/matchers/printf.rs 90.24% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #842      +/-   ##
==========================================
+ Coverage   93.07%   93.10%   +0.03%     
==========================================
  Files          37       37              
  Lines        8131     8168      +37     
  Branches      404      407       +3     
==========================================
+ Hits         7568     7605      +37     
+ Misses        424      423       -1     
- Partials      139      140       +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

codspeed Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

Warning

uutils has exceeded the Hobby plan limit of 5 active contributors. Upgrade your plan or apply to the open source program by December 8, 2026 to keep using CodSpeed.

1 warning

⚠️ 15 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 20 untouched benchmarks


Comparing MsfPablo:fix-printf-sparseness (35f1f59) with main (7a9df76)

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 ?

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.

@MsfPablo ping?

@sylvestre

Copy link
Copy Markdown
Contributor

@MsfPablo last ping before i close it

let decimals = (PRECISION - 1 - exponent).max(0) as usize;
trim(&format!("{value:.decimals$}")).to_string()
} else {
let formatted = format!("{:.*e}", (PRECISION - 1) as usize, value);

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.

same format! as line 45, could be reused, no?

format_g((meta.blocks() * STANDARD_BLOCK_SIZE) as f64 / (meta.len() as f64)).into()
} else {
"1.0".into()
"1".into()

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.

please add a test running -printf '%S' through the matcher too (an empty file giving 1 is easy)

/// Expected values come from C's `printf("%g", value)` (glibc, LC_ALL=C).
#[cfg(unix)]
#[test]
fn format_g_matches_c_printf() {

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.

please move this test below the use lines

@sylvestre

Copy link
Copy Markdown
Contributor

hi, any news on the 4 comments above? thanks

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.
@sylvestre
sylvestre force-pushed the fix-printf-sparseness branch from 1901f04 to 35f1f59 Compare November 25, 2026 02:15
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