diff --git a/src/uu/fold/src/fold.rs b/src/uu/fold/src/fold.rs index c85ed7bed1c..4a75bd2d5dc 100644 --- a/src/uu/fold/src/fold.rs +++ b/src/uu/fold/src/fold.rs @@ -569,6 +569,12 @@ fn process_utf8_chars(line: &str, ctx: &mut FoldContext<'_, W>) -> URe let mut iter = line.char_indices().peekable(); while let Some((byte_idx, ch)) = iter.next() { + if ch == '\n' { + *ctx.last_space = None; + emit_output(ctx)?; + continue; + } + // Include combining characters with the base character when we are // measuring by display columns. In character-counting mode every // scalar value must advance the counter to match `chars().count()` @@ -586,12 +592,6 @@ fn process_utf8_chars(line: &str, ctx: &mut FoldContext<'_, W>) -> URe let next_idx = iter.peek().map_or(line_bytes.len(), |(idx, _)| *idx); - if ch == '\n' { - *ctx.last_space = None; - emit_output(ctx)?; - continue; - } - if *ctx.col_count >= ctx.width { emit_output(ctx)?; } diff --git a/tests/by-util/test_fold.rs b/tests/by-util/test_fold.rs index fa0122c6f07..fc70e492172 100644 --- a/tests/by-util/test_fold.rs +++ b/tests/by-util/test_fold.rs @@ -253,6 +253,18 @@ fn test_zero_width_spaces_in_column_mode() { .stdout_is(&input); } +#[test] +fn test_zero_width_after_newline_in_column_mode() { + let input = "abc\n\u{200B}From x\n\u{200D}\u{0301}\nb\n"; + for args in [&[][..], &["-s"]] { + new_ucmd!() + .args(args) + .pipe_in(input) + .succeeds() + .stdout_is(input); + } +} + #[test] fn test_zero_width_spaces_in_character_mode() { let len = io_buf_size_times_two();