@@ -7,7 +7,7 @@ use regex::Regex;
77use rubyfmt:: init_logger;
88use similar:: TextDiff ;
99use std:: ffi:: OsStr ;
10- use std:: fs:: { File , OpenOptions , read_to_string } ;
10+ use std:: fs:: { File , OpenOptions , read } ;
1111use std:: io:: { self , BufRead , BufReader , IsTerminal , Read , Write } ;
1212use std:: path:: Path ;
1313use std:: process:: { Command , exit} ;
@@ -155,24 +155,17 @@ fn rubyfmt_string(
155155 header_opt_out,
156156 ..
157157 } : & CommandlineOpts ,
158- buffer : & str ,
158+ buffer : & [ u8 ] ,
159159) -> Result < Option < Vec < u8 > > , rubyfmt:: RichFormatError > {
160160 if header_opt_in || header_opt_out {
161161 // Only look at the first 500 bytes for the magic header.
162- // This is for performance
163- let mut slice = buffer;
164- let mut slice_size = 500 ;
165- let blength = buffer. len ( ) ;
166-
167- if blength > slice_size {
168- while !buffer. is_char_boundary ( slice_size) && slice_size < blength {
169- slice_size += 1 ;
170- }
171- slice = & buffer[ ..slice_size]
172- }
162+ // This is for performance. Use lossy UTF-8 conversion since the
163+ // magic comment is always ASCII.
164+ let slice_size = buffer. len ( ) . min ( 500 ) ;
165+ let slice = String :: from_utf8_lossy ( & buffer[ ..slice_size] ) ;
173166
174167 let matched = MAGIC_COMMENT_REGEX
175- . captures ( slice)
168+ . captures ( & slice)
176169 . and_then ( |c| c. name ( "enabled" ) )
177170 . map ( |s| s. as_str ( ) ) ;
178171
@@ -260,10 +253,10 @@ fn get_command_line_options() -> CommandlineOpts {
260253 }
261254}
262255
263- fn iterate_input_files ( opts : & CommandlineOpts , f : & dyn Fn ( ( & Path , & String ) ) ) {
256+ fn iterate_input_files ( opts : & CommandlineOpts , f : InputFunc ) {
264257 if opts. include_paths . is_empty ( ) {
265258 // If not include paths are present, assume user is passing via STDIN
266- let mut buffer = String :: new ( ) ;
259+ let mut buffer = Vec :: new ( ) ;
267260
268261 if io:: stdin ( ) . is_terminal ( ) {
269262 // Call executable with `--help` args to print help statement
@@ -274,15 +267,15 @@ fn iterate_input_files(opts: &CommandlineOpts, f: &dyn Fn((&Path, &String))) {
274267 }
275268
276269 io:: stdin ( )
277- . read_to_string ( & mut buffer)
270+ . read_to_end ( & mut buffer)
278271 . expect ( "reading from stdin to not fail" ) ;
279272
280273 let path = if let Some ( stdin_filepath) = & opts. stdin_filepath {
281274 let path = Path :: new ( stdin_filepath) ;
282275 if is_path_ignored ( path, opts. include_gitignored ) {
283276 // Print unchanged output for ignored files unless we're in check mode
284277 if !opts. check {
285- puts_stdout ( buffer. as_bytes ( ) ) ;
278+ puts_stdout ( & buffer) ;
286279 }
287280 return ;
288281 }
@@ -308,9 +301,7 @@ fn iterate_input_files(opts: &CommandlineOpts, f: &dyn Fn((&Path, &String))) {
308301 match result {
309302 Ok ( pp) => {
310303 let file_path = pp. path ( ) ;
311- let buffer_res = read_to_string ( file_path) ;
312-
313- match buffer_res {
304+ match read ( file_path) {
314305 Ok ( buffer) => f ( ( file_path, & buffer) ) ,
315306 Err ( e) => handle_execution_error (
316307 opts,
@@ -332,9 +323,7 @@ fn iterate_input_files(opts: &CommandlineOpts, f: &dyn Fn((&Path, &String))) {
332323 if file_path. is_file ( )
333324 && file_path. extension ( ) . and_then ( OsStr :: to_str) == Some ( "rb" )
334325 {
335- let buffer_res = read_to_string ( file_path) ;
336-
337- match buffer_res {
326+ match read ( file_path) {
338327 Ok ( buffer) => f ( ( file_path, & buffer) ) ,
339328 Err ( e) => handle_execution_error (
340329 opts,
@@ -350,7 +339,8 @@ fn iterate_input_files(opts: &CommandlineOpts, f: &dyn Fn((&Path, &String))) {
350339 }
351340}
352341
353- type FormattingFunc < ' a > = & ' a dyn Fn ( ( & Path , & String , Option < Vec < u8 > > ) ) ;
342+ type InputFunc < ' a > = & ' a dyn Fn ( ( & Path , & [ u8 ] ) ) ;
343+ type FormattingFunc < ' a > = & ' a dyn Fn ( ( & Path , & [ u8 ] , Option < Vec < u8 > > ) ) ;
354344
355345fn iterate_formatted ( opts : & CommandlineOpts , f : FormattingFunc ) {
356346 iterate_input_files (
@@ -392,7 +382,7 @@ fn main() {
392382 & |( file_path, before) | match rubyfmt_string ( & opts, before) {
393383 Ok ( None ) => { }
394384 Ok ( Some ( fmtted) ) => {
395- let diff = TextDiff :: from_lines ( before. as_bytes ( ) , & fmtted) ;
385+ let diff = TextDiff :: from_lines ( before, & fmtted) ;
396386 let path_string = file_path. to_str ( ) . unwrap ( ) ;
397387 text_diffs. lock ( ) . unwrap ( ) . push ( format ! (
398388 "{}" ,
@@ -434,7 +424,7 @@ fn main() {
434424 iterate_formatted ( & opts, & |( file_path, before, after) | match after {
435425 None => { }
436426 Some ( fmtted) => {
437- if fmtted. ne ( before. as_bytes ( ) ) {
427+ if fmtted. ne ( before) {
438428 let file_write = OpenOptions :: new ( )
439429 . write ( true )
440430 . truncate ( true )
@@ -455,7 +445,7 @@ fn main() {
455445
456446 _ => iterate_formatted ( & opts, & |( _, before, after) | match after {
457447 Some ( fmtted) => puts_stdout ( & fmtted) ,
458- None => puts_stdout ( before. as_bytes ( ) ) ,
448+ None => puts_stdout ( before) ,
459449 } ) ,
460450 }
461451}
0 commit comments