[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/3rdparty/diff/Diff/Engine/ -> Native.php (source)

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


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1