Skip to content

Commit 502f3b1

Browse files
authored
sort:Align sort debug key annotations with GNU coreutils (uutils#9468)
* fix: ignore NUL bytes in sort debug output alignment calculations Replaced direct length checks with filtered counts excluding b'\0' in debug underline and indentation output to prevent misalignment from embedded NUL characters, which are often stripped during inspection. Added comprehensive tests for various sort modes and inputs, including NUL byte scenarios, to verify correct debug annotations. * feat: add locale-aware tests for sort debug key annotations Split the existing `test_debug_key_annotations` into two tests: one for basic functionality and another for locale-specific behavior to handle conditional execution based on environment variables. Extracted a new helper function `debug_key_annotation_output` to generate debug output, improving test modularity and reducing code duplication. This enhances test coverage for debug key annotations in different numeric locales. * refactor(test): optimize string building in debug_key_annotation_output for efficiency Rework the `number` helper function to use a mutable String buffer with `writeln!` macro instead of collecting intermediary vectors with `map` and `collect`. This reduces allocations and improves performance in test output generation, building the numbered output directly without extra string concatenations. * refactor(tests): improve formatting and readability in sort test helpers - Reformatted command-line arguments in test_debug_key_annotations_locale to fit on fewer lines - Wrapped run_sort calls in debug_key_annotation_output for better code structure - Minor reordering of output.push_str blocks for consistency and clarity * refactor(test): embed expected debug key annotation outputs as constants Replace fixture file reads with inline constants in test functions for debug key annotations and locale variants. This makes the tests more self-contained by removing dependencies on external fixture files. * feat(sort): extract count_non_null_bytes for debug alignment Add a helper function `count_non_null_bytes` to count bytes in a slice while ignoring embedded NULs. This improves code reusability and is used in debug underline output to ensure proper alignment by filtering NUL characters that may be present in selection strings. Replaces inline counting logic in two locations within the `Line` implementation.
1 parent 3edf14e commit 502f3b1

File tree

2 files changed

+418
-2
lines changed

2 files changed

+418
-2
lines changed

src/uu/sort/src/sort.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ impl SortMode {
221221
}
222222
}
223223

224+
/// Return the length of the byte slice while ignoring embedded NULs (used for debug underline alignment).
225+
fn count_non_null_bytes(bytes: &[u8]) -> usize {
226+
bytes.iter().filter(|&&c| c != b'\0').count()
227+
}
228+
224229
pub struct Output {
225230
file: Option<(OsString, File)>,
226231
}
@@ -670,14 +675,19 @@ impl<'a> Line<'a> {
670675
_ => {}
671676
}
672677

678+
// Don't let embedded NUL bytes influence column alignment in the
679+
// debug underline output, since they are often filtered out (e.g.
680+
// via `tr -d '\0'`) before inspection.
673681
let select = &line[..selection.start];
674-
write!(writer, "{}", " ".repeat(select.len()))?;
682+
let indent = count_non_null_bytes(select);
683+
write!(writer, "{}", " ".repeat(indent))?;
675684

676685
if selection.is_empty() {
677686
writeln!(writer, "{}", translate!("sort-error-no-match-for-key"))?;
678687
} else {
679688
let select = &line[selection];
680-
writeln!(writer, "{}", "_".repeat(select.len()))?;
689+
let underline_len = count_non_null_bytes(select);
690+
writeln!(writer, "{}", "_".repeat(underline_len))?;
681691
}
682692
}
683693

0 commit comments

Comments
 (0)