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.  * $Horde: framework/Text_Diff/Diff/Engine/xdiff.php,v 1.3 2007/09/25 21:59:46 chuck Exp $
  4.  *
  5.  * Class used internally by Diff to actually compute the diffs.  This class
  6.  * uses the xdiff PECL package (http://pecl.php.net/package/xdiff) to compute
  7.  * the differences between the two input arrays.
  8.  *
  9.  * Copyright 2004-2007 The Horde Project (http://www.horde.org/)
  10.  *
  11.  * See the enclosed file COPYING for license information (LGPL). If you
  12.  * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  13.  *
  14.  * @author  Jon Parise <jon@horde.org>
  15.  * @package Text_Diff
  16.  */
  17.  
  18.     /**
  19.      */
  20.     function diff($from_lines$to_lines)
  21.     {
  22.         array_walk($from_linesarray('Text_Diff''trimNewlines'));
  23.         array_walk($to_linesarray('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_stringcount($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.             switch ($line[0]{
  44.             case ' ':
  45.                 $edits[&new Text_Diff_Op_copy(array(substr($line1)));
  46.                 break;
  47.  
  48.             case '+':
  49.                 $edits[&new Text_Diff_Op_add(array(substr($line1)));
  50.                 break;
  51.  
  52.             case '-':
  53.                 $edits[&new Text_Diff_Op_delete(array(substr($line1)));
  54.                 break;
  55.             }
  56.         }
  57.  
  58.         return $edits;
  59.     }
  60.  
  61. }

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