You are here

class WordLevelDiff in Diff 6

Same name and namespace in other branches
  1. 5.2 DiffEngine.php \WordLevelDiff
  2. 5 DiffEngine.php \WordLevelDiff
  3. 6.2 DiffEngine.php \WordLevelDiff
  4. 7.3 DiffEngine.php \WordLevelDiff
  5. 7.2 DiffEngine.php \WordLevelDiff

@todo document @private @subpackage DifferenceEngine

Hierarchy

Expanded class hierarchy of WordLevelDiff

File

./DiffEngine.php, line 961

View source
class WordLevelDiff extends MappedDiff {
  function MAX_LINE_LENGTH() {
    return 10000;
  }
  function WordLevelDiff($orig_lines, $closing_lines) {
    list($orig_words, $orig_stripped) = $this
      ->_split($orig_lines);
    list($closing_words, $closing_stripped) = $this
      ->_split($closing_lines);
    $this
      ->MappedDiff($orig_words, $closing_words, $orig_stripped, $closing_stripped);
  }
  function _split($lines) {
    $words = array();
    $stripped = array();
    $first = true;
    foreach ($lines as $line) {

      # If the line is too long, just pretend the entire line is one big word

      # This prevents resource exhaustion problems
      if ($first) {
        $first = false;
      }
      else {
        $words[] = "\n";
        $stripped[] = "\n";
      }
      if (strlen($line) > $this
        ->MAX_LINE_LENGTH()) {
        $words[] = $line;
        $stripped[] = $line;
      }
      else {
        if (preg_match_all('/ ( [^\\S\\n]+ | [0-9_A-Za-z\\x80-\\xff]+ | . ) (?: (?!< \\n) [^\\S\\n])? /xs', $line, $m)) {
          $words = array_merge($words, $m[0]);
          $stripped = array_merge($stripped, $m[1]);
        }
      }
    }
    return array(
      $words,
      $stripped,
    );
  }
  function orig() {
    $orig = new _HWLDF_WordAccumulator();
    foreach ($this->edits as $edit) {
      if ($edit->type == 'copy') {
        $orig
          ->addWords($edit->orig);
      }
      elseif ($edit->orig) {
        $orig
          ->addWords($edit->orig, 'mark');
      }
    }
    $lines = $orig
      ->getLines();
    return $lines;
  }
  function closing() {
    $closing = new _HWLDF_WordAccumulator();
    foreach ($this->edits as $edit) {
      if ($edit->type == 'copy') {
        $closing
          ->addWords($edit->closing);
      }
      elseif ($edit->closing) {
        $closing
          ->addWords($edit->closing, 'mark');
      }
    }
    $lines = $closing
      ->getLines();
    return $lines;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Diff::$edits property
Diff::Diff function * Constructor. * Computes diff between sequences of strings. * *
Diff::isEmpty function * Check for empty diff. * *
Diff::lcs function * Compute the length of the Longest Common Subsequence (LCS). * * This is mostly for diagnostic purposed. * *
Diff::reverse function * Compute reversed Diff. * * SYNOPSIS: * * $diff = new Diff($lines1, $lines2); * $rev = $diff->reverse(); *
Diff::_check function * Check a Diff for validity. * * This is here only for debugging purposes.
MappedDiff::MappedDiff function * Constructor. * * Computes diff between sequences of strings. * * This can be used to compute things like * case-insensitve diffs, or diffs which ignore * changes in white-space. * *
WordLevelDiff::closing function * Get the closing set of lines. * * This reconstructs the $to_lines parameter passed to the * constructor. * * Overrides Diff::closing
WordLevelDiff::MAX_LINE_LENGTH function
WordLevelDiff::orig function * Get the original set of lines. * * This reconstructs the $from_lines parameter passed to the * constructor. * * Overrides Diff::orig
WordLevelDiff::WordLevelDiff function
WordLevelDiff::_split function