Skip to content

uucore: break locale_cmp ties by bytes - #16150

Open
unsound-mint wants to merge 1 commit into
uutils:mainfrom
unsound-mint:locale-cmp-equal-bytes
Open

unsound-mint wants to merge 1 commit into
uutils:mainfrom
unsound-mint:locale-cmp-equal-bytes

Conversation

@unsound-mint

@unsound-mint unsound-mint commented Nov 2, 2026 •

Copy link
Copy Markdown

In a non-C locale, expr, comm and join treat strings that differ only in punctuation or zero-width characters as equal, because locale_cmp returns the collator's Equal, which only means the strings sort at the same position. expr '/srv/uploads/../..' = /srv/uploads prints 1, comm -12 lists the two as common lines, and join -t: pairs ad-min with admin. sort -u -k also dropped one of two keys that differ only in such characters.

locale_cmp now breaks such ties by the reversed bytes, which is the tie order sort already uses for whole lines (including -m). Equality therefore means the same string, and comm, join, ls and test accept and list ties in the same order as sort and GNU. The reversed-bytes fallback that sort.rs ran after locale_cmp is now dead code and is removed. Added tests for expr, comm (including --check-order on sort output), join, sort -u -k and sort -m.

Closes #15752

@codspeed

codspeed Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning, 1 note

⚠️ 126 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

ℹ️ 1 benchmark has no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

✅ 415 untouched benchmarks
⏩ 70 skipped benchmarks1


Comparing unsound-mint:locale-cmp-equal-bytes (6f3ce67) with main (ab69b86)

Open in CodSpeed

Footnotes

  1. 70 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@unsound-mint
unsound-mint force-pushed the locale-cmp-equal-bytes branch from 94d1eb1 to ce5dbcd Compare November 3, 2026 12:56
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/env/env is no longer failing!

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for tackling this!
This can't land yet: breaking ties forward in locale_cmp changes the tie order of sort -m (see the inline comment).
Please also add a test for the keyed sort -u case from the issue.

.map_or_else(|| left.cmp(right), |c| c.compare_utf8(left, right))
COLLATOR.get().map_or_else(
|| left.cmp(right),
// The collator returns `Equal` for strings that only differ in

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.

this also fixes the sort -u -k case from the issue, please add a test in tests/by-util/test_sort.rs too

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added sort -u -k1,1 with keys that differ only by U+200B.

// characters it gives no weight, like punctuation or zero-width
// characters. Callers use `Equal` to mean "same string", so break
// such ties by bytes.
|c| c.compare_utf8(left, right).then_with(|| left.cmp(right)),

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.

this changes sort -m too: compare_lines_with_collator in sort.rs breaks ties with reversed bytes, now it never gets Equal and uses forward order, so it no longer matches the precomputed-key path
could you please keep sort's order (e.g. let sort call the raw collator) and add a sort -m test with 01 / 0_1?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

locale_cmp now breaks ties by reversed bytes, the same as sort's whole-line order, so sort -m is unchanged and locale_cmp_raw is gone. Added sort -m with 01 / 0_1. comm and join accept the output of sort, and GNU gives the same results.

@unsound-mint
unsound-mint force-pushed the locale-cmp-equal-bytes branch 3 times, most recently from f8078ab to ba33706 Compare November 9, 2026 08:13
/// punctuation or zero-width characters, so such ties are broken by the
/// reversed bytes. That is the tie order `sort` uses for whole lines, which
/// keeps `comm`, `join` and `ls` consistent with the output of `sort` and
/// lets callers read `Equal` as "same string".

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.

could you please make this comment shorter? 2 lines should be enough :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Shortened it to two lines.

.get()
.map_or_else(|| left.cmp(right), |c| c.compare_utf8(left, right))
COLLATOR.get().map_or_else(
|| left.cmp(right),

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.

with this, the cmp == Ordering::Equal fallback in compare_lines_with_collator (sort.rs) is dead code, please remove it

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed the fallback in compare_lines_with_collator.

The collator returns Equal for strings that only differ in characters it
gives no weight, such as punctuation or zero-width characters. expr, comm
and join read Equal as "same string", so in a non-C locale
`expr /srv/uploads/../.. = /srv/uploads` printed 1, comm listed the two
as common lines and join paired keys like ad-min and admin. sort -u with
a key dropped one of two keys that only differ in such characters.

Fall back to comparing the reversed bytes when the collator says Equal.
This keeps the order total and consistent with equality, and it is the
tie order sort already uses for whole lines, including with -m. comm and
join therefore accept the output of sort, and ls and test list such ties
in the same order as GNU.

sort's own reversed-bytes fallback after locale_cmp is now dead code and
is removed.

Closes uutils#15752
@unsound-mint
unsound-mint force-pushed the locale-cmp-equal-bytes branch from ba33706 to 6f3ce67 Compare November 27, 2026 19:17

This branch has not been deployed

No deployments
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.

expr, comm, join: string equality ignores punctuation in non-C locales

2 participants