wppaste
WordPress

Text_Diff_Engine_native

Source
wp-includes/Text/Diff/Engine/native.php:29
Class used internally by Text_Diff to actually compute the diffs.

Description

This class is implemented using native PHP code.

The algorithm used here is mostly lifted from the perl module Algorithm::Diff (version 1.06) by Ned Konz, which is available at: https://cpan.metacpan.org/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip

More ideas are taken from: http://www.ics.uci.edu/~eppstein/161/960229.html

Some ideas (and a bit of code) are taken from analyze.c, of GNU diffutils-2.7, which can be found at: ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz

Some ideas (subdivision by NCHUNKS > 2, and some optimizations) are from Geoffrey T. Dairiki [email protected]. The original PHP version of this code was written by him, and is used/adapted with his permission.

Copyright 2004-2010 The Horde Project (http://www.horde.org/)

See the enclosed file COPYING for license information (LGPL). If you did not receive this file, see https://opensource.org/license/lgpl-2-1/.

Compatibility

WordPress
core
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0).

Properties · 9

$xchangedpublic
$ychangedpublic
$xvpublic
$yvpublic
$xindpublic
$yindpublic
$seqpublic
$in_seqpublic
$lcspublic

Methods · 5

  • diff()
  • _diag()Divides the Largest Common Subsequence (LCS) of the sequences (XOFF, XLIM) and (YOFF, YLIM) into NCHUNKS approximately equally sized segments.
  • _lcsPos()
  • _compareseq()Finds LCS of two sequences.
  • _shiftBoundaries()Adjusts inserts/deletes of identical lines to join changes as much as possible.

Source code

class Text_Diff_Engine_native {     public $xchanged;    public $ychanged;    public $xv;    public $yv;    public $xind;    public $yind;    public $seq;    public $in_seq;    public $lcs;     function diff($from_lines, $to_lines)    {        array_walk($from_lines, array('Text_Diff', 'trimNewlines'));        array_walk($to_lines, array('Text_Diff', 'trimNewlines'));         $n_from = count($from_lines);        $n_to = count($to_lines);         $this->xchanged = $this->ychanged = array();        $this->xv = $this->yv = array();        $this->xind = $this->yind = array();        unset($this->seq);        unset($this->in_seq);        unset($this->lcs);         // Skip leading common lines.        for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) {            if ($from_lines[$skip] !== $to_lines[$skip]) {                break;            }            $this->xchanged[$skip] = $this->ychanged[$skip] = false;        }         // Skip trailing common lines.        $xi = $n_from; $yi = $n_to;        for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++) {            if ($from_lines[$xi] !== $to_lines[$yi]) {                break;            }            $this->xchanged[$xi] = $this->ychanged[$yi] = false;        }         // Ignore lines which do not exist in both files.        for ($xi = $skip; $xi < $n_from - $endskip; $xi++) {            $xhash[$from_lines[$xi]] = 1;        }        for ($yi = $skip; $yi < $n_to - $endskip; $yi++) {            $line = $to_lines[$yi];            if (($this->ychanged[$yi] = empty($xhash[$line]))) {                continue;            }            $yhash[$line] = 1;            $this->yv[] = $line;            $this->yind[] = $yi;        }        for ($xi = $skip; $xi < $n_from - $endskip; $xi++) {            $line = $from_lines[$xi];            if (($this->xchanged[$xi] = empty($yhash[$line]))) {                continue;            }            $this->xv[] = $line;            $this->xind[] = $xi;        }         // Find the LCS.        $this->_compareseq(0, count($this->xv), 0, count($this->yv));         // Merge edits when possible.        $this->_shiftBoundaries($from_lines, $this->xchanged, $this->ychanged);        $this->_shiftBoundaries($to_lines, $this->ychanged, $this->xchanged);         // Compute the edit operations.        $edits = array();        $xi = $yi = 0;        while ($xi < $n_from || $yi < $n_to) {            assert($yi < $n_to || $this->xchanged[$xi]);            assert($xi < $n_from || $this->ychanged[$yi]);

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/Text/Diff/Engine/native.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.