@@ -17,7 +17,9 @@ extern crate term;
17
17
use std:: collections:: HashMap ;
18
18
use std:: fs;
19
19
use std:: io:: { self , BufRead , BufReader , Read } ;
20
+ use std:: iter:: Peekable ;
20
21
use std:: path:: { Path , PathBuf } ;
22
+ use std:: str:: Bytes ;
21
23
22
24
use rustfmt:: * ;
23
25
use rustfmt:: filemap:: { write_system_newlines, FileMap } ;
@@ -369,8 +371,8 @@ fn handle_result(
369
371
let read_error = format ! ( "Failed reading target {:?}" , & target) ;
370
372
f. read_to_string ( & mut text) . expect ( & read_error) ;
371
373
372
- // Compare line by line, for Windows machines using CRLF newline .
373
- if fmt_text . replace ( " \r \n " , " \n " ) != text . replace ( " \r \n " , " \n " ) {
374
+ // Ignore LF and CRLF difference for Windows.
375
+ if ! string_eq_ignore_newline_repr ( & fmt_text , & text ) {
374
376
let diff = make_diff ( & text, & fmt_text, DIFF_CONTEXT_SIZE ) ;
375
377
assert ! (
376
378
!diff. is_empty( ) ,
@@ -431,3 +433,33 @@ fn rustfmt_diff_no_diff_test() {
431
433
let diff = make_diff ( "a\n b\n c\n d" , "a\n b\n c\n d" , 3 ) ;
432
434
assert_eq ! ( diff, vec![ ] ) ;
433
435
}
436
+
437
+ // Compare strings without distinguishing between CRLF and LF
438
+ fn string_eq_ignore_newline_repr ( left : & str , right : & str ) -> bool {
439
+ let left = BytesIgnoreNewlineRepr ( left. bytes ( ) . peekable ( ) ) ;
440
+ let right = BytesIgnoreNewlineRepr ( right. bytes ( ) . peekable ( ) ) ;
441
+ left. eq ( right)
442
+ }
443
+
444
+ struct BytesIgnoreNewlineRepr < ' a > ( Peekable < Bytes < ' a > > ) ;
445
+
446
+ impl < ' a > Iterator for BytesIgnoreNewlineRepr < ' a > {
447
+ type Item = u8 ;
448
+ fn next ( & mut self ) -> Option < u8 > {
449
+ self . 0 . next ( ) . map ( |c| if c == b'\r' {
450
+ if * self . 0 . peek ( ) . unwrap_or ( & 0 ) == b'\n' {
451
+ self . 0 . next ( ) ;
452
+ b'\n'
453
+ } else {
454
+ b'\r'
455
+ }
456
+ } else {
457
+ c
458
+ } )
459
+ }
460
+ }
461
+
462
+ #[ test]
463
+ fn string_eq_ignore_newline_repr_test ( ) {
464
+ assert ! ( string_eq_ignore_newline_repr( "a\n b\n c\r d" , "a\n b\r \n c\r d" ) ) ;
465
+ }
0 commit comments