Text_Diff
[ class tree: Text_Diff ] [ index: Text_Diff ] [ all elements ]

Source for file Diff.php

Documentation is available at Diff.php

  1. <?php
  2. /**
  3.  * Text_Diff
  4.  *
  5.  * General API for generating and formatting diffs - the differences
  6.  * between two sequences of strings.
  7.  *
  8.  * The PHP diff code used in this package was originally written by
  9.  * Geoffrey T. Dairiki and is used with his permission.
  10.  *
  11.  * $Horde: framework/Text_Diff/Diff.php,v 1.5 2004/03/19 18:53:11 chuck Exp $
  12.  *
  13.  * @package Text_Diff
  14.  * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
  15.  */
  16. class Text_Diff {
  17.  
  18.     /**
  19.      * Array of changes.
  20.      *
  21.      * @var array $_edits 
  22.      */
  23.     var $_edits;
  24.  
  25.     /**
  26.      * Computes diff between sequences of strings.
  27.      *
  28.      * @param $from_lines array An array of strings.
  29.      *         (Typically these are lines from a file.)
  30.      * @param $to_lines array An array of strings.
  31.      */
  32.     function Text_Diff($from_lines$to_lines)
  33.     {
  34.         array_walk($from_linesarray($this'_trimNewlines'));
  35.         array_walk($to_linesarray($this'_trimNewlines'));
  36.  
  37.         if (extension_loaded('xdiff')) {
  38.             $engine &new Text_Diff_Engine_xdiff();
  39.         else {
  40.             $engine &new Text_Diff_Engine_native();
  41.         }
  42.  
  43.         $this->_edits $engine->diff($from_lines$to_lines);
  44.     }
  45.  
  46.     /**
  47.      * Return the array of differences.
  48.      */
  49.     function getDiff()
  50.     {
  51.         return $this->_edits;
  52.     }
  53.  
  54.     /**
  55.      * Compute reversed diff.
  56.      *
  57.      * SYNOPSIS:
  58.      *
  59.      * $diff = &new Text_Diff($lines1, $lines2);
  60.      * $rev = $diff->reverse();
  61.      *
  62.      * @return object  Diff object representing the inverse of the
  63.      *                  original diff. Note that we purposely don't return
  64.      *                  a reference here, since this essentially is a
  65.      *                  clone() method.
  66.      */
  67.     function reverse()
  68.     {
  69.         $rev $this;
  70.         $rev->_edits = array();
  71.         foreach ($this->_edits as $edit{
  72.             $rev->_edits[$edit->reverse();
  73.         }
  74.         return $rev;
  75.     }
  76.  
  77.     /**
  78.      * Check for empty diff.
  79.      *
  80.      * @return boolean  True iff two sequences were identical.
  81.      */
  82.     function isEmpty()
  83.     {
  84.         foreach ($this->_edits as $edit{
  85.             if (!is_a($edit'Text_Diff_Op_copy')) {
  86.                 return false;
  87.             }
  88.         }
  89.         return true;
  90.     }
  91.  
  92.     /**
  93.      * Compute the length of the Longest Common Subsequence (LCS).
  94.      *
  95.      * This is mostly for diagnostic purposed.
  96.      *
  97.      * @return int The length of the LCS.
  98.      */
  99.     function lcs()
  100.     {
  101.         $lcs = 0;
  102.         foreach ($this->_edits as $edit{
  103.             if (is_a($edit'Text_Diff_Op_copy')) {
  104.                 $lcs += count($edit->orig);
  105.             }
  106.         }
  107.         return $lcs;
  108.     }
  109.  
  110.     /**
  111.      * Get the original set of lines.
  112.      *
  113.      * This reconstructs the $from_lines parameter passed to the
  114.      * constructor.
  115.      *
  116.      * @return array The original sequence of strings.
  117.      */
  118.     function orig()
  119.     {
  120.         $lines = array();
  121.  
  122.         foreach ($this->_edits as $edit{
  123.             if ($edit->orig{
  124.                 array_splice($linescount($lines)0$edit->orig);
  125.             }
  126.         }
  127.         return $lines;
  128.     }
  129.  
  130.     /**
  131.      * Get the final set of lines.
  132.      *
  133.      * This reconstructs the $to_lines parameter passed to the
  134.      * constructor.
  135.      *
  136.      * @return array The sequence of strings.
  137.      */
  138.     function final()
  139.     {
  140.         $lines = array();
  141.  
  142.         foreach ($this->_edits as $edit{
  143.             if ($edit->final{
  144.                 array_splice($linescount($lines)0$edit->final);
  145.             }
  146.         }
  147.         return $lines;
  148.     }
  149.  
  150.     /**
  151.      * Remove trailing newlines from a line of text. This is meant to
  152.      * be used with array_walk().
  153.      *
  154.      * @param string  &$line  The line to trim.
  155.      * @param integer $key    The index of the line in the array. Not used.
  156.      */
  157.     function _trimNewlines(&$line$key)
  158.     {
  159.         $line str_replace(array("\n""\r""\r\n")''$line);
  160.     }
  161.  
  162.     /**
  163.      * Check a diff for validity.
  164.      *
  165.      * This is here only for debugging purposes.
  166.      */
  167.     function _check($from_lines$to_lines)
  168.     {
  169.         if (serialize($from_lines!= serialize($this->orig())) {
  170.             trigger_error("Reconstructed original doesn't match"E_USER_ERROR);
  171.         }
  172.         if (serialize($to_lines!= serialize($this->final())) {
  173.             trigger_error("Reconstructed final doesn't match"E_USER_ERROR);
  174.         }
  175.  
  176.         $rev $this->reverse();
  177.         if (serialize($to_lines!= serialize($rev->orig())) {
  178.             trigger_error("Reversed original doesn't match"E_USER_ERROR);
  179.         }
  180.         if (serialize($from_lines!= serialize($rev->final())) {
  181.             trigger_error("Reversed final doesn't match"E_USER_ERROR);
  182.         }
  183.  
  184.         $prevtype = null;
  185.         foreach ($this->_edits as $edit{
  186.             if ($prevtype == get_class($edit)) {
  187.                 trigger_error("Edit sequence is non-optimal"E_USER_ERROR);
  188.             }
  189.             $prevtype get_class($edit);
  190.         }
  191.  
  192.         return true;
  193.     }
  194.  
  195. }
  196.  
  197. /**
  198.  * $Horde: framework/Text_Diff/Diff.php,v 1.5 2004/03/19 18:53:11 chuck Exp $
  199.  *
  200.  * @package Text_Diff
  201.  * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
  202.  */
  203. class Text_MappedDiff extends Text_Diff {
  204.  
  205.     /**
  206.      * Computes a diff between sequences of strings.
  207.      *
  208.      * This can be used to compute things like case-insensitve diffs,
  209.      * or diffs which ignore changes in white-space.
  210.      *
  211.      * @param $from_lines        array  An array of strings.
  212.      * @param $to_lines          array  An array of strings.
  213.      * @param $mapped_from_lines array  This array should
  214.      *      have the same size number of elements as $from_lines.
  215.      *      The elements in $mapped_from_lines and
  216.      *      $mapped_to_lines are what is actually compared
  217.      *      when computing the diff.
  218.      * @param $mapped_to_lines   array  This array should
  219.      *      have the same number of elements as $to_lines.
  220.      */
  221.     function Text_MappedDiff($from_lines$to_lines,
  222.                              $mapped_from_lines$mapped_to_lines)
  223.     {
  224.         assert(count($from_lines== count($mapped_from_lines));
  225.         assert(count($to_lines== count($mapped_to_lines));
  226.  
  227.         parent::Text_Diff($mapped_from_lines$mapped_to_lines);
  228.  
  229.         $xi $yi = 0;
  230.         for ($i = 0; $i count($this->edits)$i++{
  231.             $orig &$this->edits[$i]->orig;
  232.             if (is_array($orig)) {
  233.                 $orig array_slice($from_lines$xicount($orig));
  234.                 $xi += count($orig);
  235.             }
  236.  
  237.             $final &$this->edits[$i]->final;
  238.             if (is_array($final)) {
  239.                 $final array_slice($to_lines$yicount($final));
  240.                 $yi += count($final);
  241.             }
  242.         }
  243.     }
  244.  
  245. }
  246.  
  247. /**
  248.  * Class used internally by Diff to actually compute the diffs.  This class
  249.  * uses the xdiff PECL package (http://pecl.php.net/package/xdiff) to compute
  250.  * the differences between the two input arrays.
  251.  *
  252.  * @author  Jon Parise <jon@horde.org>
  253.  * @package Text_Diff
  254.  * @access  private
  255.  */
  256. class Text_Diff_Engine_xdiff {
  257.  
  258.     function diff($from_lines$to_lines)
  259.     {
  260.         /* Convert the two input arrays into strings for xdiff processing. */
  261.         $from_string implode("\n"$from_lines);
  262.         $to_string implode("\n"$to_lines);
  263.  
  264.         /* Diff the two strings and convert the result to an array. */
  265.         $diff = xdiff_string_diff($from_string$to_stringcount($to_lines));
  266.         $diff explode("\n"$diff);
  267.  
  268.         /* Walk through the diff one line at a time.  We build the
  269.          * $edits array of diff operations by reading the first
  270.          * character of the xdiff output (which is in the "unified
  271.          * diff" format).
  272.          *
  273.          * Note that we don't have enough information to detect
  274.          * "changed" lines using this approach, so we can't add
  275.          * Text_Diff_Op_changed instances to the $edits array.  The
  276.          * result is still perfectly valid, albeit a little less
  277.          * descriptive and efficient. */
  278.         $edits = array();
  279.         foreach ($diff as $line{
  280.             switch ($line[0]{
  281.             case ' ':
  282.                 $edits[&new Text_Diff_Op_copy(array(substr($line1)));
  283.                 break;
  284.  
  285.             case '+':
  286.                 $edits[&new Text_Diff_Op_add(array(substr($line1)));
  287.                 break;
  288.  
  289.             case '-':
  290.                 $edits[&new Text_Diff_Op_delete(array(substr($line1)));
  291.                 break;
  292.             }
  293.         }
  294.  
  295.         return $edits;
  296.     }
  297.  
  298. }
  299.  
  300. /**
  301.  * Class used internally by Diff to actually compute the diffs.  This class
  302.  * is implemented using native PHP code.
  303.  *
  304.  * The algorithm used here is mostly lifted from the perl module
  305.  * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
  306.  * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
  307.  *
  308.  * More ideas are taken from:
  309.  * http://www.ics.uci.edu/~eppstein/161/960229.html
  310.  *
  311.  * Some ideas (and a bit of code) are from from analyze.c, from GNU
  312.  * diffutils-2.7, which can be found at:
  313.  * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
  314.  *
  315.  * Some ideas (subdivision by NCHUNKS > 2, and some optimizations) are
  316.  * from Geoffrey T. Dairiki <dairiki@dairiki.org>. The original PHP
  317.  * version of this code was writting by him, and is used/adapted with
  318.  * his permission.
  319.  *
  320.  * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
  321.  * @package Text_Diff
  322.  * @access  private
  323.  */
  324. class Text_Diff_Engine_native {
  325.  
  326.     function diff($from_lines$to_lines)
  327.     {
  328.         $n_from count($from_lines);
  329.         $n_to count($to_lines);
  330.  
  331.         $this->xchanged $this->ychanged = array();
  332.         $this->xv $this->yv = array();
  333.         $this->xind $this->yind = array();
  334.         unset($this->seq);
  335.         unset($this->in_seq);
  336.         unset($this->lcs);
  337.  
  338.         // Skip leading common lines.
  339.         for ($skip = 0; $skip $n_from && $skip $n_to$skip++{
  340.             if ($from_lines[$skip!= $to_lines[$skip]{
  341.                 break;
  342.             }
  343.             $this->xchanged[$skip$this->ychanged[$skip= false;
  344.         }
  345.  
  346.         // Skip trailing common lines.
  347.         $xi $n_from$yi $n_to;
  348.         for ($endskip = 0; --$xi $skip && --$yi $skip$endskip++{
  349.             if ($from_lines[$xi!= $to_lines[$yi]{
  350.                 break;
  351.             }
  352.             $this->xchanged[$xi$this->ychanged[$yi= false;
  353.         }
  354.  
  355.         // Ignore lines which do not exist in both files.
  356.         for ($xi $skip$xi $n_from $endskip$xi++{
  357.             $xhash[$from_lines[$xi]] = 1;
  358.         }
  359.         for ($yi $skip$yi $n_to $endskip$yi++{
  360.             $line $to_lines[$yi];
  361.             if (($this->ychanged[$yi= empty($xhash[$line]))) {
  362.                 continue;
  363.             }
  364.             $yhash[$line= 1;
  365.             $this->yv[$line;
  366.             $this->yind[$yi;
  367.         }
  368.         for ($xi $skip$xi $n_from $endskip$xi++{
  369.             $line $from_lines[$xi];
  370.             if (($this->xchanged[$xi= empty($yhash[$line]))) {
  371.                 continue;
  372.             }
  373.             $this->xv[$line;
  374.             $this->xind[$xi;
  375.         }
  376.  
  377.         // Find the LCS.
  378.         $this->_compareseq(0count($this->xv)0count($this->yv));
  379.  
  380.         // Merge edits when possible.
  381.         $this->_shiftBoundaries($from_lines$this->xchanged$this->ychanged);
  382.         $this->_shiftBoundaries($to_lines$this->ychanged$this->xchanged);
  383.  
  384.         // Compute the edit operations.
  385.         $edits = array();
  386.         $xi $yi = 0;
  387.         while ($xi $n_from || $yi $n_to{
  388.             assert($yi $n_to || $this->xchanged[$xi]);
  389.             assert($xi $n_from || $this->ychanged[$yi]);
  390.  
  391.             // Skip matching "snake".
  392.             $copy = array();
  393.             while ($xi $n_from && $yi $n_to
  394.                    && !$this->xchanged[$xi&& !$this->ychanged[$yi]{
  395.                 $copy[$from_lines[$xi++];
  396.                 ++$yi;
  397.             }
  398.             if ($copy{
  399.                 $edits[&new Text_Diff_Op_copy($copy);
  400.             }
  401.  
  402.             // Find deletes & adds.
  403.             $delete = array();
  404.             while ($xi $n_from && $this->xchanged[$xi]{
  405.                 $delete[$from_lines[$xi++];
  406.             }
  407.  
  408.             $add = array();
  409.             while ($yi $n_to && $this->ychanged[$yi]{
  410.                 $add[$to_lines[$yi++];
  411.             }
  412.  
  413.             if ($delete && $add{
  414.                 $edits[&new Text_Diff_Op_change($delete$add);
  415.             elseif ($delete{
  416.                 $edits[&new Text_Diff_Op_delete($delete);
  417.             elseif ($add{
  418.                 $edits[&new Text_Diff_Op_add($add);
  419.             }
  420.         }
  421.         return $edits;
  422.     }
  423.  
  424.     /**
  425.      * Divide the Largest Common Subsequence (LCS) of the sequences
  426.      * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately
  427.      * equally sized segments.
  428.      *
  429.      * Returns (LCS, PTS).  LCS is the length of the LCS. PTS is an
  430.      * array of NCHUNKS+1 (X, Y) indexes giving the diving points
  431.      * between sub sequences.  The first sub-sequence is contained in
  432.      * [X0, X1), [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on.
  433.      * Note that (X0, Y0) == (XOFF, YOFF) and (X[NCHUNKS], Y[NCHUNKS])
  434.      * == (XLIM, YLIM).
  435.      *
  436.      * This function assumes that the first lines of the specified
  437.      * portions of the two files do not match, and likewise that the
  438.      * last lines do not match.  The caller must trim matching lines
  439.      * from the beginning and end of the portions it is going to
  440.      * specify.
  441.      */
  442.     function _diag ($xoff$xlim$yoff$ylim$nchunks)
  443.     {
  444.         $flip = false;
  445.  
  446.         if ($xlim $xoff $ylim $yoff{
  447.             // Things seems faster (I'm not sure I understand why)
  448.             // when the shortest sequence is in X.
  449.             $flip = true;
  450.             list ($xoff$xlim$yoff$ylim)
  451.                 = array($yoff$ylim$xoff$xlim);
  452.         }
  453.  
  454.         if ($flip{
  455.             for ($i $ylim - 1; $i >= $yoff$i--{
  456.                 $ymatches[$this->xv[$i]][$i;
  457.             }
  458.         else {
  459.             for ($i $ylim - 1; $i >= $yoff$i--{
  460.                 $ymatches[$this->yv[$i]][$i;
  461.             }
  462.         }
  463.  
  464.         $this->lcs = 0;
  465.         $this->seq[0]$yoff - 1;
  466.         $this->in_seq = array();
  467.         $ymids[0= array();
  468.  
  469.         $numer $xlim $xoff $nchunks - 1;
  470.         $x $xoff;
  471.         for ($chunk = 0; $chunk $nchunks$chunk++{
  472.             if ($chunk > 0{
  473.                 for ($i = 0; $i <= $this->lcs$i++{
  474.                     $ymids[$i][$chunk-1$this->seq[$i];
  475.                 }
  476.             }
  477.  
  478.             $x1 $xoff + (int)(($numer ($xlim-$xoff)*$chunk$nchunks);
  479.             for ($x $x1$x++{
  480.                 $line $flip $this->yv[$x$this->xv[$x];
  481.                 if (empty($ymatches[$line])) {
  482.                     continue;
  483.                 }
  484.                 $matches $ymatches[$line];
  485.                 foreach ($matches as $y{
  486.                     if (empty($this->in_seq[$y])) {
  487.                         $k $this->_lcsPos($y);
  488.                         assert($k > 0);
  489.                         $ymids[$k$ymids[$k-1];
  490.                         break;
  491.                     }
  492.                 }
  493.  
  494.                 while (list($junk$yeach($matches)) {
  495.                     if ($y $this->seq[$k - 1]{
  496.                         assert($y $this->seq[$k]);
  497.                         // Optimization: this is a common case: next
  498.                         // match is just replacing previous match.
  499.                         $this->in_seq[$this->seq[$k]] = false;
  500.                         $this->seq[$k$y;
  501.                         $this->in_seq[$y= 1;
  502.                     elseif (empty($this->in_seq[$y])) {
  503.                         $k $this->_lcsPos($y);
  504.                         assert($k > 0);
  505.                         $ymids[$k$ymids[$k-1];
  506.                     }
  507.                 }
  508.             }
  509.         }
  510.  
  511.         $seps[$flip ? array($yoff$xoff: array($xoff$yoff);
  512.         $ymid $ymids[$this->lcs];
  513.         for ($n = 0; $n $nchunks - 1; $n++{
  514.             $x1 $xoff + (int)(($numer ($xlim $xoff$n$nchunks);
  515.             $y1 $ymid[$n+ 1;
  516.             $seps[$flip ? array($y1$x1: array($x1$y1);
  517.         }
  518.         $seps[$flip ? array($ylim$xlim: array($xlim$ylim);
  519.  
  520.         return array($this->lcs$seps);
  521.     }
  522.  
  523.     function _lcsPos($ypos)
  524.     {
  525.         $end $this->lcs;
  526.         if ($end == 0 || $ypos $this->seq[$end]{
  527.             $this->seq[++$this->lcs$ypos;
  528.             $this->in_seq[$ypos= 1;
  529.             return $this->lcs;
  530.         }
  531.  
  532.         $beg = 1;
  533.         while ($beg $end{
  534.             $mid = (int)(($beg $end/ 2);
  535.             if ($ypos $this->seq[$mid]{
  536.                 $beg $mid + 1;
  537.             else {
  538.                 $end $mid;
  539.             }
  540.         }
  541.  
  542.         assert($ypos != $this->seq[$end]);
  543.  
  544.         $this->in_seq[$this->seq[$end]] = false;
  545.         $this->seq[$end$ypos;
  546.         $this->in_seq[$ypos= 1;
  547.         return $end;
  548.     }
  549.  
  550.     /**
  551.      * Find LCS of two sequences.
  552.      *
  553.      * The results are recorded in the vectors $this->{x,y}changed[],
  554.      * by storing a 1 in the element for each line that is an
  555.      * insertion or deletion (ie. is not in the LCS).
  556.      *
  557.      * The subsequence of file 0 is [XOFF, XLIM) and likewise for file
  558.      * 1.
  559.      *
  560.      * Note that XLIM, YLIM are exclusive bounds.  All line numbers
  561.      * are origin-0 and discarded lines are not counted.
  562.      */
  563.     function _compareseq ($xoff$xlim$yoff$ylim)
  564.     {
  565.         // Slide down the bottom initial diagonal.
  566.         while ($xoff $xlim && $yoff $ylim
  567.                && $this->xv[$xoff== $this->yv[$yoff]{
  568.             ++$xoff;
  569.             ++$yoff;
  570.         }
  571.  
  572.         // Slide up the top initial diagonal.
  573.         while ($xlim $xoff && $ylim $yoff
  574.                && $this->xv[$xlim - 1== $this->yv[$ylim - 1]{
  575.             --$xlim;
  576.             --$ylim;
  577.         }
  578.  
  579.         if ($xoff == $xlim || $yoff == $ylim{
  580.             $lcs = 0;
  581.         else {
  582.             // This is ad hoc but seems to work well.  $nchunks =
  583.             // sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5); $nchunks
  584.             // = max(2,min(8,(int)$nchunks));
  585.             $nchunks min(7$xlim $xoff$ylim $yoff+ 1;
  586.             list($lcs$seps)
  587.                 = $this->_diag($xoff$xlim$yoff$ylim$nchunks);
  588.         }
  589.  
  590.         if ($lcs == 0{
  591.             // X and Y sequences have no common subsequence: mark all
  592.             // changed.
  593.             while ($yoff $ylim{
  594.                 $this->ychanged[$this->yind[$yoff++]] = 1;
  595.             }
  596.             while ($xoff $xlim{
  597.                 $this->xchanged[$this->xind[$xoff++]] = 1;
  598.             }
  599.         else {
  600.             // Use the partitions to split this problem into
  601.             // subproblems.
  602.             reset($seps);
  603.             $pt1 $seps[0];
  604.             while ($pt2 next($seps)) {
  605.                 $this->_compareseq ($pt1[0]$pt2[0]$pt1[1]$pt2[1]);
  606.                 $pt1 $pt2;
  607.             }
  608.         }
  609.     }
  610.  
  611.     /**
  612.      * Adjust inserts/deletes of identical lines to join changes as
  613.      * much as possible.
  614.      *
  615.      * We do something when a run of changed lines include a line at
  616.      * one end and has an excluded, identical line at the other.  We
  617.      * are free to choose which identical line is included.
  618.      * `compareseq' usually chooses the one at the beginning, but
  619.      * usually it is cleaner to consider the following identical line
  620.      * to be the "change".
  621.      *
  622.      * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
  623.      */
  624.     function _shiftBoundaries($lines&$changed$other_changed)
  625.     {
  626.         $i = 0;
  627.         $j = 0;
  628.  
  629.         assert('count($lines) == count($changed)');
  630.         $len count($lines);
  631.         $other_len count($other_changed);
  632.  
  633.         while (1{
  634.             /* Scan forward to find the beginning of another run of
  635.              * changes. Also keep track of the corresponding point in
  636.              * the other file.
  637.              *
  638.              * Throughout this code, $i and $j are adjusted together
  639.              * so that the first $i elements of $changed and the first
  640.              * $j elements of $other_changed both contain the same
  641.              * number of zeros (unchanged lines).
  642.              *
  643.              * Furthermore, $j is always kept so that $j == $other_len
  644.              * or $other_changed[$j] == false. */
  645.             while ($j $other_len && $other_changed[$j]{
  646.                 $j++;
  647.             }
  648.  
  649.             while ($i $len && $changed[$i]{
  650.                 assert('$j < $other_len && ! $other_changed[$j]');
  651.                 $i++; $j++;
  652.                 while ($j $other_len && $other_changed[$j]{
  653.                     $j++;
  654.                 }
  655.             }
  656.  
  657.             if ($i == $len{
  658.                 break;
  659.             }
  660.  
  661.             $start $i;
  662.  
  663.             // Find the end of this run of changes.
  664.             while (++$i $len && $changed[$i]{
  665.                 continue;
  666.             }
  667.  
  668.             do {
  669.                 /* Record the length of this run of changes, so that
  670.                  * we can later determine whether the run has
  671.                  * grown. */
  672.                 $runlength $i $start;
  673.  
  674.                 /* Move the changed region back, so long as the
  675.                  * previous unchanged line matches the last changed
  676.                  * one.  This merges with previous changed regions. */
  677.                 while ($start > 0 && $lines[$start - 1== $lines[$i - 1]{
  678.                     $changed[--$start= 1;
  679.                     $changed[--$i= false;
  680.                     while ($start > 0 && $changed[$start - 1]{
  681.                         $start--;
  682.                     }
  683.                     assert('$j > 0');
  684.                     while ($other_changed[--$j]{
  685.                         continue;
  686.                     }
  687.                     assert('$j >= 0 && !$other_changed[$j]');
  688.                 }
  689.  
  690.                 /* Set CORRESPONDING to the end of the changed run, at
  691.                  * the last point where it corresponds to a changed
  692.                  * run in the other file. CORRESPONDING == LEN means
  693.                  * no such point has been found. */
  694.                 $corresponding $j $other_len $i $len;
  695.  
  696.                 /* Move the changed region forward, so long as the
  697.                  * first changed line matches the following unchanged
  698.                  * one.  This merges with following changed regions.
  699.                  * Do this second, so that if there are no merges, the
  700.                  * changed region is moved forward as far as
  701.                  * possible. */
  702.                 while ($i $len && $lines[$start== $lines[$i]{
  703.                     $changed[$start++= false;
  704.                     $changed[$i++= 1;
  705.                     while ($i $len && $changed[$i]{
  706.                         $i++;
  707.                     }
  708.  
  709.                     assert('$j < $other_len && ! $other_changed[$j]');
  710.                     $j++;
  711.                     if ($j $other_len && $other_changed[$j]{
  712.                         $corresponding $i;
  713.                         while ($j $other_len && $other_changed[$j]{
  714.                             $j++;
  715.                         }
  716.                     }
  717.                 }
  718.             while ($runlength != $i $start);
  719.  
  720.             /* If possible, move the fully-merged run of changes back
  721.              * to a corresponding run in the other file. */
  722.             while ($corresponding $i{
  723.                 $changed[--$start= 1;
  724.                 $changed[--$i= 0;
  725.                 assert('$j > 0');
  726.                 while ($other_changed[--$j]{
  727.                     continue;
  728.                 }
  729.                 assert('$j >= 0 && !$other_changed[$j]');
  730.             }
  731.         }
  732.     }
  733.  
  734. }
  735.  
  736. /**
  737.  * @package Text_Diff
  738.  * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
  739.  * @access  private
  740.  */
  741. class Text_Diff_Op {
  742.  
  743.     var $orig;
  744.     var $final;
  745.  
  746.     function reverse()
  747.     {
  748.         trigger_error('Abstract method'E_USER_ERROR);
  749.     }
  750.  
  751.     function norig()
  752.     {
  753.         return $this->orig count($this->orig: 0;
  754.     }
  755.  
  756.     function nfinal()
  757.     {
  758.         return $this->final count($this->final: 0;
  759.     }
  760.  
  761. }
  762.  
  763. /**
  764.  * @package Text_Diff
  765.  * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
  766.  * @access  private
  767.  */
  768. class Text_Diff_Op_copy extends Text_Diff_Op {
  769.  
  770.     function Text_Diff_Op_copy($orig$final = false)
  771.     {
  772.         if (!is_array($final)) {
  773.             $final $orig;
  774.         }
  775.         $this->orig $orig;
  776.         $this->final $final;
  777.     }
  778.  
  779.     function &reverse()
  780.     {
  781.         return $diff = new Text_Diff_Op_copy($this->final$this->orig);
  782.     }
  783.  
  784. }
  785.  
  786. /**
  787.  * @package Text_Diff
  788.  * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
  789.  * @access  private
  790.  */
  791. class Text_Diff_Op_delete extends Text_Diff_Op {
  792.  
  793.     function Text_Diff_Op_delete($lines)
  794.     {
  795.         $this->orig $lines;
  796.         $this->final = false;
  797.     }
  798.  
  799.     function &reverse()
  800.     {
  801.         return $diff = new Text_Diff_Op_add($this->orig);
  802.     }
  803.  
  804. }
  805.  
  806. /**
  807.  * @package Text_Diff
  808.  * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
  809.  * @access  private
  810.  */
  811. class Text_Diff_Op_add extends Text_Diff_Op {
  812.  
  813.     function Text_Diff_Op_add($lines)
  814.     {
  815.         $this->final $lines;
  816.         $this->orig = false;
  817.     }
  818.  
  819.     function &reverse()
  820.     {
  821.         return $diff = new Text_Diff_Op_delete($this->final);
  822.     }
  823.  
  824. }
  825.  
  826. /**
  827.  * @package Text_Diff
  828.  * @author  Geoffrey T. Dairiki <dairiki@dairiki.org>
  829.  * @access  private
  830.  */
  831. class Text_Diff_Op_change extends Text_Diff_Op {
  832.  
  833.     function Text_Diff_Op_change($orig$final)
  834.     {
  835.         $this->orig $orig;
  836.         $this->final $final;
  837.     }
  838.  
  839.     function &reverse()
  840.     {
  841.         return $diff = new _DiffOp_Change($this->final$this->orig);
  842.     }
  843.  
  844. }

Documentation generated on Mon, 11 Mar 2019 10:15:12 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.