Mapped.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * $Horde: framework/Text_Diff/Diff/Mapped.php,v 1.3.2.4 2009/01/06 15:23:41 jan Exp $
  4. *
  5. * Copyright 2007-2009 The Horde Project (http://www.horde.org/)
  6. *
  7. * See the enclosed file COPYING for license information (LGPL). If you did
  8. * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
  9. *
  10. * @package Text_Diff
  11. * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
  12. */
  13. class Text_Diff_Mapped extends Text_Diff {
  14. /**
  15. * Computes a diff between sequences of strings.
  16. *
  17. * This can be used to compute things like case-insensitve diffs, or diffs
  18. * which ignore changes in white-space.
  19. *
  20. * @param array $from_lines An array of strings.
  21. * @param array $to_lines An array of strings.
  22. * @param array $mapped_from_lines This array should have the same size
  23. * number of elements as $from_lines. The
  24. * elements in $mapped_from_lines and
  25. * $mapped_to_lines are what is actually
  26. * compared when computing the diff.
  27. * @param array $mapped_to_lines This array should have the same number
  28. * of elements as $to_lines.
  29. */
  30. function Text_Diff_Mapped($from_lines, $to_lines,
  31. $mapped_from_lines, $mapped_to_lines)
  32. {
  33. assert(count($from_lines) == count($mapped_from_lines));
  34. assert(count($to_lines) == count($mapped_to_lines));
  35. parent::Text_Diff($mapped_from_lines, $mapped_to_lines);
  36. $xi = $yi = 0;
  37. for ($i = 0; $i < count($this->_edits); $i++) {
  38. $orig = &$this->_edits[$i]->orig;
  39. if (is_array($orig)) {
  40. $orig = array_slice($from_lines, $xi, count($orig));
  41. $xi += count($orig);
  42. }
  43. $final = &$this->_edits[$i]->final;
  44. if (is_array($final)) {
  45. $final = array_slice($to_lines, $yi, count($final));
  46. $yi += count($final);
  47. }
  48. }
  49. }
  50. }