WordLevelDiff.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Drupal\Component\Diff;
  3. use Drupal\Component\Diff\Engine\HWLDFWordAccumulator;
  4. use Drupal\Component\Utility\Unicode;
  5. /**
  6. * @todo document
  7. * @private
  8. * @subpackage DifferenceEngine
  9. */
  10. class WordLevelDiff extends MappedDiff {
  11. const MAX_LINE_LENGTH = 10000;
  12. public function __construct($orig_lines, $closing_lines) {
  13. list($orig_words, $orig_stripped) = $this->_split($orig_lines);
  14. list($closing_words, $closing_stripped) = $this->_split($closing_lines);
  15. parent::__construct($orig_words, $closing_words, $orig_stripped, $closing_stripped);
  16. }
  17. protected function _split($lines) {
  18. $words = [];
  19. $stripped = [];
  20. $first = TRUE;
  21. foreach ($lines as $line) {
  22. // If the line is too long, just pretend the entire line is one big word
  23. // This prevents resource exhaustion problems
  24. if ( $first ) {
  25. $first = FALSE;
  26. }
  27. else {
  28. $words[] = "\n";
  29. $stripped[] = "\n";
  30. }
  31. if (Unicode::strlen($line) > $this::MAX_LINE_LENGTH) {
  32. $words[] = $line;
  33. $stripped[] = $line;
  34. }
  35. else {
  36. if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs', $line, $m)) {
  37. $words = array_merge($words, $m[0]);
  38. $stripped = array_merge($stripped, $m[1]);
  39. }
  40. }
  41. }
  42. return [$words, $stripped];
  43. }
  44. public function orig() {
  45. $orig = new HWLDFWordAccumulator();
  46. foreach ($this->edits as $edit) {
  47. if ($edit->type == 'copy') {
  48. $orig->addWords($edit->orig);
  49. }
  50. elseif ($edit->orig) {
  51. $orig->addWords($edit->orig, 'mark');
  52. }
  53. }
  54. $lines = $orig->getLines();
  55. return $lines;
  56. }
  57. public function closing() {
  58. $closing = new HWLDFWordAccumulator();
  59. foreach ($this->edits as $edit) {
  60. if ($edit->type == 'copy') {
  61. $closing->addWords($edit->closing);
  62. }
  63. elseif ($edit->closing) {
  64. $closing->addWords($edit->closing, 'mark');
  65. }
  66. }
  67. $lines = $closing->getLines();
  68. return $lines;
  69. }
  70. }