Skip to content

Commit cea3f22

Browse files
authored
Merge pull request #8787 from Misakait/fix/ptx-tex-format-bug
fix(ptx): Correct various output formatting bugs
2 parents 9064619 + 48b5439 commit cea3f22

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

src/uu/ptx/src/ptx.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// spell-checker:ignore (ToDOs) corasick memchr Roff trunc oset iset CHARCLASS
77

88
use std::cmp;
9+
use std::cmp::PartialEq;
910
use std::collections::{BTreeSet, HashMap, HashSet};
1011
use std::ffi::{OsStr, OsString};
1112
use std::fmt::Write as FmtWrite;
@@ -22,7 +23,7 @@ use uucore::error::{FromIo, UError, UResult, UUsageError};
2223
use uucore::format_usage;
2324
use uucore::translate;
2425

25-
#[derive(Debug)]
26+
#[derive(Debug, PartialEq)]
2627
enum OutFormat {
2728
Dumb,
2829
Roff,
@@ -483,7 +484,18 @@ fn get_output_chunks(
483484
let tail_end = trim_broken_word_right(all_after, tail_beg, tail_end);
484485

485486
// trim away whitespace again.
486-
let (tail_beg, tail_end) = trim_idx(all_after, tail_beg, tail_end);
487+
let (tail_beg, mut tail_end) = trim_idx(all_after, tail_beg, tail_end);
488+
// Fix: Manually trim trailing char (like "a") that are preceded by a space.
489+
// This handles cases like "is a" which are not correctly trimmed by the
490+
// preceding functions.
491+
if tail_end >= 2
492+
&& (tail_end - 2) > tail_beg
493+
&& all_after[tail_end - 2].is_whitespace()
494+
&& !all_after[tail_end - 1].is_whitespace()
495+
{
496+
tail_end -= 1;
497+
(_, tail_end) = trim_idx(all_after, tail_beg, tail_end);
498+
}
487499

488500
// and get the string
489501
let tail_str: String = all_after[tail_beg..tail_end].iter().collect();
@@ -511,19 +523,21 @@ fn get_output_chunks(
511523
// and get the string.
512524
let head_str: String = all_before[head_beg..head_end].iter().collect();
513525
head.push_str(&head_str);
526+
//The TeX mode does not output truncation characters.
527+
if config.format != OutFormat::Tex {
528+
// put right context truncation string if needed
529+
if after_end != all_after.len() && tail_beg == tail_end {
530+
after.push_str(&config.trunc_str);
531+
} else if after_end != all_after.len() && tail_end != all_after.len() {
532+
tail.push_str(&config.trunc_str);
533+
}
514534

515-
// put right context truncation string if needed
516-
if after_end != all_after.len() && tail_beg == tail_end {
517-
after.push_str(&config.trunc_str);
518-
} else if after_end != all_after.len() && tail_end != all_after.len() {
519-
tail.push_str(&config.trunc_str);
520-
}
521-
522-
// put left context truncation string if needed
523-
if before_beg != 0 && head_beg == head_end {
524-
before = format!("{}{before}", config.trunc_str);
525-
} else if before_beg != 0 && head_beg != 0 {
526-
head = format!("{}{head}", config.trunc_str);
535+
// put left context truncation string if needed
536+
if before_beg != 0 && head_beg == head_end {
537+
before = format!("{}{before}", config.trunc_str);
538+
} else if before_beg != 0 && head_beg != 0 {
539+
head = format!("{}{head}", config.trunc_str);
540+
}
527541
}
528542

529543
(tail, before, after, head)

tests/by-util/test_ptx.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@ fn test_invalid_arg() {
1111
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
1212
}
1313

14+
#[test]
15+
fn test_tex_format_no_truncation_markers() {
16+
let input = "Hello world Rust is a fun language";
17+
new_ucmd!()
18+
.args(&["-G", "-w", "30", "--format=tex"])
19+
.pipe_in(input)
20+
.succeeds()
21+
.stdout_only_fixture("test_tex_format_no_truncation_markers.expected");
22+
}
23+
#[test]
24+
fn gnu_ext_disabled_chunk_no_over_reading() {
25+
let input = "Hello World Rust is a fun language";
26+
new_ucmd!()
27+
.args(&["-G", "-w", "30"])
28+
.pipe_in(input)
29+
.succeeds()
30+
.stdout_only_fixture("gnu_ext_disabled_chunk_no_over_reading.expected");
31+
}
32+
1433
#[test]
1534
fn test_truncation_no_extra_space_in_after() {
1635
new_ucmd!()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.xx "Rust is/" "" "Hello World" ""
2+
.xx "" "Hello World" "Rust is a/" ""
3+
.xx "is/" "Hello" "World Rust" ""
4+
.xx "" "/Rust is" "a fun/" ""
5+
.xx "" "/Rust is a" "fun language" ""
6+
.xx "" "/World Rust" "is a fun/" ""
7+
.xx "" "/is a fun" "language" ""
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
\xx {Rust is}{}{Hello}{ world}{}
2+
\xx {}{Hello world}{Rust}{ is a}{}
3+
\xx {}{Rust is}{a}{ fun}{}
4+
\xx {}{Rust is a}{fun}{ language}{}
5+
\xx {}{world Rust}{is}{ a fun}{}
6+
\xx {}{is a fun}{language}{}{}
7+
\xx {is}{Hello}{world}{ Rust}{}

0 commit comments

Comments
 (0)