@@ -2736,7 +2736,10 @@ fn pre_process<'a>(
27362736fn newline_count ( body : & str ) -> usize {
27372737 #[ cfg( feature = "simd" ) ]
27382738 {
2739- memchr:: memchr_iter ( b'\n' , body. as_bytes ( ) ) . count ( )
2739+ // Trailing newlines do not count towards the number of lines
2740+ // (this is based into `str::lines`)
2741+ let trailing_newline = body. ends_with ( '\n' ) ;
2742+ memchr:: memchr_iter ( b'\n' , body. as_bytes ( ) ) . count ( ) - usize:: from ( trailing_newline)
27402743 }
27412744 #[ cfg( not( feature = "simd" ) ) ]
27422745 {
@@ -2782,9 +2785,20 @@ mod test {
27822785 [dependencies]
27832786 bar = { base = '^^not-valid^^', path = 'bar' }
27842787 "# ;
2785- let actual_count = newline_count ( source) ;
2786- let expected_count = 10 ;
2788+ assert_eq ! ( newline_count( source) , 10 ) ;
27872789
2788- assert_eq ! ( expected_count, actual_count) ;
2790+ assert_eq ! ( newline_count( "" ) , 0 ) ;
2791+
2792+ assert_eq ! ( newline_count( "one" ) , 0 ) ;
2793+
2794+ assert_eq ! ( newline_count( "one\n " ) , 0 ) ;
2795+
2796+ assert_eq ! ( newline_count( "one\n two" ) , 1 ) ;
2797+
2798+ assert_eq ! ( newline_count( "one\n two\n " ) , 1 ) ;
2799+
2800+ assert_eq ! ( newline_count( "one\n \n " ) , 1 ) ;
2801+
2802+ assert_eq ! ( newline_count( "one\r \n two\r \n " ) , 1 ) ;
27892803 }
27902804}
0 commit comments