From: Geoffrey T. D. <da...@us...> - 2001-12-15 02:39:45
|
Update of /cvsroot/phpwiki/phpwiki/lib In directory usw-pr-cvs1:/tmp/cvs-serv6297/lib Modified Files: difflib.php Log Message: New class MappedDiff to support trailing-space insentive diffs (and the like). Index: difflib.php =================================================================== RCS file: /cvsroot/phpwiki/phpwiki/lib/difflib.php,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -r1.2 -r1.3 *** difflib.php 2001/12/14 20:15:02 1.2 --- difflib.php 2001/12/15 02:39:43 1.3 *************** *** 27,37 **** var $type = 'copy'; ! function _DiffOp_Copy ($lines) { ! $this->orig = $lines; ! $this->final = &$this->orig; } function reverse() { ! return $this; } } --- 27,39 ---- var $type = 'copy'; ! function _DiffOp_Copy ($orig, $final = false) { ! if (!is_array($final)) ! $final = $orig; ! $this->orig = $orig; ! $this->final = $final; } function reverse() { ! return new _DiffOp_Copy($this->final, $this->orig); } } *************** *** 608,611 **** --- 610,679 ---- } } + + + 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); + } + } + } + } + /** |