@@ -400,9 +400,23 @@ static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1,
400
400
}
401
401
402
402
403
+ static int is_blank_line (xrecord_t * * recs , long ix , long flags )
404
+ {
405
+ return xdl_blankline (recs [ix ]-> ptr , recs [ix ]-> size , flags );
406
+ }
407
+
408
+ static int recs_match (xrecord_t * * recs , long ixs , long ix , long flags )
409
+ {
410
+ return (recs [ixs ]-> ha == recs [ix ]-> ha &&
411
+ xdl_recmatch (recs [ixs ]-> ptr , recs [ixs ]-> size ,
412
+ recs [ix ]-> ptr , recs [ix ]-> size ,
413
+ flags ));
414
+ }
415
+
403
416
int xdl_change_compact (xdfile_t * xdf , xdfile_t * xdfo , long flags ) {
404
417
long ix , ixo , ixs , ixref , grpsiz , nrec = xdf -> nrec ;
405
418
char * rchg = xdf -> rchg , * rchgo = xdfo -> rchg ;
419
+ unsigned int blank_lines ;
406
420
xrecord_t * * recs = xdf -> recs ;
407
421
408
422
/*
@@ -436,14 +450,14 @@ int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
436
450
437
451
do {
438
452
grpsiz = ix - ixs ;
453
+ blank_lines = 0 ;
439
454
440
455
/*
441
456
* If the line before the current change group, is equal to
442
457
* the last line of the current change group, shift backward
443
458
* the group.
444
459
*/
445
- while (ixs > 0 && recs [ixs - 1 ]-> ha == recs [ix - 1 ]-> ha &&
446
- xdl_recmatch (recs [ixs - 1 ]-> ptr , recs [ixs - 1 ]-> size , recs [ix - 1 ]-> ptr , recs [ix - 1 ]-> size , flags )) {
460
+ while (ixs > 0 && recs_match (recs , ixs - 1 , ix - 1 , flags )) {
447
461
rchg [-- ixs ] = 1 ;
448
462
rchg [-- ix ] = 0 ;
449
463
@@ -470,8 +484,9 @@ int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
470
484
* the line next of the current change group, shift forward
471
485
* the group.
472
486
*/
473
- while (ix < nrec && recs [ixs ]-> ha == recs [ix ]-> ha &&
474
- xdl_recmatch (recs [ixs ]-> ptr , recs [ixs ]-> size , recs [ix ]-> ptr , recs [ix ]-> size , flags )) {
487
+ while (ix < nrec && recs_match (recs , ixs , ix , flags )) {
488
+ blank_lines += is_blank_line (recs , ix , flags );
489
+
475
490
rchg [ixs ++ ] = 0 ;
476
491
rchg [ix ++ ] = 1 ;
477
492
@@ -498,6 +513,23 @@ int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
498
513
rchg [-- ix ] = 0 ;
499
514
while (rchgo [-- ixo ]);
500
515
}
516
+
517
+ /*
518
+ * If a group can be moved back and forth, see if there is a
519
+ * blank line in the moving space. If there is a blank line,
520
+ * make sure the last blank line is the end of the group.
521
+ *
522
+ * As we already shifted the group forward as far as possible
523
+ * in the earlier loop, we need to shift it back only if at all.
524
+ */
525
+ if ((flags & XDF_COMPACTION_HEURISTIC ) && blank_lines ) {
526
+ while (ixs > 0 &&
527
+ !is_blank_line (recs , ix - 1 , flags ) &&
528
+ recs_match (recs , ixs - 1 , ix - 1 , flags )) {
529
+ rchg [-- ixs ] = 1 ;
530
+ rchg [-- ix ] = 0 ;
531
+ }
532
+ }
501
533
}
502
534
503
535
return 0 ;
0 commit comments