@@ -255,13 +255,17 @@ impl<'a> Input<'a> {
255255 }
256256
257257 /// Converts input to title that appears in stats.
258- fn to_title ( & self ) -> Option < Cow < str > > {
258+ fn to_title ( & self ) -> Option < Cow < OsStr > > {
259259 match self {
260- Self :: Path ( path) => Some ( match path. to_str ( ) {
261- Some ( s) if !s. contains ( '\n' ) => Cow :: Borrowed ( s) ,
262- _ => Cow :: Owned ( escape_name_wrapper ( path. as_os_str ( ) ) ) ,
263- } ) ,
264- Self :: Stdin ( StdinKind :: Explicit ) => Some ( Cow :: Borrowed ( STDIN_REPR ) ) ,
260+ Self :: Path ( path) => {
261+ let path = path. as_os_str ( ) ;
262+ if path. to_string_lossy ( ) . contains ( '\n' ) {
263+ Some ( Cow :: Owned ( quoting_style:: escape_name ( path, QS_ESCAPE ) ) )
264+ } else {
265+ Some ( Cow :: Borrowed ( path) )
266+ }
267+ }
268+ Self :: Stdin ( StdinKind :: Explicit ) => Some ( Cow :: Borrowed ( OsStr :: new ( STDIN_REPR ) ) ) ,
265269 Self :: Stdin ( StdinKind :: Implicit ) => None ,
266270 }
267271 }
@@ -852,14 +856,17 @@ fn wc(inputs: &Inputs, settings: &Settings) -> UResult<()> {
852856 let maybe_title = input. to_title ( ) ;
853857 let maybe_title_str = maybe_title. as_deref ( ) ;
854858 if let Err ( err) = print_stats ( settings, & word_count, maybe_title_str, number_width) {
855- let title = maybe_title_str. unwrap_or ( "<stdin>" ) ;
856- show ! ( err. map_err_context( || format!( "failed to print result for {title}" ) ) ) ;
859+ let title = maybe_title_str. unwrap_or ( OsStr :: new ( "<stdin>" ) ) ;
860+ show ! ( err. map_err_context( || format!(
861+ "failed to print result for {}" ,
862+ title. to_string_lossy( )
863+ ) ) ) ;
857864 }
858865 }
859866 }
860867
861868 if settings. total_when . is_total_row_visible ( num_inputs) {
862- let title = are_stats_visible. then_some ( "total" ) ;
869+ let title = are_stats_visible. then_some ( OsStr :: new ( "total" ) ) ;
863870 if let Err ( err) = print_stats ( settings, & total_word_count, title, number_width) {
864871 show ! ( err. map_err_context( || "failed to print total" . into( ) ) ) ;
865872 }
@@ -873,7 +880,7 @@ fn wc(inputs: &Inputs, settings: &Settings) -> UResult<()> {
873880fn print_stats (
874881 settings : & Settings ,
875882 result : & WordCount ,
876- title : Option < & str > ,
883+ title : Option < & OsStr > ,
877884 number_width : usize ,
878885) -> io:: Result < ( ) > {
879886 let mut stdout = io:: stdout ( ) . lock ( ) ;
@@ -893,8 +900,8 @@ fn print_stats(
893900 }
894901
895902 if let Some ( title) = title {
896- writeln ! ( stdout, "{space}{title}" )
897- } else {
898- writeln ! ( stdout)
903+ write ! ( stdout, "{space}" ) ?;
904+ stdout. write_all ( & uucore:: os_str_as_bytes_lossy ( title) ) ?;
899905 }
906+ writeln ! ( stdout)
900907}
0 commit comments