Mapped.php 2.0 KB

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