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