From f2df176b330b60d0f57b7ab0b799a61b1ace8a86 Mon Sep 17 00:00:00 2001 From: dairiki Date: Sat, 15 Dec 2001 02:39:43 +0000 Subject: [PATCH] New class MappedDiff to support trailing-space insentive diffs (and the like). git-svn-id: svn://svn.code.sf.net/p/phpwiki/code/trunk@777 96ab9672-09ca-45d6-a79d-3d69d39ca109 --- lib/difflib.php | 78 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 5 deletions(-) diff --git a/lib/difflib.php b/lib/difflib.php index f9b17bdbd..22077c7e5 100644 --- a/lib/difflib.php +++ b/lib/difflib.php @@ -1,5 +1,5 @@ orig = $lines; - $this->final = &$this->orig; + function _DiffOp_Copy ($orig, $final = false) { + if (!is_array($final)) + $final = $orig; + $this->orig = $orig; + $this->final = $final; } function reverse() { - return $this; + return new _DiffOp_Copy($this->final, $this->orig); } } @@ -608,6 +610,72 @@ class Diff } } + +if (!function_exists('array_map')) { + function array_map($map_func, $array) { + $result = array(); + foreach ($array as $x) + $result[] = call_user_func($map_func, $x); + return $result; + } +} + + +/** + * FIXME: bad name. + */ +class MappedDiff +extends Diff +{ + /** + * 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. + * + * @param $from_lines array An array of strings. + * (Typically these are lines from a file.) + * + * @param $to_lines array An array of strings. + * + * @param $mapped_from_lines array 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 $mapped_to_lines array This array should + * have the same number of elements as $to_lines. + */ + function MappedDiff($from_lines, $to_lines, + $mapped_from_lines, $mapped_to_lines) { + + assert(sizeof($from_lines) == sizeof($mapped_from_lines)); + assert(sizeof($to_lines) == sizeof($mapped_to_lines)); + + $this->Diff($mapped_from_lines, $mapped_to_lines); + + $xi = $yi = 0; + for ($i = 0; $i < sizeof($this->edits); $i++) { + $orig = &$this->edits[$i]->orig; + if (is_array($orig)) { + $orig = array_slice($from_lines, $xi, sizeof($orig)); + $xi += sizeof($orig); + } + + $final = &$this->edits[$i]->final; + if (is_array($final)) { + $final = array_slice($to_lines, $yi, sizeof($final)); + $yi += sizeof($final); + } + } + } +} + + /** * A class to format Diffs * -- 2.45.0