[ Index ]

PHP Cross Reference of MyBB 1.8.38

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


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref