11using System ;
2+ using System . Runtime . CompilerServices ;
23using System . Text ;
34
45namespace JavaScriptEngineSwitcher . Benchmarks
56{
67 internal static class Assert
78 {
8- public static void Equal ( string expected , string actual )
9+ private static readonly char [ ] _lineBreakChars = new [ ] { '\r ' , '\n ' } ;
10+
11+
12+ public static void Equal ( string expected , string actual , bool ignoreLineBreaks = false )
913 {
10- if ( actual != expected )
14+ if ( ! EqualInternal ( expected , actual , ignoreLineBreaks ) )
1115 {
1216 var messageBuilder = new StringBuilder ( ) ;
1317 messageBuilder . AppendLine ( "Assert.Equal() Failure" ) ;
@@ -20,5 +24,81 @@ public static void Equal(string expected, string actual)
2024 throw new InvalidOperationException ( errorMessage ) ;
2125 }
2226 }
27+
28+ private static bool EqualInternal ( string a , string b , bool ignoreLineBreaks )
29+ {
30+ if ( ! ignoreLineBreaks )
31+ {
32+ return a . Equals ( b ) ;
33+ }
34+
35+ if ( ReferenceEquals ( a , b ) )
36+ {
37+ return true ;
38+ }
39+
40+ if ( a == null || b == null )
41+ {
42+ return false ;
43+ }
44+
45+ if ( a . IndexOfAny ( _lineBreakChars ) == - 1 && b . IndexOfAny ( _lineBreakChars ) == - 1 )
46+ {
47+ return a . Equals ( b ) ;
48+ }
49+
50+ int aIndex = 0 ;
51+ int aLength = a . Length ;
52+ int bIndex = 0 ;
53+ int bLength = b . Length ;
54+
55+ while ( true )
56+ {
57+ if ( aIndex >= aLength )
58+ {
59+ return bIndex >= bLength ;
60+ }
61+
62+ if ( bIndex >= bLength )
63+ {
64+ return false ;
65+ }
66+
67+ char aChar = a [ aIndex ] ;
68+ char bChar = b [ bIndex ] ;
69+
70+ if ( aChar != bChar )
71+ {
72+ if ( Array . IndexOf ( _lineBreakChars , aChar ) != - 1 && Array . IndexOf ( _lineBreakChars , bChar ) != - 1 )
73+ {
74+ ProcessLineBreaks ( a , aChar , ref aIndex , aLength ) ;
75+ ProcessLineBreaks ( b , bChar , ref bIndex , bLength ) ;
76+
77+ continue ;
78+ }
79+ else
80+ {
81+ return false ;
82+ }
83+ }
84+
85+ aIndex ++ ;
86+ bIndex ++ ;
87+ }
88+ }
89+
90+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
91+ private static void ProcessLineBreaks ( string value , char charValue , ref int charIndex , int charCount )
92+ {
93+ if ( charValue == '\r ' )
94+ {
95+ int nextCharIndex = charIndex + 1 ;
96+ if ( nextCharIndex < charCount && value [ nextCharIndex ] == '\n ' )
97+ {
98+ charIndex ++ ;
99+ }
100+ }
101+ charIndex ++ ;
102+ }
23103 }
24104}
0 commit comments