Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/uu/sort/src/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2793,11 +2793,8 @@ pub fn merge_compare<'a>(
#[cfg(feature = "i18n-collator")]
#[inline(never)]
fn compare_lines_with_collator(a: &Line<'_>, b: &Line<'_>, settings: &GlobalSettings) -> Ordering {
let mut cmp = locale_cmp(a.line, b.line);
if cmp == Ordering::Equal {
// Equal keys for inputs like `01` and `0_1`; fall back to (reversed) byte order.
cmp = b.line.cmp(a.line);
}
// `locale_cmp` already breaks collator ties by reversed bytes.
let cmp = locale_cmp(a.line, b.line);
if settings.reverse { cmp.reverse() } else { cmp }
}

Expand Down
10 changes: 7 additions & 3 deletions src/uucore/src/lib/features/i18n/collator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,18 @@ pub fn compute_sort_key_utf8(input: &[u8], buf: &mut Vec<u8>) {
}

/// Compare both strings with regard to the current locale.
///
/// Collator ties are broken by reversed bytes, as `sort` does for whole lines,
/// so `Equal` means identical bytes.
pub fn locale_cmp(left: &[u8], right: &[u8]) -> Ordering {
// If the detected locale is 'C', just do byte-wise comparison
if get_collating_locale().0 == DEFAULT_LOCALE {
left.cmp(right)
} else {
// Fall back to byte comparison if collator is not available
COLLATOR
.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.

|c| c.compare_utf8(left, right).then_with(|| right.cmp(left)),
)
}
}
38 changes: 38 additions & 0 deletions tests/by-util/test_comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,44 @@ fn test_locale_collation() {
.stdout_only("a1\n");
}

#[uutests::wasi_ignore(NoLocaleData)]
#[test]
#[cfg(unix)]
fn test_locale_collation_accepts_the_tie_order_of_sort() {
// The collator gives U+200B no weight, so `ab` and `a\u{200b}b` tie there.
// `sort` puts the longer one first, and `comm` must read that as sorted.
let ts = TestScenario::new(util_name!());
ts.fixtures.write("one", "a\u{200b}b\nab\n");
ts.fixtures.write("two", "ab\n");

ts.ucmd()
.env("LC_ALL", "en_US.UTF-8")
.args(&["--check-order", "one", "two"])
.succeeds()
.stdout_only("a\u{200b}b\n\t\tab\n");
}

#[uutests::wasi_ignore(NoLocaleData)]
#[test]
#[cfg(unix)]
fn test_locale_collation_does_not_match_punctuation_variants() {
// The collator gives no weight to `/` and `.`, but these lines differ.
let locale = "en_US.UTF-8";
if !is_locale_available(locale) {
return;
}
let scene = TestScenario::new(util_name!());
scene.fixtures.write("allowed", "/srv/uploads\n");
scene.fixtures.write("requested", "/srv/uploads/../..\n");

scene
.ucmd()
.env("LC_ALL", locale)
.args(&["-12", "allowed", "requested"])
.succeeds()
.no_stdout();
}

#[test]
#[cfg(unix)]
fn test_c_locale_still_orders_by_bytes() {
Expand Down
22 changes: 21 additions & 1 deletion tests/by-util/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// spell-checker:ignore αbcdef ; (people) kkos
// spell-checker:ignore aabcccd aabcd aabd abbb abbbd abbcabc abbcac abbcbbbd abbcbd
// spell-checker:ignore abbccd abcabc abcac acabc andand bigcmp bignum emptysub
// spell-checker:ignore orempty oror bcdef fedcb GHSA xjfw pqfw
// spell-checker:ignore orempty oror bcdef fedcb GHSA xjfw pqfw zabc

use rstest::rstest;
use uutests::new_ucmd;
Expand Down Expand Up @@ -1701,6 +1701,26 @@ mod locale_aware {
.stdout_only(output);
}
}

#[uutests::wasi_ignore(NoLocaleData)]
#[test]
fn test_expr_equality_is_exact_in_locale() {
// The collator gives no weight to punctuation, but `=` must still
// tell these strings apart.
for (left, right) in [("/srv/uploads/../..", "/srv/uploads"), ("r.o.o.t", "root")] {
new_ucmd!()
.args(&[left, "=", right])
.env("LC_ALL", "en_US.UTF-8")
.run()
.code_is(1)
.stdout_only("0\n");
new_ucmd!()
.args(&[left, "!=", right])
.env("LC_ALL", "en_US.UTF-8")
.succeeds()
.stdout_only("1\n");
}
}
}

/// Tests for multibyte character arithmetic in expr
Expand Down
20 changes: 19 additions & 1 deletion tests/by-util/test_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore (words) autoformat nocheck FILENUM
// spell-checker:ignore (words) autoformat nocheck FILENUM svcabc

use rstest::rstest;
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
Expand Down Expand Up @@ -922,6 +922,24 @@ fn test_locale_collation() {
.stdout_contains("ab:d 1 x");
}

#[uutests::wasi_ignore(NoLocaleData)]
#[cfg(unix)]
#[test]
fn test_locale_collation_does_not_pair_punctuation_variants() {
// The collator gives no weight to `-`, but the keys differ.
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;

at.write("accounts", "admin:1\n");
at.write("requests", "ad-min:2\n");

ts.ucmd()
.env("LC_ALL", "en_US.UTF-8")
.args(&["-t:", "accounts", "requests"])
.succeeds()
.no_stdout();
}

#[uutests::wasi_ignore(Usize32BitOverflow)]
#[test]
fn test_incompatible_fields_reports_exact_field_number() {
Expand Down
52 changes: 51 additions & 1 deletion tests/by-util/test_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore (words) ints (linux) NOFILE dfgi abmon avril naab
// spell-checker:ignore (words) ints (linux) NOFILE dfgi abmon avril naab Xena

#![allow(clippy::cast_possible_wrap)]

Expand Down Expand Up @@ -4136,6 +4136,56 @@ fn test_consistent_sorting_with_i18n_collate() {
.stdout_is(expected_output);
}

#[test]
#[cfg(unix)]
fn test_unique_key_keeps_keys_the_collator_cannot_tell_apart() {
// The collator gives U+200B no weight, so these keys compare `Equal`
// there, but they are different strings and `-u` must keep both.
let locale = "en_US.UTF-8";
if !is_locale_available(locale) {
return;
}
let out = new_ucmd!()
.env("LC_ALL", locale)
.args(&["-u", "-k1,1"])
.pipe_in("ab x\na\u{200b}b y\n")
.succeeds()
.stdout_move_str();
// GNU orders the tie like a full sort does: by reversed bytes.
assert_eq!(out, "a\u{200b}b y\nab x\n");
}

#[test]
#[cfg(unix)]
fn test_merge_breaks_collator_ties_like_a_full_sort() {
// The collator gives U+200B and `_` no weight, so these pairs tie there. A
// full sort orders such a tie by reversed bytes, and `-m` must do the same
// rather than fall back to the file order or to forward bytes.
let locale = "en_US.UTF-8";
if !is_locale_available(locale) {
return;
}
for (first, second, expected) in [
("ab", "a\u{200b}b", "a\u{200b}b\nab\n"),
("01", "0_1", "0_1\n01\n"),
] {
let ts = TestScenario::new("sort");
ts.fixtures.write("a.txt", &format!("{first}\n"));
ts.fixtures.write("b.txt", &format!("{second}\n"));

ts.ucmd()
.env("LC_ALL", locale)
.args(&["a.txt", "b.txt"])
.succeeds()
.stdout_is(expected);
ts.ucmd()
.env("LC_ALL", locale)
.args(&["-m", "a.txt", "b.txt"])
.succeeds()
.stdout_is(expected);
}
}

#[test]
fn test_merge_locale_collation() {
// `sort -m` compares lines lazily with the collator instead of precomputed
Expand Down
Loading