Repository navigation
uucore: break locale_cmp ties by bytes - #16150
unsound-mint wants to merge 1 commit into
Conversation
Merging this PR will not alter performance1 warning, 1 note
Comparing Footnotes
|
94d1eb1 to
ce5dbcd
Compare
|
GNU testsuite comparison: |
|
thanks for tackling this! |
| .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 |
There was a problem hiding this comment.
this also fixes the sort -u -k case from the issue, please add a test in tests/by-util/test_sort.rs too
There was a problem hiding this comment.
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)), |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
f8078ab to
ba33706
Compare
| /// 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". |
There was a problem hiding this comment.
could you please make this comment shorter? 2 lines should be enough :)
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
with this, the cmp == Ordering::Equal fallback in compare_lines_with_collator (sort.rs) is dead code, please remove it
There was a problem hiding this comment.
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
ba33706 to
6f3ce67
Compare
In a non-C locale,
expr,commandjointreat strings that differ only in punctuation or zero-width characters as equal, becauselocale_cmpreturns the collator'sEqual, which only means the strings sort at the same position.expr '/srv/uploads/../..' = /srv/uploadsprints 1,comm -12lists the two as common lines, andjoin -t:pairsad-minwithadmin.sort -u -kalso dropped one of two keys that differ only in such characters.locale_cmpnow breaks such ties by the reversed bytes, which is the tie ordersortalready uses for whole lines (including-m). Equality therefore means the same string, andcomm,join,lsandtestaccept and list ties in the same order assortand GNU. The reversed-bytes fallback thatsort.rsran afterlocale_cmpis now dead code and is removed. Added tests for expr, comm (including--check-orderonsortoutput), join,sort -u -kandsort -m.Closes #15752