includes/diffengine/Engine/xdiff.php
changeset 1 fe660c52c48f
child 1227 bdac73ed481e
equal deleted inserted replaced
0:902822492a68 1:fe660c52c48f
       
     1 <?php
       
     2 /**
       
     3  * Class used internally by Diff to actually compute the diffs.  This class
       
     4  * uses the xdiff PECL package (http://pecl.php.net/package/xdiff) to compute
       
     5  * the differences between the two input arrays.
       
     6  *
       
     7  * @author  Jon Parise <jon@horde.org>
       
     8  * @package Text_Diff
       
     9  *
       
    10  * @access private
       
    11  */
       
    12 class Text_Diff_Engine_xdiff {
       
    13 
       
    14     function diff($from_lines, $to_lines)
       
    15     {
       
    16         array_walk($from_lines, array('Text_Diff', 'trimNewlines'));
       
    17         array_walk($to_lines, array('Text_Diff', 'trimNewlines'));
       
    18 
       
    19         /* Convert the two input arrays into strings for xdiff processing. */
       
    20         $from_string = implode("\n", $from_lines);
       
    21         $to_string = implode("\n", $to_lines);
       
    22 
       
    23         /* Diff the two strings and convert the result to an array. */
       
    24         $diff = xdiff_string_diff($from_string, $to_string, count($to_lines));
       
    25         $diff = explode("\n", $diff);
       
    26 
       
    27         /* Walk through the diff one line at a time.  We build the $edits
       
    28          * array of diff operations by reading the first character of the
       
    29          * xdiff output (which is in the "unified diff" format).
       
    30          *
       
    31          * Note that we don't have enough information to detect "changed"
       
    32          * lines using this approach, so we can't add Text_Diff_Op_changed
       
    33          * instances to the $edits array.  The result is still perfectly
       
    34          * valid, albeit a little less descriptive and efficient. */
       
    35         $edits = array();
       
    36         foreach ($diff as $line) {
       
    37             switch ($line[0]) {
       
    38             case ' ':
       
    39                 $edits[] = &new Text_Diff_Op_copy(array(substr($line, 1)));
       
    40                 break;
       
    41 
       
    42             case '+':
       
    43                 $edits[] = &new Text_Diff_Op_add(array(substr($line, 1)));
       
    44                 break;
       
    45 
       
    46             case '-':
       
    47                 $edits[] = &new Text_Diff_Op_delete(array(substr($line, 1)));
       
    48                 break;
       
    49             }
       
    50         }
       
    51 
       
    52         return $edits;
       
    53     }
       
    54 
       
    55 }