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

Source for file xdiff.php

Documentation is available at xdiff.php

  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.  * $Horde: framework/Text_Diff/Diff/Engine/xdiff.php,v 1.6 2008/01/04 10:07:50 jan Exp $
  9.  *
  10.  * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
  11.  *
  12.  * See the enclosed file COPYING for license information (LGPL). If you did
  13.  * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
  14.  *
  15.  * @author  Jon Parise <jon@horde.org>
  16.  * @package Text_Diff
  17.  */
  18.  
  19.     /**
  20.      */
  21.     function diff($from_lines$to_lines)
  22.     {
  23.         array_walk($from_linesarray('Text_Diff''trimNewlines'));
  24.         array_walk($to_linesarray('Text_Diff''trimNewlines'));
  25.  
  26.         /* Convert the two input arrays into strings for xdiff processing. */
  27.         $from_string implode("\n"$from_lines);
  28.         $to_string implode("\n"$to_lines);
  29.  
  30.         /* Diff the two strings and convert the result to an array. */
  31.         $diff = xdiff_string_diff($from_string$to_stringcount($to_lines));
  32.         $diff explode("\n"$diff);
  33.  
  34.         /* Walk through the diff one line at a time.  We build the $edits
  35.          * array of diff operations by reading the first character of the
  36.          * xdiff output (which is in the "unified diff" format).
  37.          *
  38.          * Note that we don't have enough information to detect "changed"
  39.          * lines using this approach, so we can't add Text_Diff_Op_changed
  40.          * instances to the $edits array.  The result is still perfectly
  41.          * valid, albeit a little less descriptive and efficient. */
  42.         $edits = array();
  43.         foreach ($diff as $line{
  44.             switch ($line[0]{
  45.             case ' ':
  46.                 $edits[&new Text_Diff_Op_copy(array(substr($line1)));
  47.                 break;
  48.  
  49.             case '+':
  50.                 $edits[&new Text_Diff_Op_add(array(substr($line1)));
  51.                 break;
  52.  
  53.             case '-':
  54.                 $edits[&new Text_Diff_Op_delete(array(substr($line1)));
  55.                 break;
  56.             }
  57.         }
  58.  
  59.         return $edits;
  60.     }
  61.  
  62. }

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