b2evolution PHP Cross Reference Blogging Systems

Source: /inc/_ext/phpsvnclient/ext/Diff/Diff/Engine/native.php - 441 lines - 16031 bytes - Summary - Text - Print

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

title

Description

title

Description

title

Description

title

title

Body