mp-wp_genesis           1 <?php
mp-wp_genesis           2 /**
mp-wp_genesis           3  * $Horde: framework/Text_Diff/Diff/Engine/native.php,v 1.10 2008/01/04 10:27:53 jan Exp $
mp-wp_genesis           4  *
mp-wp_genesis           5  * Class used internally by Text_Diff to actually compute the diffs. This
mp-wp_genesis           6  * class is implemented using native PHP code.
mp-wp_genesis           7  *
mp-wp_genesis           8  * The algorithm used here is mostly lifted from the perl module
mp-wp_genesis           9  * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
mp-wp_genesis          10  * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
mp-wp_genesis          11  *
mp-wp_genesis          12  * More ideas are taken from: http://www.ics.uci.edu/~eppstein/161/960229.html
mp-wp_genesis          13  *
mp-wp_genesis          14  * Some ideas (and a bit of code) are taken from analyze.c, of GNU
mp-wp_genesis          15  * diffutils-2.7, which can be found at:
mp-wp_genesis          16  * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
mp-wp_genesis          17  *
mp-wp_genesis          18  * Some ideas (subdivision by NCHUNKS > 2, and some optimizations) are from
mp-wp_genesis          19  * Geoffrey T. Dairiki <dairiki@dairiki.org>. The original PHP version of this
mp-wp_genesis          20  * code was written by him, and is used/adapted with his permission.
mp-wp_genesis          21  *
mp-wp_genesis          22  * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
mp-wp_genesis          23  *
mp-wp_genesis          24  * See the enclosed file COPYING for license information (LGPL). If you did
mp-wp_genesis          25  * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
mp-wp_genesis          26  *
mp-wp_genesis          27  * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
mp-wp_genesis          28  * @package Text_Diff
mp-wp_genesis          29  */
mp-wp_genesis          30 class Text_Diff_Engine_native {
mp-wp_genesis          31 
mp-wp_genesis          32     function diff($from_lines, $to_lines)
mp-wp_genesis          33     {
mp-wp_genesis          34         array_walk($from_lines, array('Text_Diff', 'trimNewlines'));
mp-wp_genesis          35         array_walk($to_lines, array('Text_Diff', 'trimNewlines'));
mp-wp_genesis          36 
mp-wp_genesis          37         $n_from = count($from_lines);
mp-wp_genesis          38         $n_to = count($to_lines);
mp-wp_genesis          39 
mp-wp_genesis          40         $this->xchanged = $this->ychanged = array();
mp-wp_genesis          41         $this->xv = $this->yv = array();
mp-wp_genesis          42         $this->xind = $this->yind = array();
mp-wp_genesis          43         unset($this->seq);
mp-wp_genesis          44         unset($this->in_seq);
mp-wp_genesis          45         unset($this->lcs);
mp-wp_genesis          46 
mp-wp_genesis          47         // Skip leading common lines.
mp-wp_genesis          48         for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) {
mp-wp_genesis          49             if ($from_lines[$skip] !== $to_lines[$skip]) {
mp-wp_genesis          50                 break;
mp-wp_genesis          51             }
mp-wp_genesis          52             $this->xchanged[$skip] = $this->ychanged[$skip] = false;
mp-wp_genesis          53         }
mp-wp_genesis          54 
mp-wp_genesis          55         // Skip trailing common lines.
mp-wp_genesis          56         $xi = $n_from; $yi = $n_to;
mp-wp_genesis          57         for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++) {
mp-wp_genesis          58             if ($from_lines[$xi] !== $to_lines[$yi]) {
mp-wp_genesis          59                 break;
mp-wp_genesis          60             }
mp-wp_genesis          61             $this->xchanged[$xi] = $this->ychanged[$yi] = false;
mp-wp_genesis          62         }
mp-wp_genesis          63 
mp-wp_genesis          64         // Ignore lines which do not exist in both files.
mp-wp_genesis          65         for ($xi = $skip; $xi < $n_from - $endskip; $xi++) {
mp-wp_genesis          66             $xhash[$from_lines[$xi]] = 1;
mp-wp_genesis          67         }
mp-wp_genesis          68         for ($yi = $skip; $yi < $n_to - $endskip; $yi++) {
mp-wp_genesis          69             $line = $to_lines[$yi];
mp-wp_genesis          70             if (($this->ychanged[$yi] = empty($xhash[$line]))) {
mp-wp_genesis          71                 continue;
mp-wp_genesis          72             }
mp-wp_genesis          73             $yhash[$line] = 1;
mp-wp_genesis          74             $this->yv[] = $line;
mp-wp_genesis          75             $this->yind[] = $yi;
mp-wp_genesis          76         }
mp-wp_genesis          77         for ($xi = $skip; $xi < $n_from - $endskip; $xi++) {
mp-wp_genesis          78             $line = $from_lines[$xi];
mp-wp_genesis          79             if (($this->xchanged[$xi] = empty($yhash[$line]))) {
mp-wp_genesis          80                 continue;
mp-wp_genesis          81             }
mp-wp_genesis          82             $this->xv[] = $line;
mp-wp_genesis          83             $this->xind[] = $xi;
mp-wp_genesis          84         }
mp-wp_genesis          85 
mp-wp_genesis          86         // Find the LCS.
mp-wp_genesis          87         $this->_compareseq(0, count($this->xv), 0, count($this->yv));
mp-wp_genesis          88 
mp-wp_genesis          89         // Merge edits when possible.
mp-wp_genesis          90         $this->_shiftBoundaries($from_lines, $this->xchanged, $this->ychanged);
mp-wp_genesis          91         $this->_shiftBoundaries($to_lines, $this->ychanged, $this->xchanged);
mp-wp_genesis          92 
mp-wp_genesis          93         // Compute the edit operations.
mp-wp_genesis          94         $edits = array();
mp-wp_genesis          95         $xi = $yi = 0;
mp-wp_genesis          96         while ($xi < $n_from || $yi < $n_to) {
mp-wp_genesis          97             assert($yi < $n_to || $this->xchanged[$xi]);
mp-wp_genesis          98             assert($xi < $n_from || $this->ychanged[$yi]);
mp-wp_genesis          99 
mp-wp_genesis         100             // Skip matching "snake".
mp-wp_genesis         101             $copy = array();
mp-wp_genesis         102             while ($xi < $n_from && $yi < $n_to
mp-wp_genesis         103                    && !$this->xchanged[$xi] && !$this->ychanged[$yi]) {
mp-wp_genesis         104                 $copy[] = $from_lines[$xi++];
mp-wp_genesis         105                 ++$yi;
mp-wp_genesis         106             }
mp-wp_genesis         107             if ($copy) {
mp-wp_genesis         108                 $edits[] = &new Text_Diff_Op_copy($copy);
mp-wp_genesis         109             }
mp-wp_genesis         110 
mp-wp_genesis         111             // Find deletes & adds.
mp-wp_genesis         112             $delete = array();
mp-wp_genesis         113             while ($xi < $n_from && $this->xchanged[$xi]) {
mp-wp_genesis         114                 $delete[] = $from_lines[$xi++];
mp-wp_genesis         115             }
mp-wp_genesis         116 
mp-wp_genesis         117             $add = array();
mp-wp_genesis         118             while ($yi < $n_to && $this->ychanged[$yi]) {
mp-wp_genesis         119                 $add[] = $to_lines[$yi++];
mp-wp_genesis         120             }
mp-wp_genesis         121 
mp-wp_genesis         122             if ($delete && $add) {
mp-wp_genesis         123                 $edits[] = &new Text_Diff_Op_change($delete, $add);
mp-wp_genesis         124             } elseif ($delete) {
mp-wp_genesis         125                 $edits[] = &new Text_Diff_Op_delete($delete);
mp-wp_genesis         126             } elseif ($add) {
mp-wp_genesis         127                 $edits[] = &new Text_Diff_Op_add($add);
mp-wp_genesis         128             }
mp-wp_genesis         129         }
mp-wp_genesis         130 
mp-wp_genesis         131         return $edits;
mp-wp_genesis         132     }
mp-wp_genesis         133 
mp-wp_genesis         134     /**
mp-wp_genesis         135      * Divides the Largest Common Subsequence (LCS) of the sequences (XOFF,
mp-wp_genesis         136      * XLIM) and (YOFF, YLIM) into NCHUNKS approximately equally sized
mp-wp_genesis         137      * segments.
mp-wp_genesis         138      *
mp-wp_genesis         139      * Returns (LCS, PTS).  LCS is the length of the LCS. PTS is an array of
mp-wp_genesis         140      * NCHUNKS+1 (X, Y) indexes giving the diving points between sub
mp-wp_genesis         141      * sequences.  The first sub-sequence is contained in (X0, X1), (Y0, Y1),
mp-wp_genesis         142      * the second in (X1, X2), (Y1, Y2) and so on.  Note that (X0, Y0) ==
mp-wp_genesis         143      * (XOFF, YOFF) and (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
mp-wp_genesis         144      *
mp-wp_genesis         145      * This function assumes that the first lines of the specified portions of
mp-wp_genesis         146      * the two files do not match, and likewise that the last lines do not
mp-wp_genesis         147      * match.  The caller must trim matching lines from the beginning and end
mp-wp_genesis         148      * of the portions it is going to specify.
mp-wp_genesis         149      */
mp-wp_genesis         150     function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks)
mp-wp_genesis         151     {
mp-wp_genesis         152         $flip = false;
mp-wp_genesis         153 
mp-wp_genesis         154         if ($xlim - $xoff > $ylim - $yoff) {
mp-wp_genesis         155             /* Things seems faster (I'm not sure I understand why) when the
mp-wp_genesis         156              * shortest sequence is in X. */
mp-wp_genesis         157             $flip = true;
mp-wp_genesis         158             list ($xoff, $xlim, $yoff, $ylim)
mp-wp_genesis         159                 = array($yoff, $ylim, $xoff, $xlim);
mp-wp_genesis         160         }
mp-wp_genesis         161 
mp-wp_genesis         162         if ($flip) {
mp-wp_genesis         163             for ($i = $ylim - 1; $i >= $yoff; $i--) {
mp-wp_genesis         164                 $ymatches[$this->xv[$i]][] = $i;
mp-wp_genesis         165             }
mp-wp_genesis         166         } else {
mp-wp_genesis         167             for ($i = $ylim - 1; $i >= $yoff; $i--) {
mp-wp_genesis         168                 $ymatches[$this->yv[$i]][] = $i;
mp-wp_genesis         169             }
mp-wp_genesis         170         }
mp-wp_genesis         171 
mp-wp_genesis         172         $this->lcs = 0;
mp-wp_genesis         173         $this->seq[0]= $yoff - 1;
mp-wp_genesis         174         $this->in_seq = array();
mp-wp_genesis         175         $ymids[0] = array();
mp-wp_genesis         176 
mp-wp_genesis         177         $numer = $xlim - $xoff + $nchunks - 1;
mp-wp_genesis         178         $x = $xoff;
mp-wp_genesis         179         for ($chunk = 0; $chunk < $nchunks; $chunk++) {
mp-wp_genesis         180             if ($chunk > 0) {
mp-wp_genesis         181                 for ($i = 0; $i <= $this->lcs; $i++) {
mp-wp_genesis         182                     $ymids[$i][$chunk - 1] = $this->seq[$i];
mp-wp_genesis         183                 }
mp-wp_genesis         184             }
mp-wp_genesis         185 
mp-wp_genesis         186             $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $chunk) / $nchunks);
mp-wp_genesis         187             for (; $x < $x1; $x++) {
mp-wp_genesis         188                 $line = $flip ? $this->yv[$x] : $this->xv[$x];
mp-wp_genesis         189                 if (empty($ymatches[$line])) {
mp-wp_genesis         190                     continue;
mp-wp_genesis         191                 }
mp-wp_genesis         192                 $matches = $ymatches[$line];
mp-wp_genesis         193                 reset($matches);
mp-wp_genesis         194                 while (list(, $y) = each($matches)) {
mp-wp_genesis         195                     if (empty($this->in_seq[$y])) {
mp-wp_genesis         196                         $k = $this->_lcsPos($y);
mp-wp_genesis         197                         assert($k > 0);
mp-wp_genesis         198                         $ymids[$k] = $ymids[$k - 1];
mp-wp_genesis         199                         break;
mp-wp_genesis         200                     }
mp-wp_genesis         201                 }
mp-wp_genesis         202                 while (list(, $y) = each($matches)) {
mp-wp_genesis         203                     if ($y > $this->seq[$k - 1]) {
mp-wp_genesis         204                         assert($y <= $this->seq[$k]);
mp-wp_genesis         205                         /* Optimization: this is a common case: next match is
mp-wp_genesis         206                          * just replacing previous match. */
mp-wp_genesis         207                         $this->in_seq[$this->seq[$k]] = false;
mp-wp_genesis         208                         $this->seq[$k] = $y;
mp-wp_genesis         209                         $this->in_seq[$y] = 1;
mp-wp_genesis         210                     } elseif (empty($this->in_seq[$y])) {
mp-wp_genesis         211                         $k = $this->_lcsPos($y);
mp-wp_genesis         212                         assert($k > 0);
mp-wp_genesis         213                         $ymids[$k] = $ymids[$k - 1];
mp-wp_genesis         214                     }
mp-wp_genesis         215                 }
mp-wp_genesis         216             }
mp-wp_genesis         217         }
mp-wp_genesis         218 
mp-wp_genesis         219         $seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff);
mp-wp_genesis         220         $ymid = $ymids[$this->lcs];
mp-wp_genesis         221         for ($n = 0; $n < $nchunks - 1; $n++) {
mp-wp_genesis         222             $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks);
mp-wp_genesis         223             $y1 = $ymid[$n] + 1;
mp-wp_genesis         224             $seps[] = $flip ? array($y1, $x1) : array($x1, $y1);
mp-wp_genesis         225         }
mp-wp_genesis         226         $seps[] = $flip ? array($ylim, $xlim) : array($xlim, $ylim);
mp-wp_genesis         227 
mp-wp_genesis         228         return array($this->lcs, $seps);
mp-wp_genesis         229     }
mp-wp_genesis         230 
mp-wp_genesis         231     function _lcsPos($ypos)
mp-wp_genesis         232     {
mp-wp_genesis         233         $end = $this->lcs;
mp-wp_genesis         234         if ($end == 0 || $ypos > $this->seq[$end]) {
mp-wp_genesis         235             $this->seq[++$this->lcs] = $ypos;
mp-wp_genesis         236             $this->in_seq[$ypos] = 1;
mp-wp_genesis         237             return $this->lcs;
mp-wp_genesis         238         }
mp-wp_genesis         239 
mp-wp_genesis         240         $beg = 1;
mp-wp_genesis         241         while ($beg < $end) {
mp-wp_genesis         242             $mid = (int)(($beg + $end) / 2);
mp-wp_genesis         243             if ($ypos > $this->seq[$mid]) {
mp-wp_genesis         244                 $beg = $mid + 1;
mp-wp_genesis         245             } else {
mp-wp_genesis         246                 $end = $mid;
mp-wp_genesis         247             }
mp-wp_genesis         248         }
mp-wp_genesis         249 
mp-wp_genesis         250         assert($ypos != $this->seq[$end]);
mp-wp_genesis         251 
mp-wp_genesis         252         $this->in_seq[$this->seq[$end]] = false;
mp-wp_genesis         253         $this->seq[$end] = $ypos;
mp-wp_genesis         254         $this->in_seq[$ypos] = 1;
mp-wp_genesis         255         return $end;
mp-wp_genesis         256     }
mp-wp_genesis         257 
mp-wp_genesis         258     /**
mp-wp_genesis         259      * Finds LCS of two sequences.
mp-wp_genesis         260      *
mp-wp_genesis         261      * The results are recorded in the vectors $this->{x,y}changed[], by
mp-wp_genesis         262      * storing a 1 in the element for each line that is an insertion or
mp-wp_genesis         263      * deletion (ie. is not in the LCS).
mp-wp_genesis         264      *
mp-wp_genesis         265      * The subsequence of file 0 is (XOFF, XLIM) and likewise for file 1.
mp-wp_genesis         266      *
mp-wp_genesis         267      * Note that XLIM, YLIM are exclusive bounds.  All line numbers are
mp-wp_genesis         268      * origin-0 and discarded lines are not counted.
mp-wp_genesis         269      */
mp-wp_genesis         270     function _compareseq ($xoff, $xlim, $yoff, $ylim)
mp-wp_genesis         271     {
mp-wp_genesis         272         /* Slide down the bottom initial diagonal. */
mp-wp_genesis         273         while ($xoff < $xlim && $yoff < $ylim
mp-wp_genesis         274                && $this->xv[$xoff] == $this->yv[$yoff]) {
mp-wp_genesis         275             ++$xoff;
mp-wp_genesis         276             ++$yoff;
mp-wp_genesis         277         }
mp-wp_genesis         278 
mp-wp_genesis         279         /* Slide up the top initial diagonal. */
mp-wp_genesis         280         while ($xlim > $xoff && $ylim > $yoff
mp-wp_genesis         281                && $this->xv[$xlim - 1] == $this->yv[$ylim - 1]) {
mp-wp_genesis         282             --$xlim;
mp-wp_genesis         283             --$ylim;
mp-wp_genesis         284         }
mp-wp_genesis         285 
mp-wp_genesis         286         if ($xoff == $xlim || $yoff == $ylim) {
mp-wp_genesis         287             $lcs = 0;
mp-wp_genesis         288         } else {
mp-wp_genesis         289             /* This is ad hoc but seems to work well.  $nchunks =
mp-wp_genesis         290              * sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5); $nchunks =
mp-wp_genesis         291              * max(2,min(8,(int)$nchunks)); */
mp-wp_genesis         292             $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1;
mp-wp_genesis         293             list($lcs, $seps)
mp-wp_genesis         294                 = $this->_diag($xoff, $xlim, $yoff, $ylim, $nchunks);
mp-wp_genesis         295         }
mp-wp_genesis         296 
mp-wp_genesis         297         if ($lcs == 0) {
mp-wp_genesis         298             /* X and Y sequences have no common subsequence: mark all
mp-wp_genesis         299              * changed. */
mp-wp_genesis         300             while ($yoff < $ylim) {
mp-wp_genesis         301                 $this->ychanged[$this->yind[$yoff++]] = 1;
mp-wp_genesis         302             }
mp-wp_genesis         303             while ($xoff < $xlim) {
mp-wp_genesis         304                 $this->xchanged[$this->xind[$xoff++]] = 1;
mp-wp_genesis         305             }
mp-wp_genesis         306         } else {
mp-wp_genesis         307             /* Use the partitions to split this problem into subproblems. */
mp-wp_genesis         308             reset($seps);
mp-wp_genesis         309             $pt1 = $seps[0];
mp-wp_genesis         310             while ($pt2 = next($seps)) {
mp-wp_genesis         311                 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
mp-wp_genesis         312                 $pt1 = $pt2;
mp-wp_genesis         313             }
mp-wp_genesis         314         }
mp-wp_genesis         315     }
mp-wp_genesis         316 
mp-wp_genesis         317     /**
mp-wp_genesis         318      * Adjusts inserts/deletes of identical lines to join changes as much as
mp-wp_genesis         319      * possible.
mp-wp_genesis         320      *
mp-wp_genesis         321      * We do something when a run of changed lines include a line at one end
mp-wp_genesis         322      * and has an excluded, identical line at the other.  We are free to
mp-wp_genesis         323      * choose which identical line is included.  `compareseq' usually chooses
mp-wp_genesis         324      * the one at the beginning, but usually it is cleaner to consider the
mp-wp_genesis         325      * following identical line to be the "change".
mp-wp_genesis         326      *
mp-wp_genesis         327      * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
mp-wp_genesis         328      */
mp-wp_genesis         329     function _shiftBoundaries($lines, &$changed, $other_changed)
mp-wp_genesis         330     {
mp-wp_genesis         331         $i = 0;
mp-wp_genesis         332         $j = 0;
mp-wp_genesis         333 
mp-wp_genesis         334         assert('count($lines) == count($changed)');
mp-wp_genesis         335         $len = count($lines);
mp-wp_genesis         336         $other_len = count($other_changed);
mp-wp_genesis         337 
mp-wp_genesis         338         while (1) {
mp-wp_genesis         339             /* Scan forward to find the beginning of another run of
mp-wp_genesis         340              * changes. Also keep track of the corresponding point in the
mp-wp_genesis         341              * other file.
mp-wp_genesis         342              *
mp-wp_genesis         343              * Throughout this code, $i and $j are adjusted together so that
mp-wp_genesis         344              * the first $i elements of $changed and the first $j elements of
mp-wp_genesis         345              * $other_changed both contain the same number of zeros (unchanged
mp-wp_genesis         346              * lines).
mp-wp_genesis         347              *
mp-wp_genesis         348              * Furthermore, $j is always kept so that $j == $other_len or
mp-wp_genesis         349              * $other_changed[$j] == false. */
mp-wp_genesis         350             while ($j < $other_len && $other_changed[$j]) {
mp-wp_genesis         351                 $j++;
mp-wp_genesis         352             }
mp-wp_genesis         353 
mp-wp_genesis         354             while ($i < $len && ! $changed[$i]) {
mp-wp_genesis         355                 assert('$j < $other_len && ! $other_changed[$j]');
mp-wp_genesis         356                 $i++; $j++;
mp-wp_genesis         357                 while ($j < $other_len && $other_changed[$j]) {
mp-wp_genesis         358                     $j++;
mp-wp_genesis         359                 }
mp-wp_genesis         360             }
mp-wp_genesis         361 
mp-wp_genesis         362             if ($i == $len) {
mp-wp_genesis         363                 break;
mp-wp_genesis         364             }
mp-wp_genesis         365 
mp-wp_genesis         366             $start = $i;
mp-wp_genesis         367 
mp-wp_genesis         368             /* Find the end of this run of changes. */
mp-wp_genesis         369             while (++$i < $len && $changed[$i]) {
mp-wp_genesis         370                 continue;
mp-wp_genesis         371             }
mp-wp_genesis         372 
mp-wp_genesis         373             do {
mp-wp_genesis         374                 /* Record the length of this run of changes, so that we can
mp-wp_genesis         375                  * later determine whether the run has grown. */
mp-wp_genesis         376                 $runlength = $i - $start;
mp-wp_genesis         377 
mp-wp_genesis         378                 /* Move the changed region back, so long as the previous
mp-wp_genesis         379                  * unchanged line matches the last changed one.  This merges
mp-wp_genesis         380                  * with previous changed regions. */
mp-wp_genesis         381                 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
mp-wp_genesis         382                     $changed[--$start] = 1;
mp-wp_genesis         383                     $changed[--$i] = false;
mp-wp_genesis         384                     while ($start > 0 && $changed[$start - 1]) {
mp-wp_genesis         385                         $start--;
mp-wp_genesis         386                     }
mp-wp_genesis         387                     assert('$j > 0');
mp-wp_genesis         388                     while ($other_changed[--$j]) {
mp-wp_genesis         389                         continue;
mp-wp_genesis         390                     }
mp-wp_genesis         391                     assert('$j >= 0 && !$other_changed[$j]');
mp-wp_genesis         392                 }
mp-wp_genesis         393 
mp-wp_genesis         394                 /* Set CORRESPONDING to the end of the changed run, at the
mp-wp_genesis         395                  * last point where it corresponds to a changed run in the
mp-wp_genesis         396                  * other file. CORRESPONDING == LEN means no such point has
mp-wp_genesis         397                  * been found. */
mp-wp_genesis         398                 $corresponding = $j < $other_len ? $i : $len;
mp-wp_genesis         399 
mp-wp_genesis         400                 /* Move the changed region forward, so long as the first
mp-wp_genesis         401                  * changed line matches the following unchanged one.  This
mp-wp_genesis         402                  * merges with following changed regions.  Do this second, so
mp-wp_genesis         403                  * that if there are no merges, the changed region is moved
mp-wp_genesis         404                  * forward as far as possible. */
mp-wp_genesis         405                 while ($i < $len && $lines[$start] == $lines[$i]) {
mp-wp_genesis         406                     $changed[$start++] = false;
mp-wp_genesis         407                     $changed[$i++] = 1;
mp-wp_genesis         408                     while ($i < $len && $changed[$i]) {
mp-wp_genesis         409                         $i++;
mp-wp_genesis         410                     }
mp-wp_genesis         411 
mp-wp_genesis         412                     assert('$j < $other_len && ! $other_changed[$j]');
mp-wp_genesis         413                     $j++;
mp-wp_genesis         414                     if ($j < $other_len && $other_changed[$j]) {
mp-wp_genesis         415                         $corresponding = $i;
mp-wp_genesis         416                         while ($j < $other_len && $other_changed[$j]) {
mp-wp_genesis         417                             $j++;
mp-wp_genesis         418                         }
mp-wp_genesis         419                     }
mp-wp_genesis         420                 }
mp-wp_genesis         421             } while ($runlength != $i - $start);
mp-wp_genesis         422 
mp-wp_genesis         423             /* If possible, move the fully-merged run of changes back to a
mp-wp_genesis         424              * corresponding run in the other file. */
mp-wp_genesis         425             while ($corresponding < $i) {
mp-wp_genesis         426                 $changed[--$start] = 1;
mp-wp_genesis         427                 $changed[--$i] = 0;
mp-wp_genesis         428                 assert('$j > 0');
mp-wp_genesis         429                 while ($other_changed[--$j]) {
mp-wp_genesis         430                     continue;
mp-wp_genesis         431                 }
mp-wp_genesis         432                 assert('$j >= 0 && !$other_changed[$j]');
mp-wp_genesis         433             }
mp-wp_genesis         434         }
mp-wp_genesis         435     }
mp-wp_genesis         436 
mp-wp_genesis         437 }