Skip to content

Commit 425b232

Browse files
jfinkelssylvestre
andauthored
pr: ignore empty line after newline char (#10332)
Fix a bug in `pr` where a form feed character (`\f`) immediately following a newline character (`\n`) was incorrectly rendered as an extra blank line in the output when running with the `-f` option. After this commit, the empty line is correctly ignored. Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
1 parent 8a7c3fb commit 425b232

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/uu/pr/src/pr.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,13 @@ fn get_pages(
836836
if buf[i] == FF {
837837
// Treat everything up to (but not including) the form feed
838838
// character as the last line of the page.
839-
let file_line = FileLine::from_buf(file_id, page_num, line_num, &buf[prev..i])?;
840-
page.push(file_line);
839+
if i > 0 && i == prev && buf[i - 1] == NL {
840+
// If the file has the pattern `\n\f`, don't treat the
841+
// `\f` as its own line; instead ignore the empty line.
842+
} else {
843+
let file_line = FileLine::from_buf(file_id, page_num, line_num, &buf[prev..i])?;
844+
page.push(file_line);
845+
}
841846

842847
// Remember where the last line ended.
843848
prev = i + 1;

tests/by-util/test_pr.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,22 @@ fn test_form_feed_newlines() {
632632
.stdout_matches(&regex);
633633
}
634634

635+
#[test]
636+
fn test_new_line_followed_by_form_feed() {
637+
// Here we define the expected output.
638+
let whitespace = " ".repeat(50);
639+
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d";
640+
let pattern = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\nabc\n\x0c");
641+
let regex = Regex::new(&pattern).unwrap();
642+
643+
// Command line: `printf "abc\n\f" | pr -f`.
644+
new_ucmd!()
645+
.arg("-f")
646+
.pipe_in("abc\n\x0c")
647+
.succeeds()
648+
.stdout_matches(&regex);
649+
}
650+
635651
#[test]
636652
fn test_form_feed_followed_by_new_line() {
637653
// Here we define the expected output.

0 commit comments

Comments
 (0)