Skip to content

Commit f8078ab

Browse files
committed
uucore: break locale_cmp ties by bytes
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. Closes #15752
1 parent 17fc1a5 commit f8078ab

5 files changed

Lines changed: 137 additions & 3 deletions

File tree

‎src/uucore/src/lib/features/i18n/collator.rs‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,22 @@ pub fn compute_sort_key_utf8(input: &[u8], buf: &mut Vec<u8>) {
8888
}
8989

9090
/// Compare both strings with regard to the current locale.
91+
///
92+
/// Returns `Equal` only for identical bytes. The collator also says `Equal`
93+
/// for strings that differ only in characters it gives no weight, such as
94+
/// punctuation or zero-width characters, so such ties are broken by the
95+
/// reversed bytes. That is the tie order `sort` uses for whole lines, which
96+
/// keeps `comm`, `join` and `ls` consistent with the output of `sort` and
97+
/// lets callers read `Equal` as "same string".
9198
pub fn locale_cmp(left: &[u8], right: &[u8]) -> Ordering {
9299
// If the detected locale is 'C', just do byte-wise comparison
93100
if get_collating_locale().0 == DEFAULT_LOCALE {
94101
left.cmp(right)
95102
} else {
96103
// Fall back to byte comparison if collator is not available
97-
COLLATOR
98-
.get()
99-
.map_or_else(|| left.cmp(right), |c| c.compare_utf8(left, right))
104+
COLLATOR.get().map_or_else(
105+
|| left.cmp(right),
106+
|c| c.compare_utf8(left, right).then_with(|| right.cmp(left)),
107+
)
100108
}
101109
}

‎tests/by-util/test_comm.rs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,44 @@ fn test_locale_collation() {
790790
.stdout_only("a1\n");
791791
}
792792

793+
#[uutests::wasi_ignore(NoLocaleData)]
794+
#[test]
795+
#[cfg(unix)]
796+
fn test_locale_collation_accepts_the_tie_order_of_sort() {
797+
// The collator gives U+200B no weight, so `ab` and `a\u{200b}b` tie there.
798+
// `sort` puts the longer one first, and `comm` must read that as sorted.
799+
let ts = TestScenario::new(util_name!());
800+
ts.fixtures.write("one", "a\u{200b}b\nab\n");
801+
ts.fixtures.write("two", "ab\n");
802+
803+
ts.ucmd()
804+
.env("LC_ALL", "en_US.UTF-8")
805+
.args(&["--check-order", "one", "two"])
806+
.succeeds()
807+
.stdout_only("a\u{200b}b\n\t\tab\n");
808+
}
809+
810+
#[uutests::wasi_ignore(NoLocaleData)]
811+
#[test]
812+
#[cfg(unix)]
813+
fn test_locale_collation_does_not_match_punctuation_variants() {
814+
// The collator gives no weight to `/` and `.`, but these lines differ.
815+
let locale = "en_US.UTF-8";
816+
if !is_locale_available(locale) {
817+
return;
818+
}
819+
let scene = TestScenario::new(util_name!());
820+
scene.fixtures.write("allowed", "/srv/uploads\n");
821+
scene.fixtures.write("requested", "/srv/uploads/../..\n");
822+
823+
scene
824+
.ucmd()
825+
.env("LC_ALL", locale)
826+
.args(&["-12", "allowed", "requested"])
827+
.succeeds()
828+
.no_stdout();
829+
}
830+
793831
#[test]
794832
#[cfg(unix)]
795833
fn test_c_locale_still_orders_by_bytes() {

‎tests/by-util/test_expr.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,26 @@ mod locale_aware {
16201620
.stdout_only(output);
16211621
}
16221622
}
1623+
1624+
#[uutests::wasi_ignore(NoLocaleData)]
1625+
#[test]
1626+
fn test_expr_equality_is_exact_in_locale() {
1627+
// The collator gives no weight to punctuation, but `=` must still
1628+
// tell these strings apart.
1629+
for (left, right) in [("/srv/uploads/../..", "/srv/uploads"), ("r.o.o.t", "root")] {
1630+
new_ucmd!()
1631+
.args(&[left, "=", right])
1632+
.env("LC_ALL", "en_US.UTF-8")
1633+
.run()
1634+
.code_is(1)
1635+
.stdout_only("0\n");
1636+
new_ucmd!()
1637+
.args(&[left, "!=", right])
1638+
.env("LC_ALL", "en_US.UTF-8")
1639+
.succeeds()
1640+
.stdout_only("1\n");
1641+
}
1642+
}
16231643
}
16241644

16251645
/// Tests for multibyte character arithmetic in expr

‎tests/by-util/test_join.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,24 @@ fn test_locale_collation() {
897897
.stdout_contains("ab:d 1 x");
898898
}
899899

900+
#[uutests::wasi_ignore(NoLocaleData)]
901+
#[cfg(unix)]
902+
#[test]
903+
fn test_locale_collation_does_not_pair_punctuation_variants() {
904+
// The collator gives no weight to `-`, but the keys differ.
905+
let ts = TestScenario::new(util_name!());
906+
let at = &ts.fixtures;
907+
908+
at.write("accounts", "admin:1\n");
909+
at.write("requests", "ad-min:2\n");
910+
911+
ts.ucmd()
912+
.env("LC_ALL", "en_US.UTF-8")
913+
.args(&["-t:", "accounts", "requests"])
914+
.succeeds()
915+
.no_stdout();
916+
}
917+
900918
#[uutests::wasi_ignore(Usize32BitOverflow)]
901919
#[test]
902920
fn test_incompatible_fields_reports_exact_field_number() {

‎tests/by-util/test_sort.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3891,6 +3891,56 @@ fn test_consistent_sorting_with_i18n_collate() {
38913891
.stdout_is(expected_output);
38923892
}
38933893

3894+
#[test]
3895+
#[cfg(unix)]
3896+
fn test_unique_key_keeps_keys_the_collator_cannot_tell_apart() {
3897+
// The collator gives U+200B no weight, so these keys compare `Equal`
3898+
// there, but they are different strings and `-u` must keep both.
3899+
let locale = "en_US.UTF-8";
3900+
if !is_locale_available(locale) {
3901+
return;
3902+
}
3903+
let out = new_ucmd!()
3904+
.env("LC_ALL", locale)
3905+
.args(&["-u", "-k1,1"])
3906+
.pipe_in("ab x\na\u{200b}b y\n")
3907+
.succeeds()
3908+
.stdout_move_str();
3909+
// GNU orders the tie like a full sort does: by reversed bytes.
3910+
assert_eq!(out, "a\u{200b}b y\nab x\n");
3911+
}
3912+
3913+
#[test]
3914+
#[cfg(unix)]
3915+
fn test_merge_breaks_collator_ties_like_a_full_sort() {
3916+
// The collator gives U+200B and `_` no weight, so these pairs tie there. A
3917+
// full sort orders such a tie by reversed bytes, and `-m` must do the same
3918+
// rather than fall back to the file order or to forward bytes.
3919+
let locale = "en_US.UTF-8";
3920+
if !is_locale_available(locale) {
3921+
return;
3922+
}
3923+
for (first, second, expected) in [
3924+
("ab", "a\u{200b}b", "a\u{200b}b\nab\n"),
3925+
("01", "0_1", "0_1\n01\n"),
3926+
] {
3927+
let ts = TestScenario::new("sort");
3928+
ts.fixtures.write("a.txt", &format!("{first}\n"));
3929+
ts.fixtures.write("b.txt", &format!("{second}\n"));
3930+
3931+
ts.ucmd()
3932+
.env("LC_ALL", locale)
3933+
.args(&["a.txt", "b.txt"])
3934+
.succeeds()
3935+
.stdout_is(expected);
3936+
ts.ucmd()
3937+
.env("LC_ALL", locale)
3938+
.args(&["-m", "a.txt", "b.txt"])
3939+
.succeeds()
3940+
.stdout_is(expected);
3941+
}
3942+
}
3943+
38943944
#[test]
38953945
fn test_merge_locale_collation() {
38963946
// `sort -m` compares lines lazily with the collator instead of precomputed

0 commit comments

Comments
 (0)