1 diff.inc DiffEngine::diff($from_lines, $to_lines)

Parameters

$from_lines:

$to_lines:

Return value

array

File

core/includes/diff.inc, line 184
A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)

Class

DiffEngine
Class used internally by Diff to actually compute the diffs.

Code

function diff($from_lines, $to_lines) {
  // Diff and store locally
  $this->diffLocal($from_lines, $to_lines);

  // Merge edits when possible
  $this->shiftBoundaries($from_lines, $this->xchanged, $this->ychanged);
  $this->shiftBoundaries($to_lines, $this->ychanged, $this->xchanged);

  // Compute the edit operations.
  $n_from = count($from_lines);
  $n_to = count($to_lines);

  $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 DiffOpCopy($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 DiffOpChange($delete, $add);
    }
    elseif ($delete) {
      $edits[] = new DiffOpDelete($delete);
    }
    elseif ($add) {
      $edits[] = new DiffOpAdd($add);
    }
  }

  return $edits;
}