@@ -45,24 +45,23 @@ public Diff(string repo, Models.DiffOption opt, int unified, bool ignoreWhitespa
4545 using var proc = new Process ( ) ;
4646 proc . StartInfo = CreateGitStartInfo ( true ) ;
4747 proc . Start ( ) ;
48-
49- var text = await proc . StandardOutput . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
50-
48+ using var ms = new System . IO . MemoryStream ( ) ;
49+ await proc . StandardOutput . BaseStream . CopyToAsync ( ms , CancellationToken ) . ConfigureAwait ( false ) ;
50+ var bytes = ms . ToArray ( ) ;
5151 var start = 0 ;
52- var end = text . IndexOf ( '\n ' , start ) ;
53- while ( end > 0 )
52+ while ( start < bytes . Length )
5453 {
55- var line = text [ start ..end ] ;
56- ParseLine ( line ) ;
57-
58- start = end + 1 ;
59- end = text . IndexOf ( '\n ' , start ) ;
54+ var end = Array . IndexOf ( bytes , ( byte ) '\n ' , start ) ;
55+ if ( end < 0 )
56+ end = bytes . Length ;
57+ var next = end + 1 ;
58+ if ( start <= end - 1 && bytes [ end - 1 ] == '\r ' )
59+ end -- ;
60+ if ( ! _result . IsBinary )
61+ ParseLine ( bytes [ start ..end ] ) ;
62+ start = next ;
6063 }
61-
62- if ( start < text . Length )
63- ParseLine ( text [ start ..] ) ;
64-
65- await proc . WaitForExitAsync ( ) . ConfigureAwait ( false ) ;
64+ await proc . WaitForExitAsync ( CancellationToken ) . ConfigureAwait ( false ) ;
6665 }
6766 catch
6867 {
@@ -82,10 +81,9 @@ public Diff(string repo, Models.DiffOption opt, int unified, bool ignoreWhitespa
8281 return _result ;
8382 }
8483
85- private void ParseLine ( string line )
84+ private void ParseLine ( byte [ ] lineBytes )
8685 {
87- if ( _result . IsBinary )
88- return ;
86+ var line = Encoding . UTF8 . GetString ( lineBytes ) ;
8987
9088 if ( line . StartsWith ( "old mode " , StringComparison . Ordinal ) )
9189 {
@@ -168,7 +166,7 @@ private void ParseLine(string line)
168166
169167 _oldLine = int . Parse ( match . Groups [ 1 ] . Value ) ;
170168 _newLine = int . Parse ( match . Groups [ 2 ] . Value ) ;
171- _last = new Models . TextDiffLine ( Models . TextDiffLineType . Indicator , line , 0 , 0 ) ;
169+ _last = new Models . TextDiffLine ( Models . TextDiffLineType . Indicator , lineBytes , 0 , 0 ) ;
172170 _result . TextDiff . Lines . Add ( _last ) ;
173171 }
174172 }
@@ -177,7 +175,7 @@ private void ParseLine(string line)
177175 if ( line . Length == 0 )
178176 {
179177 ProcessInlineHighlights ( ) ;
180- _last = new Models . TextDiffLine ( Models . TextDiffLineType . Normal , "" , _oldLine , _newLine ) ;
178+ _last = new Models . TextDiffLine ( Models . TextDiffLineType . Normal , Array . Empty < byte > ( ) , _oldLine , _newLine ) ;
181179 _result . TextDiff . Lines . Add ( _last ) ;
182180 _oldLine ++ ;
183181 _newLine ++ ;
@@ -195,7 +193,7 @@ private void ParseLine(string line)
195193 }
196194
197195 _result . TextDiff . DeletedLines ++ ;
198- _last = new Models . TextDiffLine ( Models . TextDiffLineType . Deleted , line . Substring ( 1 ) , _oldLine , 0 ) ;
196+ _last = new Models . TextDiffLine ( Models . TextDiffLineType . Deleted , lineBytes [ 1 .. ] , _oldLine , 0 ) ;
199197 _deleted . Add ( _last ) ;
200198 _oldLine ++ ;
201199 }
@@ -209,7 +207,7 @@ private void ParseLine(string line)
209207 }
210208
211209 _result . TextDiff . AddedLines ++ ;
212- _last = new Models . TextDiffLine ( Models . TextDiffLineType . Added , line . Substring ( 1 ) , 0 , _newLine ) ;
210+ _last = new Models . TextDiffLine ( Models . TextDiffLineType . Added , lineBytes [ 1 .. ] , 0 , _newLine ) ;
213211 _added . Add ( _last ) ;
214212 _newLine ++ ;
215213 }
@@ -221,7 +219,7 @@ private void ParseLine(string line)
221219 {
222220 _oldLine = int . Parse ( match . Groups [ 1 ] . Value ) ;
223221 _newLine = int . Parse ( match . Groups [ 2 ] . Value ) ;
224- _last = new Models . TextDiffLine ( Models . TextDiffLineType . Indicator , line , 0 , 0 ) ;
222+ _last = new Models . TextDiffLine ( Models . TextDiffLineType . Indicator , lineBytes , 0 , 0 ) ;
225223 _result . TextDiff . Lines . Add ( _last ) ;
226224 }
227225 else
@@ -233,7 +231,7 @@ private void ParseLine(string line)
233231 return ;
234232 }
235233
236- _last = new Models . TextDiffLine ( Models . TextDiffLineType . Normal , line . Substring ( 1 ) , _oldLine , _newLine ) ;
234+ _last = new Models . TextDiffLine ( Models . TextDiffLineType . Normal , lineBytes [ 1 .. ] , _oldLine , _newLine ) ;
237235 _result . TextDiff . Lines . Add ( _last ) ;
238236 _oldLine ++ ;
239237 _newLine ++ ;
0 commit comments