44// file that was distributed with this source code.
55
66use crate :: errors:: NumfmtError ;
7- use crate :: format:: { format_and_print_delimited , format_and_print_whitespace } ;
7+ use crate :: format:: { write_formatted_with_delimiter , write_formatted_with_whitespace } ;
88use crate :: options:: {
99 DEBUG , DELIMITER , FIELD , FIELD_DEFAULT , FORMAT , FROM , FROM_DEFAULT , FROM_UNIT ,
1010 FROM_UNIT_DEFAULT , FormatOptions , HEADER , HEADER_DEFAULT , INVALID , InvalidModes , NUMBER ,
@@ -14,7 +14,7 @@ use crate::options::{
1414use crate :: units:: { Result , Unit } ;
1515use clap:: { Arg , ArgAction , ArgMatches , Command , builder:: ValueParser , parser:: ValueSource } ;
1616use std:: ffi:: OsString ;
17- use std:: io:: { BufRead , Error , Write , stderr} ;
17+ use std:: io:: { BufRead , Error , Write as _ , stderr} ;
1818use std:: result:: Result as StdResult ;
1919use std:: str:: FromStr ;
2020
@@ -32,8 +32,9 @@ pub mod options;
3232mod units;
3333
3434fn handle_args < ' a > ( args : impl Iterator < Item = & ' a [ u8 ] > , options : & NumfmtOptions ) -> UResult < ( ) > {
35+ let mut stdout = std:: io:: stdout ( ) . lock ( ) ;
3536 for l in args {
36- format_and_handle_validation ( l, options) ?;
37+ write_line ( & mut stdout , l, options) ?;
3738 }
3839 Ok ( ( ) )
3940}
@@ -51,33 +52,32 @@ fn handle_buffer_iterator(
5152 options : & NumfmtOptions ,
5253 terminator : u8 ,
5354) -> UResult < ( ) > {
55+ let mut stdout = std:: io:: stdout ( ) . lock ( ) ;
5456 for ( idx, line_result) in iter. enumerate ( ) {
5557 match line_result {
5658 Ok ( line) if idx < options. header => {
57- std :: io :: stdout ( ) . write_all ( & line) ?;
58- std :: io :: stdout ( ) . write_all ( & [ terminator] ) ?;
59+ stdout. write_all ( & line) ?;
60+ stdout. write_all ( & [ terminator] ) ?;
5961 Ok ( ( ) )
6062 }
61- Ok ( line) => format_and_handle_validation ( & line, options) ,
63+ Ok ( line) => write_line ( & mut stdout , & line, options) ,
6264 Err ( err) => return Err ( Box :: new ( NumfmtError :: IoError ( err. to_string ( ) ) ) ) ,
6365 } ?;
6466 }
6567 Ok ( ( ) )
6668}
6769
68- fn format_and_handle_validation ( input_line : & [ u8 ] , options : & NumfmtOptions ) -> UResult < ( ) > {
69- let eol = if options. zero_terminated {
70- b'\0'
71- } else {
72- b'\n'
73- } ;
74-
70+ fn write_line < W : std:: io:: Write > (
71+ writer : & mut W ,
72+ input_line : & [ u8 ] ,
73+ options : & NumfmtOptions ,
74+ ) -> UResult < ( ) > {
7575 let handled_line = if options. delimiter . is_some ( ) {
76- format_and_print_delimited ( input_line, options)
76+ write_formatted_with_delimiter ( writer , input_line, options)
7777 } else {
7878 // Whitespace mode requires valid UTF-8
7979 match std:: str:: from_utf8 ( input_line) {
80- Ok ( s) => format_and_print_whitespace ( s, options) ,
80+ Ok ( s) => write_formatted_with_whitespace ( writer , s, options) ,
8181 Err ( _) => Err ( translate ! ( "numfmt-error-invalid-input" ) ) ,
8282 }
8383 } ;
@@ -95,8 +95,14 @@ fn format_and_handle_validation(input_line: &[u8], options: &NumfmtOptions) -> U
9595 }
9696 InvalidModes :: Ignore => { }
9797 }
98- std:: io:: stdout ( ) . write_all ( input_line) ?;
99- std:: io:: stdout ( ) . write_all ( & [ eol] ) ?;
98+ writer. write_all ( input_line) ?;
99+
100+ let eol = if options. zero_terminated {
101+ b"\0 "
102+ } else {
103+ b"\n "
104+ } ;
105+ writer. write_all ( eol) ?;
100106 }
101107
102108 Ok ( ( ) )
0 commit comments