Repository navigation
pr: fill the space between columns with tabs, add -i - #16555
ihsandeniz wants to merge 5 commits into
Conversation
POSIX says output in more than one column implies -e and -i. Like GNU pr, the padding between columns is now written as tabs followed by the spaces that are left, tabs in a padded column are expanded from where the column starts, and -i[CHAR[WIDTH]] / --output-tabs is accepted. Also like GNU pr: - the -o margin is written once per line, not before every column - -s without -w prints the columns without padding them - a -S separator longer than one byte takes its share of the line - in a column, the line number takes its width from the column, and when merging it precedes the first column only - a merged page is filled out with empty lines Fixes uutils#16510
Like GNU pr: - a TAB that starts a line is expanded as if the line number were not there - without padding, a line number followed by text is counted as if a TAB came after it, and printing across with a TAB between columns, each TAB of the text is counted as 8 columns - text of spaces only after a line number is not printed
| @@ -1519,14 +1586,129 @@ fn to_table_short_file( | |||
|
|
|||
| /// Write `n` space characters to `out` in fixed-size chunks, so the indent is | |||
There was a problem hiding this comment.
leftover doc comment from write_offset_spaces, please remove it
| self.flush_spaces()?; | ||
| self.buf.push(byte); | ||
| self.column = if byte == TAB as u8 { | ||
| self.column + 8 - self.column % 8 |
There was a problem hiding this comment.
x + 8 - x % 8 is now repeated 6 times (here, unpadded_column_end, number_field_width, get_line_for_printing). Could you please add a small next_tab_stop() helper?
There was a problem hiding this comment.
Done: next_tab_stop(column, width), also used for the -i width in flush_spaces.
| .as_ref() | ||
| .is_some_and(|i| i.across_mode); | ||
| let text = &content[number_len..]; | ||
| if across && options.col_sep_for_printing == TAB.to_string() { |
There was a problem hiding this comment.
this allocates a String for every cell, could you compare with "\t" instead?
There was a problem hiding this comment.
Done, here and in number_field_width.
| let text = &content[number_len..]; | ||
| if across && options.col_sep_for_printing == TAB.to_string() { | ||
| let tabs = memchr::memchr_iter(TAB as u8, text).count(); | ||
| cell_start + number_width + display_width(text) + 7 * tabs |
There was a problem hiding this comment.
how did you find the 7 * tabs rule? From GNU output only, i hope?
There was a problem hiding this comment.
Yes, from GNU's output only, I haven't looked at its source. Printing across with a TAB between columns, the spaces that follow in the next column turn into a TAB earlier than the tab stops would give, as if each TAB of the previous column took 8 columns. Without -a, GNU counts to the tab stop:
input (LC_ALL=C) |
pr -t -2 -J -a |
pr -t -2 -J |
|---|---|---|
bb\t / ab a ba |
bb\t\tab a\t ba |
bb\t\tab a ba |
a\tb / ab a ba |
a\tb\tab a\t ba |
a\tb\tab a\tba |
a\t\t / ab a ba |
a\t\t\tab a\tba |
a\t\t\tab a ba |
In the first one bb\t ends at 2 + 8 = 10 rather than 8, the separator makes it 11, ab a 15, and the two spaces reach 17, past the tab stop at 16. Without -a they go from 13 to 15. Counting to the tab stop instead differs from GNU on 107 of 3000 random -J -a inputs that this rule gets right.
I wrote this with the help of an AI assistant; the rules were worked out by running GNU's pr binary on hand-picked inputs and then compared on random ones, and I can share those scripts if useful.
| if no_trailer { | ||
| break; | ||
| } | ||
| // Like GNU pr, the rest of the page is filled with empty lines. |
There was a problem hiding this comment.
this is a separate fix from #16510, could it go in its own PR? Same for the -o margin and the -S width
There was a problem hiding this comment.
Sure: the empty lines of a merged page, the -o margin and the -S width are out of this PR now, and I'll send them as three small PRs. Until then the -o and -Sdivide fixtures and the blank lines in test_merge pin the current output here.
| /// TAB and a missing WIDTH is 8. | ||
| fn parse_tab_argument(s: &str, option: char) -> Result<(char, i32), PrError> { | ||
| let invalid = |arg: &str| { | ||
| let message = if option == 'i' { |
There was a problem hiding this comment.
could you pass the ftl key instead of the option char?
Also compare the separators with "\t" instead of allocating a String, and drop a doc comment left over from write_offset_spaces.
The fixtures of -o and -S pin the current output until then.
pr exits on the invalid argument before reading stdin, so writing to it could fail with a broken pipe now and then.
Merging this PR will not alter performance1 warning, 1 note
Comparing Footnotes
|
Fixes #16510.
POSIX says that output in more than one column implies
-eand-i. GNU prpads columns with tabs and expands input tabs from the start of each column.
uutils padded with spaces and then wrote the separator as a literal tab, so
every column moved on to the next tab stop:
pr -t -4put the columns at24/48/72 instead of 18/36/54.
LC_ALL=C)printf 'a\nb\n' | pr -t -2a\t\t\t\t ba+ 34 spaces +\tbpr -t -4ona,bbbbbbbbbbb,ccc,dprintf 'a\n\tb\n' | pr -t -2a\t\t\t\t \t ba+ 34 spaces +\t\tbprintf 'a\tb\nc\n' | pr -t -2 -s,a\tb,ca\tb+ 25 spaces +,cThe first commit:
-i[CHAR[WIDTH]]/--output-tabs[=CHAR[WIDTH]], parsed like-e,with GNU's error message
-ifor output in more than one column, and-efor padded columns-swithout-w, as was already done for-mbefore the first column only
The second commit matches a few more of GNU's column positions: a TAB that
starts a line is expanded as if the line number were not there; without
padding, a line number followed by text is counted as if a TAB came after it;
printing across with a TAB between columns, each TAB of the text counts as 8
columns; text of spaces only after a line number is not printed.
The fixtures and inline expectations that pinned the old output were
regenerated from GNU 9.12 output, as the TODOs in
test_columnsandtest_mergeasked. The exceptions are left to separate PRs: the-omargin(
column_spaces_across.log.expected), the width of a-Sstring longer thanone byte (
column_across_sep1.log.expected) and the empty lines that fill amerged page (
test_merge). Those still pin the current output. There are 9new tests.
On 5000 random inputs mixing
-2..-4,-w,-s,-S,-n,-o,-i,-e,-J,-mand-a(LC_ALL=C), mismatches with GNU went from 3916 to1191, with no input that matched before and differs now; the same in
C.UTF-8.#16518 changes the same function. The single-column
-Wpath is left as itis here, so whichever lands first, the other only needs a textual rebase.