@@ -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:: Chars ;
21
23
22
24
use rustfmt:: * ;
23
25
use rustfmt:: filemap:: { write_system_newlines, FileMap } ;
@@ -369,7 +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
- if fmt_text != text {
374
+ // Ignore LF and CRLF difference for Windows.
375
+ if !string_eq_ignore_newline_repr ( & fmt_text, & text) {
373
376
let diff = make_diff ( & text, & fmt_text, DIFF_CONTEXT_SIZE ) ;
374
377
assert ! (
375
378
!diff. is_empty( ) ,
@@ -430,3 +433,38 @@ fn rustfmt_diff_no_diff_test() {
430
433
let diff = make_diff ( "a\n b\n c\n d" , "a\n b\n c\n d" , 3 ) ;
431
434
assert_eq ! ( diff, vec![ ] ) ;
432
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 = CharsIgnoreNewlineRepr ( left. chars ( ) . peekable ( ) ) ;
440
+ let right = CharsIgnoreNewlineRepr ( right. chars ( ) . peekable ( ) ) ;
441
+ left. eq ( right)
442
+ }
443
+
444
+ struct CharsIgnoreNewlineRepr < ' a > ( Peekable < Chars < ' a > > ) ;
445
+
446
+ impl < ' a > Iterator for CharsIgnoreNewlineRepr < ' a > {
447
+ type Item = char ;
448
+ fn next ( & mut self ) -> Option < char > {
449
+ self . 0 . next ( ) . map ( |c| if c == '\r' {
450
+ if * self . 0 . peek ( ) . unwrap_or ( & '\0' ) == '\n' {
451
+ self . 0 . next ( ) ;
452
+ '\n'
453
+ } else {
454
+ '\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( "" , "" ) ) ;
465
+ assert ! ( !string_eq_ignore_newline_repr( "" , "abc" ) ) ;
466
+ assert ! ( !string_eq_ignore_newline_repr( "abc" , "" ) ) ;
467
+ assert ! ( string_eq_ignore_newline_repr( "a\n b\n c\r d" , "a\n b\r \n c\r d" ) ) ;
468
+ assert ! ( string_eq_ignore_newline_repr( "a\r \n \r \n \r \n b" , "a\n \n \n b" ) ) ;
469
+ assert ! ( !string_eq_ignore_newline_repr( "a\r \n bcd" , "a\n bcdefghijk" ) ) ;
470
+ }
0 commit comments