Repository navigation
feat(sort): add -h/--human-numeric-sort - #216
Conversation
c79c4f1 to
db19566
Compare
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
TL;DR: approve. The -h comparator matches GNU sort 9.1 on everything realistic I threw at it (~1,900 differential cases, no panics). There's one 🟡 that doesn't block: sort -hu can print lines that are byte-for-byte identical.
🟡 -hu keeps exact duplicates when a value-equal line sorts between them. printf '1K\n1k\n1K\n' \| sort -hu prints 1K 1k 1K, while GNU prints 1K. The cause is src/commands/sort.rs:266-274: dedup_by compares the raw key text instead of using the sort comparator, so it only drops adjacent identical lines. This goes a bit beyond the gap you scoped out, because -u ends up emitting real duplicates. -n already has the same bug on main (1/01/1). The fix is one line, so it could go here or in your follow-up (see below and the inline test suggestion).
📝 Docs: harness-sdk site/src/content/docs/user-guide/shell/commands.mdx:35 still says No ... -h (-h incorrectly prints help). That needs a small follow-up PR in that repo once this merges.
Fix for sort.rs:266-274 (verified)
It's outside the diff, so I can't attach a suggestion block there:
if unique {
lines.dedup_by(|a, b| cmp(a, b) == std::cmp::Ordering::Equal);
}This matches GNU (-u uses the comparator) and fixes -n too (1/01/1 → 1). With this change and the stronger test from the inline suggestion, cargo test --test shell_integration sort passes 46/46. The stronger test fails at db19566 (left: "1K\n1k\n1K\n1M").
✅ What I verified
- Head
db19566:cargo test --test shell_integration sort→ 46 passed, 0 failed. The fullshell_integrationrun had 1631 passed. - Ran a differential against
LC_ALL=C /usr/bin/sort -s(GNU 9.1) with the patterns below. All matched.du -h-style output, including CRLF- negatives and zeros (
-0K,-.5K) - inputs like
+5K,1,000K,1KiB,1e3,KK,.,- - flag combos:
-rh,-k2h,-t: -k2,2hr,-bk2h,-hf, multiple files
- Panic probes (
1é,-é,.é, 400-digit numbers) ran cleanly.scan_numberonly advances over ASCII, sos[..i]can't split a UTF-8 character. sort --helpstill prints usage. All 7 new expectations match GNU, and every one of them fails on main.- I didn't run clippy (it isn't installed in my sandbox). CI was still pending when I reviewed.
Appendix: non-blocking, or already on main (6)
- ⚪ GNU rejects
sort -hn(options '-hn' are incompatible, rc=2). This PR silently uses-hinstead. It's a contrived combination, and leaving it lenient seems fine. - ⚪
--human-numeric-sortworks but isn't in the--helptext (sort.rs:9lists only-h). - Already on main, same as
-n: global ordering flags get ORed into a key that has its own flags (sort -h -k2r). GNU doesn't inherit them in that case. - Already on main:
-k2.2h/-k2.2nfail withinvalid digit found in string(parse_key_spec,sort.rs:84-85), because start.char offsets aren't supported. Separately,-kNstops at field N instead of running to end of line (extract_key). - Already on main: an f64 tie on numbers with 20+ digits. The
-npath behaves the same. - Already on main:
trim_start()treats a leading\r, NBSP, or other Unicode space as blank, while GNU in the C locale skips only space and tab.
|
Thanks for the differential checks. I confirmed the shared The command-reference entry in The rebased head passed the full local Rust suite, doc tests, formatting, clippy, and docs with warnings denied. Eight CLI comparisons against system sort also passed. CI is still finishing on this revision. |
Description
sort -hprinted the usage text and exited 0, so something likedu | sort -hquietly turned into help text in the middle of a pipeline:Now
-h(and--human-numeric-sort) sorts human-readable sizes the way GNU does: first by sign and suffix (K/k, M, G, ... Y), then by the number. It also works as a key flag, like-k2hor-k2,2hr.--helpstill prints the help, and the other sort modes don't change.I compared it against GNU sort on a mix of sizes, negatives, zeros, blanks and plain text, with
-r,-s,-kand-t, and the output matched. One gap I left out: when two lines compare as equal, GNU breaks the tie by comparing the whole line, and-udrops lines that are equal by value. This sort doesn't do either of those today, and-nhas the same problem, so I think it belongs in its own fix.Related Issues
Part of #207
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.