66// spell-checker:ignore (ToDOs) corasick memchr Roff trunc oset iset CHARCLASS
77
88use std:: cmp;
9+ use std:: cmp:: PartialEq ;
910use std:: collections:: { BTreeSet , HashMap , HashSet } ;
1011use std:: ffi:: { OsStr , OsString } ;
1112use std:: fmt:: Write as FmtWrite ;
@@ -22,7 +23,7 @@ use uucore::error::{FromIo, UError, UResult, UUsageError};
2223use uucore:: format_usage;
2324use uucore:: translate;
2425
25- #[ derive( Debug ) ]
26+ #[ derive( Debug , PartialEq ) ]
2627enum 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)
0 commit comments