Text_Diff_Engine_native::diff()publicWP 1.0

Method of the class: Text_Diff_Engine_native{}

No Hooks.

Return

null. Nothing (null).

Usage

$Text_Diff_Engine_native = new Text_Diff_Engine_native();
$Text_Diff_Engine_native->diff( $from_lines, $to_lines );
$from_lines (required)
-
$to_lines (required)
-

Text_Diff_Engine_native::diff() code WP 6.5.2

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]);

        // Skip matching "snake".
        $copy = array();
        while ($xi < $n_from && $yi < $n_to
               && !$this->xchanged[$xi] && !$this->ychanged[$yi]) {
            $copy[] = $from_lines[$xi++];
            ++$yi;
        }
        if ($copy) {
            $edits[] = new Text_Diff_Op_copy($copy);
        }

        // Find deletes & adds.
        $delete = array();
        while ($xi < $n_from && $this->xchanged[$xi]) {
            $delete[] = $from_lines[$xi++];
        }

        $add = array();
        while ($yi < $n_to && $this->ychanged[$yi]) {
            $add[] = $to_lines[$yi++];
        }

        if ($delete && $add) {
            $edits[] = new Text_Diff_Op_change($delete, $add);
        } elseif ($delete) {
            $edits[] = new Text_Diff_Op_delete($delete);
        } elseif ($add) {
            $edits[] = new Text_Diff_Op_add($add);
        }
    }

    return $edits;
}