1/* 2 * LibXDiff by Davide Libenzi ( File Differential Library ) 3 * Copyright (C) 2003 Davide Libenzi 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2.1 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, see 17 * <http://www.gnu.org/licenses/>. 18 * 19 * Davide Libenzi <davidel@xmailserver.org> 20 * 21 */ 22 23#include"xinclude.h" 24 25#define XDL_MAX_COST_MIN 256 26#define XDL_HEUR_MIN_COST 256 27#define XDL_LINE_MAX (long)((1UL << (CHAR_BIT * sizeof(long) - 1)) - 1) 28#define XDL_SNAKE_CNT 20 29#define XDL_K_HEUR 4 30 31typedefstruct s_xdpsplit { 32long i1, i2; 33int min_lo, min_hi; 34} xdpsplit_t; 35 36/* 37 * See "An O(ND) Difference Algorithm and its Variations", by Eugene Myers. 38 * Basically considers a "box" (off1, off2, lim1, lim2) and scan from both 39 * the forward diagonal starting from (off1, off2) and the backward diagonal 40 * starting from (lim1, lim2). If the K values on the same diagonal crosses 41 * returns the furthest point of reach. We might end up having to expensive 42 * cases using this algorithm is full, so a little bit of heuristic is needed 43 * to cut the search and to return a suboptimal point. 44 */ 45static longxdl_split(unsigned long const*ha1,long off1,long lim1, 46unsigned long const*ha2,long off2,long lim2, 47long*kvdf,long*kvdb,int need_min, xdpsplit_t *spl, 48 xdalgoenv_t *xenv) { 49long dmin = off1 - lim2, dmax = lim1 - off2; 50long fmid = off1 - off2, bmid = lim1 - lim2; 51long odd = (fmid - bmid) &1; 52long fmin = fmid, fmax = fmid; 53long bmin = bmid, bmax = bmid; 54long ec, d, i1, i2, prev1, best, dd, v, k; 55 56/* 57 * Set initial diagonal values for both forward and backward path. 58 */ 59 kvdf[fmid] = off1; 60 kvdb[bmid] = lim1; 61 62for(ec =1;; ec++) { 63int got_snake =0; 64 65/* 66 * We need to extent the diagonal "domain" by one. If the next 67 * values exits the box boundaries we need to change it in the 68 * opposite direction because (max - min) must be a power of two. 69 * Also we initialize the external K value to -1 so that we can 70 * avoid extra conditions check inside the core loop. 71 */ 72if(fmin > dmin) 73 kvdf[--fmin -1] = -1; 74else 75++fmin; 76if(fmax < dmax) 77 kvdf[++fmax +1] = -1; 78else 79--fmax; 80 81for(d = fmax; d >= fmin; d -=2) { 82if(kvdf[d -1] >= kvdf[d +1]) 83 i1 = kvdf[d -1] +1; 84else 85 i1 = kvdf[d +1]; 86 prev1 = i1; 87 i2 = i1 - d; 88for(; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++); 89if(i1 - prev1 > xenv->snake_cnt) 90 got_snake =1; 91 kvdf[d] = i1; 92if(odd && bmin <= d && d <= bmax && kvdb[d] <= i1) { 93 spl->i1 = i1; 94 spl->i2 = i2; 95 spl->min_lo = spl->min_hi =1; 96return ec; 97} 98} 99 100/* 101 * We need to extent the diagonal "domain" by one. If the next 102 * values exits the box boundaries we need to change it in the 103 * opposite direction because (max - min) must be a power of two. 104 * Also we initialize the external K value to -1 so that we can 105 * avoid extra conditions check inside the core loop. 106 */ 107if(bmin > dmin) 108 kvdb[--bmin -1] = XDL_LINE_MAX; 109else 110++bmin; 111if(bmax < dmax) 112 kvdb[++bmax +1] = XDL_LINE_MAX; 113else 114--bmax; 115 116for(d = bmax; d >= bmin; d -=2) { 117if(kvdb[d -1] < kvdb[d +1]) 118 i1 = kvdb[d -1]; 119else 120 i1 = kvdb[d +1] -1; 121 prev1 = i1; 122 i2 = i1 - d; 123for(; i1 > off1 && i2 > off2 && ha1[i1 -1] == ha2[i2 -1]; i1--, i2--); 124if(prev1 - i1 > xenv->snake_cnt) 125 got_snake =1; 126 kvdb[d] = i1; 127if(!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) { 128 spl->i1 = i1; 129 spl->i2 = i2; 130 spl->min_lo = spl->min_hi =1; 131return ec; 132} 133} 134 135if(need_min) 136continue; 137 138/* 139 * If the edit cost is above the heuristic trigger and if 140 * we got a good snake, we sample current diagonals to see 141 * if some of the, have reached an "interesting" path. Our 142 * measure is a function of the distance from the diagonal 143 * corner (i1 + i2) penalized with the distance from the 144 * mid diagonal itself. If this value is above the current 145 * edit cost times a magic factor (XDL_K_HEUR) we consider 146 * it interesting. 147 */ 148if(got_snake && ec > xenv->heur_min) { 149for(best =0, d = fmax; d >= fmin; d -=2) { 150 dd = d > fmid ? d - fmid: fmid - d; 151 i1 = kvdf[d]; 152 i2 = i1 - d; 153 v = (i1 - off1) + (i2 - off2) - dd; 154 155if(v > XDL_K_HEUR * ec && v > best && 156 off1 + xenv->snake_cnt <= i1 && i1 < lim1 && 157 off2 + xenv->snake_cnt <= i2 && i2 < lim2) { 158for(k =1; ha1[i1 - k] == ha2[i2 - k]; k++) 159if(k == xenv->snake_cnt) { 160 best = v; 161 spl->i1 = i1; 162 spl->i2 = i2; 163break; 164} 165} 166} 167if(best >0) { 168 spl->min_lo =1; 169 spl->min_hi =0; 170return ec; 171} 172 173for(best =0, d = bmax; d >= bmin; d -=2) { 174 dd = d > bmid ? d - bmid: bmid - d; 175 i1 = kvdb[d]; 176 i2 = i1 - d; 177 v = (lim1 - i1) + (lim2 - i2) - dd; 178 179if(v > XDL_K_HEUR * ec && v > best && 180 off1 < i1 && i1 <= lim1 - xenv->snake_cnt && 181 off2 < i2 && i2 <= lim2 - xenv->snake_cnt) { 182for(k =0; ha1[i1 + k] == ha2[i2 + k]; k++) 183if(k == xenv->snake_cnt -1) { 184 best = v; 185 spl->i1 = i1; 186 spl->i2 = i2; 187break; 188} 189} 190} 191if(best >0) { 192 spl->min_lo =0; 193 spl->min_hi =1; 194return ec; 195} 196} 197 198/* 199 * Enough is enough. We spent too much time here and now we collect 200 * the furthest reaching path using the (i1 + i2) measure. 201 */ 202if(ec >= xenv->mxcost) { 203long fbest, fbest1, bbest, bbest1; 204 205 fbest = fbest1 = -1; 206for(d = fmax; d >= fmin; d -=2) { 207 i1 =XDL_MIN(kvdf[d], lim1); 208 i2 = i1 - d; 209if(lim2 < i2) 210 i1 = lim2 + d, i2 = lim2; 211if(fbest < i1 + i2) { 212 fbest = i1 + i2; 213 fbest1 = i1; 214} 215} 216 217 bbest = bbest1 = XDL_LINE_MAX; 218for(d = bmax; d >= bmin; d -=2) { 219 i1 =XDL_MAX(off1, kvdb[d]); 220 i2 = i1 - d; 221if(i2 < off2) 222 i1 = off2 + d, i2 = off2; 223if(i1 + i2 < bbest) { 224 bbest = i1 + i2; 225 bbest1 = i1; 226} 227} 228 229if((lim1 + lim2) - bbest < fbest - (off1 + off2)) { 230 spl->i1 = fbest1; 231 spl->i2 = fbest - fbest1; 232 spl->min_lo =1; 233 spl->min_hi =0; 234}else{ 235 spl->i1 = bbest1; 236 spl->i2 = bbest - bbest1; 237 spl->min_lo =0; 238 spl->min_hi =1; 239} 240return ec; 241} 242} 243} 244 245 246/* 247 * Rule: "Divide et Impera". Recursively split the box in sub-boxes by calling 248 * the box splitting function. Note that the real job (marking changed lines) 249 * is done in the two boundary reaching checks. 250 */ 251intxdl_recs_cmp(diffdata_t *dd1,long off1,long lim1, 252 diffdata_t *dd2,long off2,long lim2, 253long*kvdf,long*kvdb,int need_min, xdalgoenv_t *xenv) { 254unsigned long const*ha1 = dd1->ha, *ha2 = dd2->ha; 255 256/* 257 * Shrink the box by walking through each diagonal snake (SW and NE). 258 */ 259for(; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++); 260for(; off1 < lim1 && off2 < lim2 && ha1[lim1 -1] == ha2[lim2 -1]; lim1--, lim2--); 261 262/* 263 * If one dimension is empty, then all records on the other one must 264 * be obviously changed. 265 */ 266if(off1 == lim1) { 267char*rchg2 = dd2->rchg; 268long*rindex2 = dd2->rindex; 269 270for(; off2 < lim2; off2++) 271 rchg2[rindex2[off2]] =1; 272}else if(off2 == lim2) { 273char*rchg1 = dd1->rchg; 274long*rindex1 = dd1->rindex; 275 276for(; off1 < lim1; off1++) 277 rchg1[rindex1[off1]] =1; 278}else{ 279 xdpsplit_t spl; 280 spl.i1 = spl.i2 =0; 281 282/* 283 * Divide ... 284 */ 285if(xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb, 286 need_min, &spl, xenv) <0) { 287 288return-1; 289} 290 291/* 292 * ... et Impera. 293 */ 294if(xdl_recs_cmp(dd1, off1, spl.i1, dd2, off2, spl.i2, 295 kvdf, kvdb, spl.min_lo, xenv) <0|| 296xdl_recs_cmp(dd1, spl.i1, lim1, dd2, spl.i2, lim2, 297 kvdf, kvdb, spl.min_hi, xenv) <0) { 298 299return-1; 300} 301} 302 303return0; 304} 305 306 307intxdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const*xpp, 308 xdfenv_t *xe) { 309long ndiags; 310long*kvd, *kvdf, *kvdb; 311 xdalgoenv_t xenv; 312 diffdata_t dd1, dd2; 313 314if(XDF_DIFF_ALG(xpp->flags) == XDF_PATIENCE_DIFF) 315returnxdl_do_patience_diff(mf1, mf2, xpp, xe); 316 317if(XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF) 318returnxdl_do_histogram_diff(mf1, mf2, xpp, xe); 319 320if(xdl_prepare_env(mf1, mf2, xpp, xe) <0) { 321 322return-1; 323} 324 325/* 326 * Allocate and setup K vectors to be used by the differential algorithm. 327 * One is to store the forward path and one to store the backward path. 328 */ 329 ndiags = xe->xdf1.nreff + xe->xdf2.nreff +3; 330if(!(kvd = (long*)xdl_malloc((2* ndiags +2) *sizeof(long)))) { 331 332xdl_free_env(xe); 333return-1; 334} 335 kvdf = kvd; 336 kvdb = kvdf + ndiags; 337 kvdf += xe->xdf2.nreff +1; 338 kvdb += xe->xdf2.nreff +1; 339 340 xenv.mxcost =xdl_bogosqrt(ndiags); 341if(xenv.mxcost < XDL_MAX_COST_MIN) 342 xenv.mxcost = XDL_MAX_COST_MIN; 343 xenv.snake_cnt = XDL_SNAKE_CNT; 344 xenv.heur_min = XDL_HEUR_MIN_COST; 345 346 dd1.nrec = xe->xdf1.nreff; 347 dd1.ha = xe->xdf1.ha; 348 dd1.rchg = xe->xdf1.rchg; 349 dd1.rindex = xe->xdf1.rindex; 350 dd2.nrec = xe->xdf2.nreff; 351 dd2.ha = xe->xdf2.ha; 352 dd2.rchg = xe->xdf2.rchg; 353 dd2.rindex = xe->xdf2.rindex; 354 355if(xdl_recs_cmp(&dd1,0, dd1.nrec, &dd2,0, dd2.nrec, 356 kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) !=0, &xenv) <0) { 357 358xdl_free(kvd); 359xdl_free_env(xe); 360return-1; 361} 362 363xdl_free(kvd); 364 365return0; 366} 367 368 369static xdchange_t *xdl_add_change(xdchange_t *xscr,long i1,long i2,long chg1,long chg2) { 370 xdchange_t *xch; 371 372if(!(xch = (xdchange_t *)xdl_malloc(sizeof(xdchange_t)))) 373return NULL; 374 375 xch->next = xscr; 376 xch->i1 = i1; 377 xch->i2 = i2; 378 xch->chg1 = chg1; 379 xch->chg2 = chg2; 380 xch->ignore =0; 381 382return xch; 383} 384 385 386static intrecs_match(xrecord_t *rec1, xrecord_t *rec2,long flags) 387{ 388return(rec1->ha == rec2->ha && 389xdl_recmatch(rec1->ptr, rec1->size, 390 rec2->ptr, rec2->size, 391 flags)); 392} 393 394/* 395 * If a line is indented more than this, get_indent() just returns this value. 396 * This avoids having to do absurd amounts of work for data that are not 397 * human-readable text, and also ensures that the output of get_indent fits within 398 * an int. 399 */ 400#define MAX_INDENT 200 401 402/* 403 * Return the amount of indentation of the specified line, treating TAB as 8 404 * columns. Return -1 if line is empty or contains only whitespace. Clamp the 405 * output value at MAX_INDENT. 406 */ 407static intget_indent(xrecord_t *rec) 408{ 409long i; 410int ret =0; 411 412for(i =0; i < rec->size; i++) { 413char c = rec->ptr[i]; 414 415if(!XDL_ISSPACE(c)) 416return ret; 417else if(c ==' ') 418 ret +=1; 419else if(c =='\t') 420 ret +=8- ret %8; 421/* ignore other whitespace characters */ 422 423if(ret >= MAX_INDENT) 424return MAX_INDENT; 425} 426 427/* The line contains only whitespace. */ 428return-1; 429} 430 431/* 432 * If more than this number of consecutive blank rows are found, just return this 433 * value. This avoids requiring O(N^2) work for pathological cases, and also 434 * ensures that the output of score_split fits in an int. 435 */ 436#define MAX_BLANKS 20 437 438/* Characteristics measured about a hypothetical split position. */ 439struct split_measurement { 440/* 441 * Is the split at the end of the file (aside from any blank lines)? 442 */ 443int end_of_file; 444 445/* 446 * How much is the line immediately following the split indented (or -1 if 447 * the line is blank): 448 */ 449int indent; 450 451/* 452 * How many consecutive lines above the split are blank? 453 */ 454int pre_blank; 455 456/* 457 * How much is the nearest non-blank line above the split indented (or -1 458 * if there is no such line)? 459 */ 460int pre_indent; 461 462/* 463 * How many lines after the line following the split are blank? 464 */ 465int post_blank; 466 467/* 468 * How much is the nearest non-blank line after the line following the 469 * split indented (or -1 if there is no such line)? 470 */ 471int post_indent; 472}; 473 474struct split_score { 475/* The effective indent of this split (smaller is preferred). */ 476int effective_indent; 477 478/* Penalty for this split (smaller is preferred). */ 479int penalty; 480}; 481 482/* 483 * Fill m with information about a hypothetical split of xdf above line split. 484 */ 485static voidmeasure_split(const xdfile_t *xdf,long split, 486struct split_measurement *m) 487{ 488long i; 489 490if(split >= xdf->nrec) { 491 m->end_of_file =1; 492 m->indent = -1; 493}else{ 494 m->end_of_file =0; 495 m->indent =get_indent(xdf->recs[split]); 496} 497 498 m->pre_blank =0; 499 m->pre_indent = -1; 500for(i = split -1; i >=0; i--) { 501 m->pre_indent =get_indent(xdf->recs[i]); 502if(m->pre_indent != -1) 503break; 504 m->pre_blank +=1; 505if(m->pre_blank == MAX_BLANKS) { 506 m->pre_indent =0; 507break; 508} 509} 510 511 m->post_blank =0; 512 m->post_indent = -1; 513for(i = split +1; i < xdf->nrec; i++) { 514 m->post_indent =get_indent(xdf->recs[i]); 515if(m->post_indent != -1) 516break; 517 m->post_blank +=1; 518if(m->post_blank == MAX_BLANKS) { 519 m->post_indent =0; 520break; 521} 522} 523} 524 525/* 526 * The empirically-determined weight factors used by score_split() below. 527 * Larger values means that the position is a less favorable place to split. 528 * 529 * Note that scores are only ever compared against each other, so multiplying 530 * all of these weight/penalty values by the same factor wouldn't change the 531 * heuristic's behavior. Still, we need to set that arbitrary scale *somehow*. 532 * In practice, these numbers are chosen to be large enough that they can be 533 * adjusted relative to each other with sufficient precision despite using 534 * integer math. 535 */ 536 537/* Penalty if there are no non-blank lines before the split */ 538#define START_OF_FILE_PENALTY 1 539 540/* Penalty if there are no non-blank lines after the split */ 541#define END_OF_FILE_PENALTY 21 542 543/* Multiplier for the number of blank lines around the split */ 544#define TOTAL_BLANK_WEIGHT (-30) 545 546/* Multiplier for the number of blank lines after the split */ 547#define POST_BLANK_WEIGHT 6 548 549/* 550 * Penalties applied if the line is indented more than its predecessor 551 */ 552#define RELATIVE_INDENT_PENALTY (-4) 553#define RELATIVE_INDENT_WITH_BLANK_PENALTY 10 554 555/* 556 * Penalties applied if the line is indented less than both its predecessor and 557 * its successor 558 */ 559#define RELATIVE_OUTDENT_PENALTY 24 560#define RELATIVE_OUTDENT_WITH_BLANK_PENALTY 17 561 562/* 563 * Penalties applied if the line is indented less than its predecessor but not 564 * less than its successor 565 */ 566#define RELATIVE_DEDENT_PENALTY 23 567#define RELATIVE_DEDENT_WITH_BLANK_PENALTY 17 568 569/* 570 * We only consider whether the sum of the effective indents for splits are 571 * less than (-1), equal to (0), or greater than (+1) each other. The resulting 572 * value is multiplied by the following weight and combined with the penalty to 573 * determine the better of two scores. 574 */ 575#define INDENT_WEIGHT 60 576 577/* 578 * How far do we slide a hunk at most? 579 */ 580#define INDENT_HEURISTIC_MAX_SLIDING 100 581 582/* 583 * Compute a badness score for the hypothetical split whose measurements are 584 * stored in m. The weight factors were determined empirically using the tools and 585 * corpus described in 586 * 587 * https://github.com/mhagger/diff-slider-tools 588 * 589 * Also see that project if you want to improve the weights based on, for example, 590 * a larger or more diverse corpus. 591 */ 592static voidscore_add_split(const struct split_measurement *m,struct split_score *s) 593{ 594/* 595 * A place to accumulate penalty factors (positive makes this index more 596 * favored): 597 */ 598int post_blank, total_blank, indent, any_blanks; 599 600if(m->pre_indent == -1&& m->pre_blank ==0) 601 s->penalty += START_OF_FILE_PENALTY; 602 603if(m->end_of_file) 604 s->penalty += END_OF_FILE_PENALTY; 605 606/* 607 * Set post_blank to the number of blank lines following the split, 608 * including the line immediately after the split: 609 */ 610 post_blank = (m->indent == -1) ?1+ m->post_blank :0; 611 total_blank = m->pre_blank + post_blank; 612 613/* Penalties based on nearby blank lines: */ 614 s->penalty += TOTAL_BLANK_WEIGHT * total_blank; 615 s->penalty += POST_BLANK_WEIGHT * post_blank; 616 617if(m->indent != -1) 618 indent = m->indent; 619else 620 indent = m->post_indent; 621 622 any_blanks = (total_blank !=0); 623 624/* Note that the effective indent is -1 at the end of the file: */ 625 s->effective_indent += indent; 626 627if(indent == -1) { 628/* No additional adjustments needed. */ 629}else if(m->pre_indent == -1) { 630/* No additional adjustments needed. */ 631}else if(indent > m->pre_indent) { 632/* 633 * The line is indented more than its predecessor. 634 */ 635 s->penalty += any_blanks ? 636 RELATIVE_INDENT_WITH_BLANK_PENALTY : 637 RELATIVE_INDENT_PENALTY; 638}else if(indent == m->pre_indent) { 639/* 640 * The line has the same indentation level as its predecessor. 641 * No additional adjustments needed. 642 */ 643}else{ 644/* 645 * The line is indented less than its predecessor. It could be 646 * the block terminator of the previous block, but it could 647 * also be the start of a new block (e.g., an "else" block, or 648 * maybe the previous block didn't have a block terminator). 649 * Try to distinguish those cases based on what comes next: 650 */ 651if(m->post_indent != -1&& m->post_indent > indent) { 652/* 653 * The following line is indented more. So it is likely 654 * that this line is the start of a block. 655 */ 656 s->penalty += any_blanks ? 657 RELATIVE_OUTDENT_WITH_BLANK_PENALTY : 658 RELATIVE_OUTDENT_PENALTY; 659}else{ 660/* 661 * That was probably the end of a block. 662 */ 663 s->penalty += any_blanks ? 664 RELATIVE_DEDENT_WITH_BLANK_PENALTY : 665 RELATIVE_DEDENT_PENALTY; 666} 667} 668} 669 670static intscore_cmp(struct split_score *s1,struct split_score *s2) 671{ 672/* -1 if s1.effective_indent < s2->effective_indent, etc. */ 673int cmp_indents = ((s1->effective_indent > s2->effective_indent) - 674(s1->effective_indent < s2->effective_indent)); 675 676return INDENT_WEIGHT * cmp_indents + (s1->penalty - s2->penalty); 677} 678 679/* 680 * Represent a group of changed lines in an xdfile_t (i.e., a contiguous group 681 * of lines that was inserted or deleted from the corresponding version of the 682 * file). We consider there to be such a group at the beginning of the file, at 683 * the end of the file, and between any two unchanged lines, though most such 684 * groups will usually be empty. 685 * 686 * If the first line in a group is equal to the line following the group, then 687 * the group can be slid down. Similarly, if the last line in a group is equal 688 * to the line preceding the group, then the group can be slid up. See 689 * group_slide_down() and group_slide_up(). 690 * 691 * Note that loops that are testing for changed lines in xdf->rchg do not need 692 * index bounding since the array is prepared with a zero at position -1 and N. 693 */ 694struct xdlgroup { 695/* 696 * The index of the first changed line in the group, or the index of 697 * the unchanged line above which the (empty) group is located. 698 */ 699long start; 700 701/* 702 * The index of the first unchanged line after the group. For an empty 703 * group, end is equal to start. 704 */ 705long end; 706}; 707 708/* 709 * Initialize g to point at the first group in xdf. 710 */ 711static voidgroup_init(xdfile_t *xdf,struct xdlgroup *g) 712{ 713 g->start = g->end =0; 714while(xdf->rchg[g->end]) 715 g->end++; 716} 717 718/* 719 * Move g to describe the next (possibly empty) group in xdf and return 0. If g 720 * is already at the end of the file, do nothing and return -1. 721 */ 722staticinlineintgroup_next(xdfile_t *xdf,struct xdlgroup *g) 723{ 724if(g->end == xdf->nrec) 725return-1; 726 727 g->start = g->end +1; 728for(g->end = g->start; xdf->rchg[g->end]; g->end++) 729; 730 731return0; 732} 733 734/* 735 * Move g to describe the previous (possibly empty) group in xdf and return 0. 736 * If g is already at the beginning of the file, do nothing and return -1. 737 */ 738staticinlineintgroup_previous(xdfile_t *xdf,struct xdlgroup *g) 739{ 740if(g->start ==0) 741return-1; 742 743 g->end = g->start -1; 744for(g->start = g->end; xdf->rchg[g->start -1]; g->start--) 745; 746 747return0; 748} 749 750/* 751 * If g can be slid toward the end of the file, do so, and if it bumps into a 752 * following group, expand this group to include it. Return 0 on success or -1 753 * if g cannot be slid down. 754 */ 755static intgroup_slide_down(xdfile_t *xdf,struct xdlgroup *g,long flags) 756{ 757if(g->end < xdf->nrec && 758recs_match(xdf->recs[g->start], xdf->recs[g->end], flags)) { 759 xdf->rchg[g->start++] =0; 760 xdf->rchg[g->end++] =1; 761 762while(xdf->rchg[g->end]) 763 g->end++; 764 765return0; 766}else{ 767return-1; 768} 769} 770 771/* 772 * If g can be slid toward the beginning of the file, do so, and if it bumps 773 * into a previous group, expand this group to include it. Return 0 on success 774 * or -1 if g cannot be slid up. 775 */ 776static intgroup_slide_up(xdfile_t *xdf,struct xdlgroup *g,long flags) 777{ 778if(g->start >0&& 779recs_match(xdf->recs[g->start -1], xdf->recs[g->end -1], flags)) { 780 xdf->rchg[--g->start] =1; 781 xdf->rchg[--g->end] =0; 782 783while(xdf->rchg[g->start -1]) 784 g->start--; 785 786return0; 787}else{ 788return-1; 789} 790} 791 792static voidxdl_bug(const char*msg) 793{ 794fprintf(stderr,"BUG:%s\n", msg); 795exit(1); 796} 797 798/* 799 * Move back and forward change groups for a consistent and pretty diff output. 800 * This also helps in finding joinable change groups and reducing the diff 801 * size. 802 */ 803intxdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo,long flags) { 804struct xdlgroup g, go; 805long earliest_end, end_matching_other; 806long groupsize; 807 808group_init(xdf, &g); 809group_init(xdfo, &go); 810 811while(1) { 812/* If the group is empty in the to-be-compacted file, skip it: */ 813if(g.end == g.start) 814goto next; 815 816/* 817 * Now shift the change up and then down as far as possible in 818 * each direction. If it bumps into any other changes, merge them. 819 */ 820do{ 821 groupsize = g.end - g.start; 822 823/* 824 * Keep track of the last "end" index that causes this 825 * group to align with a group of changed lines in the 826 * other file. -1 indicates that we haven't found such 827 * a match yet: 828 */ 829 end_matching_other = -1; 830 831/* Shift the group backward as much as possible: */ 832while(!group_slide_up(xdf, &g, flags)) 833if(group_previous(xdfo, &go)) 834xdl_bug("group sync broken sliding up"); 835 836/* 837 * This is this highest that this group can be shifted. 838 * Record its end index: 839 */ 840 earliest_end = g.end; 841 842if(go.end > go.start) 843 end_matching_other = g.end; 844 845/* Now shift the group forward as far as possible: */ 846while(1) { 847if(group_slide_down(xdf, &g, flags)) 848break; 849if(group_next(xdfo, &go)) 850xdl_bug("group sync broken sliding down"); 851 852if(go.end > go.start) 853 end_matching_other = g.end; 854} 855}while(groupsize != g.end - g.start); 856 857/* 858 * If the group can be shifted, then we can possibly use this 859 * freedom to produce a more intuitive diff. 860 * 861 * The group is currently shifted as far down as possible, so the 862 * heuristics below only have to handle upwards shifts. 863 */ 864 865if(g.end == earliest_end) { 866/* no shifting was possible */ 867}else if(end_matching_other != -1) { 868/* 869 * Move the possibly merged group of changes back to line 870 * up with the last group of changes from the other file 871 * that it can align with. 872 */ 873while(go.end == go.start) { 874if(group_slide_up(xdf, &g, flags)) 875xdl_bug("match disappeared"); 876if(group_previous(xdfo, &go)) 877xdl_bug("group sync broken sliding to match"); 878} 879}else if(flags & XDF_INDENT_HEURISTIC) { 880/* 881 * Indent heuristic: a group of pure add/delete lines 882 * implies two splits, one between the end of the "before" 883 * context and the start of the group, and another between 884 * the end of the group and the beginning of the "after" 885 * context. Some splits are aesthetically better and some 886 * are worse. We compute a badness "score" for each split, 887 * and add the scores for the two splits to define a 888 * "score" for each position that the group can be shifted 889 * to. Then we pick the shift with the lowest score. 890 */ 891long shift, best_shift = -1; 892struct split_score best_score; 893 894 shift = earliest_end; 895if(g.end - groupsize -1> shift) 896 shift = g.end - groupsize -1; 897if(g.end - INDENT_HEURISTIC_MAX_SLIDING > shift) 898 shift = g.end - INDENT_HEURISTIC_MAX_SLIDING; 899for(; shift <= g.end; shift++) { 900struct split_measurement m; 901struct split_score score = {0,0}; 902 903measure_split(xdf, shift, &m); 904score_add_split(&m, &score); 905measure_split(xdf, shift - groupsize, &m); 906score_add_split(&m, &score); 907if(best_shift == -1|| 908score_cmp(&score, &best_score) <=0) { 909 best_score.effective_indent = score.effective_indent; 910 best_score.penalty = score.penalty; 911 best_shift = shift; 912} 913} 914 915while(g.end > best_shift) { 916if(group_slide_up(xdf, &g, flags)) 917xdl_bug("best shift unreached"); 918if(group_previous(xdfo, &go)) 919xdl_bug("group sync broken sliding to blank line"); 920} 921} 922 923 next: 924/* Move past the just-processed group: */ 925if(group_next(xdf, &g)) 926break; 927if(group_next(xdfo, &go)) 928xdl_bug("group sync broken moving to next group"); 929} 930 931if(!group_next(xdfo, &go)) 932xdl_bug("group sync broken at end of file"); 933 934return0; 935} 936 937 938intxdl_build_script(xdfenv_t *xe, xdchange_t **xscr) { 939 xdchange_t *cscr = NULL, *xch; 940char*rchg1 = xe->xdf1.rchg, *rchg2 = xe->xdf2.rchg; 941long i1, i2, l1, l2; 942 943/* 944 * Trivial. Collects "groups" of changes and creates an edit script. 945 */ 946for(i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >=0|| i2 >=0; i1--, i2--) 947if(rchg1[i1 -1] || rchg2[i2 -1]) { 948for(l1 = i1; rchg1[i1 -1]; i1--); 949for(l2 = i2; rchg2[i2 -1]; i2--); 950 951if(!(xch =xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) { 952xdl_free_script(cscr); 953return-1; 954} 955 cscr = xch; 956} 957 958*xscr = cscr; 959 960return0; 961} 962 963 964voidxdl_free_script(xdchange_t *xscr) { 965 xdchange_t *xch; 966 967while((xch = xscr) != NULL) { 968 xscr = xscr->next; 969xdl_free(xch); 970} 971} 972 973static intxdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, 974 xdemitconf_t const*xecfg) 975{ 976 xdchange_t *xch, *xche; 977 978for(xch = xscr; xch; xch = xche->next) { 979 xche =xdl_get_hunk(&xch, xecfg); 980if(!xch) 981break; 982if(xecfg->hunk_func(xch->i1, xche->i1 + xche->chg1 - xch->i1, 983 xch->i2, xche->i2 + xche->chg2 - xch->i2, 984 ecb->priv) <0) 985return-1; 986} 987return0; 988} 989 990static voidxdl_mark_ignorable(xdchange_t *xscr, xdfenv_t *xe,long flags) 991{ 992 xdchange_t *xch; 993 994for(xch = xscr; xch; xch = xch->next) { 995int ignore =1; 996 xrecord_t **rec; 997long i; 998 999 rec = &xe->xdf1.recs[xch->i1];1000for(i =0; i < xch->chg1 && ignore; i++)1001 ignore =xdl_blankline(rec[i]->ptr, rec[i]->size, flags);10021003 rec = &xe->xdf2.recs[xch->i2];1004for(i =0; i < xch->chg2 && ignore; i++)1005 ignore =xdl_blankline(rec[i]->ptr, rec[i]->size, flags);10061007 xch->ignore = ignore;1008}1009}10101011intxdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const*xpp,1012 xdemitconf_t const*xecfg, xdemitcb_t *ecb) {1013 xdchange_t *xscr;1014 xdfenv_t xe;1015 emit_func_t ef = xecfg->hunk_func ? xdl_call_hunk_func : xdl_emit_diff;10161017if(xdl_do_diff(mf1, mf2, xpp, &xe) <0) {10181019return-1;1020}1021if(xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) <0||1022xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) <0||1023xdl_build_script(&xe, &xscr) <0) {10241025xdl_free_env(&xe);1026return-1;1027}1028if(xscr) {1029if(xpp->flags & XDF_IGNORE_BLANK_LINES)1030xdl_mark_ignorable(xscr, &xe, xpp->flags);10311032if(ef(&xe, xscr, ecb, xecfg) <0) {10331034xdl_free_script(xscr);1035xdl_free_env(&xe);1036return-1;1037}1038xdl_free_script(xscr);1039}1040xdl_free_env(&xe);10411042return0;1043}