The original PHP version of this code was written by Geoffrey T. Dairiki , and is used/adapted with his permission. Copyright 2004 Geoffrey T. Dairiki Copyright 2004-2016 Horde LLC (http://www.horde.org/) See the enclosed file COPYING for license information (LGPL). If you did not receive this file, see http://www.horde.org/licenses/lgpl21.
Author: Geoffrey T. Dairiki (dairiki@dairiki.org)
コード例 #1
0
ファイル: Mapped.php プロジェクト: andi98/antragsgruen
 /**
  * Computes a diff between sequences of strings.
  *
  * This can be used to compute things like case-insensitve diffs, or diffs
  * which ignore changes in white-space.
  *
  * @param array $from_lines         An array of strings.
  * @param array $to_lines           An array of strings.
  * @param array $mapped_from_lines  This array should have the same size
  *                                  number of elements as $from_lines.  The
  *                                  elements in $mapped_from_lines and
  *                                  $mapped_to_lines are what is actually
  *                                  compared when computing the diff.
  * @param array $mapped_to_lines    This array should have the same number
  *                                  of elements as $to_lines.
  */
 public function __construct($from_lines, $to_lines, $mapped_from_lines, $mapped_to_lines)
 {
     assert(count($from_lines) == count($mapped_from_lines));
     assert(count($to_lines) == count($mapped_to_lines));
     parent::__construct($mapped_from_lines, $mapped_to_lines);
     $xi = $yi = 0;
     for ($i = 0; $i < count($this->_edits); $i++) {
         $orig =& $this->_edits[$i]->orig;
         if (is_array($orig)) {
             $orig = array_slice($from_lines, $xi, count($orig));
             $xi += count($orig);
         }
         $final =& $this->_edits[$i]->final;
         if (is_array($final)) {
             $final = array_slice($to_lines, $yi, count($final));
             $yi += count($final);
         }
     }
 }
コード例 #2
0
ファイル: Renderer.php プロジェクト: JCQS04/myimouto
 /**
  * Renders a diff.
  *
  * @param Horde_Text_Diff $diff  A Horde_Text_Diff object.
  *
  * @return string  The formatted output.
  */
 public function render($diff)
 {
     $xi = $yi = 1;
     $block = false;
     $context = array();
     $nlead = $this->_leading_context_lines;
     $ntrail = $this->_trailing_context_lines;
     $output = $this->_startDiff();
     $diffs = $diff->getDiff();
     foreach ($diffs as $i => $edit) {
         /* If these are unchanged (copied) lines, and we want to keep
          * leading or trailing context lines, extract them from the copy
          * block. */
         if ($edit instanceof Horde_Text_Diff_Op_Copy) {
             /* Do we have any diff blocks yet? */
             if (is_array($block)) {
                 /* How many lines to keep as context from the copy
                  * block. */
                 $keep = $i == count($diffs) - 1 ? $ntrail : $nlead + $ntrail;
                 if (count($edit->orig) <= $keep) {
                     /* We have less lines in the block than we want for
                      * context => keep the whole block. */
                     $block[] = $edit;
                 } else {
                     if ($ntrail) {
                         /* Create a new block with as many lines as we need
                          * for the trailing context. */
                         $context = array_slice($edit->orig, 0, $ntrail);
                         $block[] = new Horde_Text_Diff_Op_Copy($context);
                     }
                     /* @todo */
                     $output .= $this->_block($x0, $ntrail + $xi - $x0, $y0, $ntrail + $yi - $y0, $block);
                     $block = false;
                 }
             }
             /* Keep the copy block as the context for the next block. */
             $context = $edit->orig;
         } else {
             /* Don't we have any diff blocks yet? */
             if (!is_array($block)) {
                 /* Extract context lines from the preceding copy block. */
                 $context = array_slice($context, count($context) - $nlead);
                 $x0 = $xi - count($context);
                 $y0 = $yi - count($context);
                 $block = array();
                 if ($context) {
                     $block[] = new Horde_Text_Diff_Op_Copy($context);
                 }
             }
             $block[] = $edit;
         }
         if ($edit->orig) {
             $xi += count($edit->orig);
         }
         if ($edit->final) {
             $yi += count($edit->final);
         }
     }
     if (is_array($block)) {
         $output .= $this->_block($x0, $xi - $x0, $y0, $yi - $y0, $block);
     }
     return $output . $this->_endDiff();
 }
コード例 #3
0
ファイル: DiffUtils.php プロジェクト: andi98/antragsgruen
 /**
  * @static
  * @param Horde_Text_Diff $diff
  * @param int $first_line_no
  * @return int
  */
 public static function getFistDiffLine($diff, $first_line_no = 1)
 {
     $edits = $diff->getDiff();
     $line = $first_line_no;
     foreach ($edits as $edit) {
         if (get_class($edit) == "Horde_Text_Diff_Op_Add") {
             return $line + 1;
         }
         if (get_class($edit) == "Horde_Text_Diff_Op_Delete") {
             return $line;
         }
         if (get_class($edit) == "Horde_Text_Diff_Op_Change") {
             return $line;
         }
         if (is_array($edit->orig)) {
             $line += substr_count(implode("\n", $edit->orig), "#ZEILE#");
         }
     }
     return 0;
 }
コード例 #4
0
ファイル: Sidebyside.php プロジェクト: ByMyHandsOnly/BMHO_Web
 public function diffChar($orig, $final)
 {
     $line1 = preg_split('//', implode("<br />", $orig), -1, PREG_SPLIT_NO_EMPTY);
     $line2 = preg_split('//', implode("<br />", $final), -1, PREG_SPLIT_NO_EMPTY);
     $z = new Horde_Text_Diff($line1, $line2);
     if ($z->isEmpty()) {
         return [$orig[0], $final[0]];
     }
     // echo "<pre>";print_r($z);echo "</pre>";
     $renderer = new Horde_Text_Diff_Renderer_Character(10000);
     return $renderer->render($z);
 }
コード例 #5
-1
ファイル: EngineTest.php プロジェクト: jubinpatel/horde
 public function testStringEngine()
 {
     $patch = file_get_contents(__DIR__ . '/fixtures/unified.patch');
     $diff = new Horde_Text_Diff('String', array($patch));
     $this->_testDiff($diff);
     $patch = file_get_contents(__DIR__ . '/fixtures/unified2.patch');
     try {
         $diff = new Horde_Text_Diff('String', array($patch));
         $this->fail('Horde_Text_Diff_Exception expected');
     } catch (Horde_Text_Diff_Exception $e) {
     }
     $diff = new Horde_Text_Diff('String', array($patch, 'unified'));
     $edits = $diff->getDiff();
     $this->assertEquals(1, count($edits));
     $this->assertInstanceof('Horde_Text_Diff_Op_Change', $edits[0]);
     $this->assertEquals('For the first time in U.S. history number of private contractors and troops are equal', $edits[0]->orig[0]);
     $this->assertEquals('Number of private contractors and troops are equal for first time in U.S. history', $edits[0]->final[0]);
     $patch = file_get_contents(__DIR__ . '/fixtures/context.patch');
     $diff = new Horde_Text_Diff('String', array($patch));
     $this->_testDiff($diff);
 }