2323#include "xinclude.h"
2424
2525
26- static int xdl_emit_record (xdfile_t * xdf , long ri , char const * pre , xdemitcb_t * ecb )
27- {
28- xrecord_t * rec = & xdf -> recs [ri ];
26+ /*
27+ * Batch complete diff lines without changing their order or bytes.
28+ * Flush before headers, exceptional records and the final return.
29+ */
30+ struct xdl_emit_buf {
31+ size_t n ;
32+ char buf [16384 ];
33+ };
2934
30- if (xdl_emit_diffrec ((char const * )rec -> ptr , (long )rec -> size , pre , strlen (pre ), ecb ) < 0 )
35+ static int xdl_emit_flush (struct xdl_emit_buf * eb , xdemitcb_t * ecb )
36+ {
37+ mmfile_t mb ;
38+
39+ if (!eb -> n )
40+ return 0 ;
41+ mb .ptr = eb -> buf ;
42+ mb .size = eb -> n ;
43+ eb -> n = 0 ;
44+ if (ecb -> out_line (ecb -> priv , & mb , 1 ) < 0 )
3145 return -1 ;
46+ return 0 ;
47+ }
48+
3249
50+ static int xdl_emit_records (struct xdl_emit_buf * eb , xdfile_t * xdf ,
51+ long s , long e , char pre , xdemitcb_t * ecb )
52+ {
53+ for (; s < e ; s ++ ) {
54+ xrecord_t * rec = & xdf -> recs [s ];
55+ size_t size = rec -> size ;
56+
57+ if (!size || rec -> ptr [size - 1 ] != '\n' ||
58+ size >= sizeof (eb -> buf )) {
59+ if (xdl_emit_flush (eb , ecb ) < 0 ||
60+ xdl_emit_diffrec ((char const * )rec -> ptr , (long )size ,
61+ & pre , 1 , ecb ) < 0 )
62+ return -1 ;
63+ continue ;
64+ }
65+ if (eb -> n + size + 1 > sizeof (eb -> buf ) &&
66+ xdl_emit_flush (eb , ecb ) < 0 )
67+ return -1 ;
68+ eb -> buf [eb -> n ++ ] = pre ;
69+ memcpy (eb -> buf + eb -> n , rec -> ptr , size );
70+ eb -> n += size ;
71+ }
3372 return 0 ;
3473}
3574
@@ -172,6 +211,9 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
172211 xdchange_t * xch , * xche ;
173212 long funclineprev = -1 ;
174213 struct func_line func_line = { 0 };
214+ struct xdl_emit_buf eb ;
215+
216+ eb .n = 0 ;
175217
176218 for (xch = xscr ; xch ; xch = xche -> next ) {
177219 xdchange_t * xchp = xch ;
@@ -280,6 +322,8 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
280322 s1 - 1 , funclineprev );
281323 funclineprev = s1 - 1 ;
282324 }
325+ if (xdl_emit_flush (& eb , ecb ) < 0 )
326+ return -1 ;
283327 if (!(xecfg -> flags & XDL_EMIT_NO_HUNK_HDR ) &&
284328 xdl_emit_hunk_hdr (s1 + 1 , e1 - s1 , s2 + 1 , e2 - s2 ,
285329 func_line .buf , func_line .len , ecb ) < 0 )
@@ -288,31 +332,33 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
288332 /*
289333 * Emit pre-context.
290334 */
291- for (; s2 < xch -> i2 ; s2 ++ )
292- if (xdl_emit_record (& xe -> xdf2 , s2 , " " , ecb ) < 0 )
293- return -1 ;
335+ if (xdl_emit_records (& eb , & xe -> xdf2 , s2 , xch -> i2 , ' ' , ecb ) < 0 )
336+ return -1 ;
294337
295338 for (s1 = xch -> i1 , s2 = xch -> i2 ;; xch = xch -> next ) {
296339 /*
297340 * Merge previous with current change atom.
298341 */
299- for (; s1 < xch -> i1 && s2 < xch -> i2 ; s1 ++ , s2 ++ )
300- if (xdl_emit_record (& xe -> xdf2 , s2 , " " , ecb ) < 0 )
342+ if (s1 < xch -> i1 && s2 < xch -> i2 ) {
343+ long k = XDL_MIN (xch -> i1 - s1 , xch -> i2 - s2 );
344+
345+ if (xdl_emit_records (& eb , & xe -> xdf2 , s2 , s2 + k , ' ' , ecb ) < 0 )
301346 return -1 ;
347+ }
302348
303349 /*
304350 * Removes lines from the first file.
305351 */
306- for ( s1 = xch -> i1 ; s1 < xch -> i1 + xch -> chg1 ; s1 ++ )
307- if ( xdl_emit_record ( & xe -> xdf1 , s1 , "-" , ecb ) < 0 )
308- return -1 ;
352+ if ( xdl_emit_records ( & eb , & xe -> xdf1 , xch -> i1 ,
353+ xch -> i1 + xch -> chg1 , '-' , ecb ) < 0 )
354+ return -1 ;
309355
310356 /*
311357 * Adds lines from the second file.
312358 */
313- for ( s2 = xch -> i2 ; s2 < xch -> i2 + xch -> chg2 ; s2 ++ )
314- if ( xdl_emit_record ( & xe -> xdf2 , s2 , "+" , ecb ) < 0 )
315- return -1 ;
359+ if ( xdl_emit_records ( & eb , & xe -> xdf2 , xch -> i2 ,
360+ xch -> i2 + xch -> chg2 , '+' , ecb ) < 0 )
361+ return -1 ;
316362
317363 if (xch == xche )
318364 break ;
@@ -323,10 +369,10 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
323369 /*
324370 * Emit post-context.
325371 */
326- for ( s2 = xche -> i2 + xche -> chg2 ; s2 < e2 ; s2 ++ )
327- if ( xdl_emit_record ( & xe -> xdf2 , s2 , " " , ecb ) < 0 )
328- return -1 ;
372+ if ( xdl_emit_records ( & eb , & xe -> xdf2 , xche -> i2 + xche -> chg2 ,
373+ e2 , ' ' , ecb ) < 0 )
374+ return -1 ;
329375 }
330376
331- return 0 ;
377+ return xdl_emit_flush ( & eb , ecb ) ;
332378}
0 commit comments