pr: ignore empty line after form feed char (#10331)

Fix a bug in `pr` where a newline character (`\n`) immediately following
a form feed character (`\f`) was incorrectly rendered as an extra blank
line in the output. After this commit, the newline is correctly ignored.
This commit is contained in:
jfinkels
2026-01-19 09:14:50 +01:00
committed by GitHub
parent c720245457
commit 597361324c
2 changed files with 27 additions and 3 deletions
+8 -3
View File
@@ -856,9 +856,14 @@ fn get_pages(
} else {
// Add everything up to (but not including) the newline
// character as one line of the page.
let file_line = FileLine::from_buf(file_id, page_num, line_num, &buf[prev..i])?;
page.push(file_line);
line_num += 1;
if i > 0 && i == prev && buf[i - 1] == FF {
// If the file has the pattern `\f\n`, don't treat the
// `\n` as its own line; instead ignore the empty line.
} else {
let file_line = FileLine::from_buf(file_id, page_num, line_num, &buf[prev..i])?;
page.push(file_line);
line_num += 1;
}
// Remember where the last line ended.
prev = i + 1;
+19
View File
@@ -631,3 +631,22 @@ fn test_form_feed_newlines() {
.succeeds()
.stdout_matches(&regex);
}
#[test]
fn test_form_feed_followed_by_new_line() {
// Here we define the expected output.
let whitespace = " ".repeat(50);
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d";
let blank_lines_61 = "\n".repeat(61);
let blank_lines_60 = "\n".repeat(60);
let page1 = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\n{blank_lines_61}");
let page2 = format!("\n\n{datetime_pattern}{whitespace}Page 2\n\n\nabc\n{blank_lines_60}");
let pattern = format!("{page1}{page2}");
let regex = Regex::new(&pattern).unwrap();
// Command line: `printf "\f\nabc" | pr`.
new_ucmd!()
.pipe_in("\x0c\nabc")
.succeeds()
.stdout_matches(&regex);
}