Skip to content

Commit 3f16c72

Browse files
mattsu2020sylvestre
authored andcommitted
fix(sort): fix case-insensitive sorting to order punctuation after letters
Changed ascii_case_insensitive_cmp to fold characters to uppercase instead of lowercase, ensuring that in ASCII case-insensitive sorting, letters are ordered before punctuation. Added tests to verify the correct behavior.
1 parent 5e32752 commit 3f16c72

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/uu/sort/src/sort.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,13 +2622,13 @@ fn compare_by<'a>(
26222622
/// untouched and we avoid locale-sensitive routines such as `strcasecmp`.
26232623
fn ascii_case_insensitive_cmp(a: &[u8], b: &[u8]) -> Ordering {
26242624
#[inline]
2625-
fn lower(byte: u8) -> u8 {
2626-
byte.to_ascii_lowercase()
2625+
fn fold(byte: u8) -> u8 {
2626+
byte.to_ascii_uppercase()
26272627
}
26282628

26292629
for (lhs, rhs) in a.iter().copied().zip(b.iter().copied()) {
2630-
let l = lower(lhs);
2631-
let r = lower(rhs);
2630+
let l = fold(lhs);
2631+
let r = fold(rhs);
26322632
if l != r {
26332633
return l.cmp(&r);
26342634
}

tests/by-util/test_sort.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,24 @@ fn test_version_sort_stable() {
208208
.stdout_is("0.1\n0.02\n0.2\n0.002\n0.3\n");
209209
}
210210

211+
#[test]
212+
fn test_ignore_case_orders_punctuation_after_letters() {
213+
new_ucmd!()
214+
.arg("-f")
215+
.pipe_in("A\na\n_\n")
216+
.succeeds()
217+
.stdout_is("A\na\n_\n");
218+
}
219+
220+
#[test]
221+
fn test_ignore_case_unique_orders_punctuation_after_letters() {
222+
new_ucmd!()
223+
.arg("-fu")
224+
.pipe_in("a\n_\n")
225+
.succeeds()
226+
.stdout_is("a\n_\n");
227+
}
228+
211229
#[test]
212230
fn test_human_numeric_whitespace() {
213231
test_helper(

0 commit comments

Comments
 (0)