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