diff --git a/src/uu/fold/src/fold.rs b/src/uu/fold/src/fold.rs index fac0ff28e..3bcb55e51 100644 --- a/src/uu/fold/src/fold.rs +++ b/src/uu/fold/src/fold.rs @@ -232,13 +232,9 @@ fn fold_file(mut file: BufReader, spaces: bool, width: usize) { last_space = if spaces { Some(char_count) } else { None }; } '\x08' => { - // FIXME: does not match GNU's handling of backspace if col_count > 0 { col_count -= 1; - char_count -= 1; - output.truncate(char_count); } - continue; } '\r' => { // FIXME: does not match GNU's handling of carriage return @@ -258,7 +254,7 @@ fn fold_file(mut file: BufReader, spaces: bool, width: usize) { char_count += 1; } - if col_count > 0 { + if char_count > 0 { print!("{}", output); output.truncate(0); } diff --git a/tests/by-util/test_fold.rs b/tests/by-util/test_fold.rs index cc92c8ff3..287b05157 100644 --- a/tests/by-util/test_fold.rs +++ b/tests/by-util/test_fold.rs @@ -248,6 +248,43 @@ fn test_fold_at_word_boundary_only_whitespace_preserve_final_newline() { .stdout_is(" \n \n"); } +#[test] +fn test_backspace_should_be_preserved() { + new_ucmd!().pipe_in("\x08").succeeds().stdout_is("\x08"); +} + +#[test] +fn test_backspaced_char_should_be_preserved() { + new_ucmd!().pipe_in("x\x08").succeeds().stdout_is("x\x08"); +} + +#[test] +fn test_backspace_should_decrease_column_count() { + new_ucmd!() + .arg("-w2") + .pipe_in("1\x08345") + .succeeds() + .stdout_is("1\x0834\n5"); +} + +#[test] +fn test_backspace_should_not_decrease_column_count_past_zero() { + new_ucmd!() + .arg("-w2") + .pipe_in("1\x08\x083456") + .succeeds() + .stdout_is("1\x08\x0834\n56"); +} + +#[test] +fn test_backspace_is_not_word_boundary() { + new_ucmd!() + .args(&["-w10", "-s"]) + .pipe_in("foobar\x086789abcdef") + .succeeds() + .stdout_is("foobar\x086789a\nbcdef"); +} + // // bytewise tests @@ -397,3 +434,39 @@ fn test_bytewise_fold_at_word_boundary_only_whitespace_preserve_final_newline() .succeeds() .stdout_is(" \n \n"); } + +#[test] +fn test_bytewise_backspace_should_be_preserved() { + new_ucmd!() + .arg("-b") + .pipe_in("\x08") + .succeeds() + .stdout_is("\x08"); +} + +#[test] +fn test_bytewise_backspaced_char_should_be_preserved() { + new_ucmd!() + .arg("-b") + .pipe_in("x\x08") + .succeeds() + .stdout_is("x\x08"); +} + +#[test] +fn test_bytewise_backspace_should_not_decrease_column_count() { + new_ucmd!() + .args(&["-w2", "-b"]) + .pipe_in("1\x08345") + .succeeds() + .stdout_is("1\x08\n34\n5"); +} + +#[test] +fn test_bytewise_backspace_is_not_word_boundary() { + new_ucmd!() + .args(&["-w10", "-s", "-b"]) + .pipe_in("foobar\x0889abcdef") + .succeeds() + .stdout_is("foobar\x0889a\nbcdef"); +}