Skip to content

Commit bfe6f0f

Browse files
authored
Merge pull request #5979 from BenWiederhake/dev-shuf-null-input
shuf: Treat empty file as zero elements instead of one emptystring
2 parents de74f70 + e54c9be commit bfe6f0f

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/uu/shuf/src/shuf.rs

+7
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ fn read_input_file(filename: &str) -> UResult<Vec<u8>> {
195195
}
196196

197197
fn find_seps(data: &mut Vec<&[u8]>, sep: u8) {
198+
// Special case: If data is empty (and does not even contain a single 'sep'
199+
// to indicate the presence of the empty element), then behave as if the input contained no elements at all.
200+
if data.len() == 1 && data[0].is_empty() {
201+
data.clear();
202+
return;
203+
}
204+
198205
// need to use for loop so we don't borrow the vector as we modify it in place
199206
// basic idea:
200207
// * We don't care about the order of the result. This lets us slice the slices

tests/by-util/test_shuf.rs

+7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ fn test_zero_termination() {
4848
assert_eq!(result_seq, input_seq, "Output is not a permutation");
4949
}
5050

51+
#[test]
52+
fn test_empty_input() {
53+
let result = new_ucmd!().pipe_in(vec![]).succeeds();
54+
result.no_stderr();
55+
result.no_stdout();
56+
}
57+
5158
#[test]
5259
fn test_echo() {
5360
let input_seq = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

0 commit comments

Comments
 (0)