3 namespace Drupal\Component\Diff;
5 use Drupal\Component\Diff\Engine\HWLDFWordAccumulator;
6 use Drupal\Component\Utility\Unicode;
11 * @subpackage DifferenceEngine
13 class WordLevelDiff extends MappedDiff {
15 const MAX_LINE_LENGTH = 10000;
17 public function __construct($orig_lines, $closing_lines) {
18 list($orig_words, $orig_stripped) = $this->_split($orig_lines);
19 list($closing_words, $closing_stripped) = $this->_split($closing_lines);
21 parent::__construct($orig_words, $closing_words, $orig_stripped, $closing_stripped);
24 protected function _split($lines) {
28 foreach ($lines as $line) {
29 // If the line is too long, just pretend the entire line is one big word
30 // This prevents resource exhaustion problems
38 if (Unicode::strlen($line) > $this::MAX_LINE_LENGTH) {
43 if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs', $line, $m)) {
44 $words = array_merge($words, $m[0]);
45 $stripped = array_merge($stripped, $m[1]);
49 return [$words, $stripped];
52 public function orig() {
53 $orig = new HWLDFWordAccumulator();
55 foreach ($this->edits as $edit) {
56 if ($edit->type == 'copy') {
57 $orig->addWords($edit->orig);
59 elseif ($edit->orig) {
60 $orig->addWords($edit->orig, 'mark');
63 $lines = $orig->getLines();
67 public function closing() {
68 $closing = new HWLDFWordAccumulator();
70 foreach ($this->edits as $edit) {
71 if ($edit->type == 'copy') {
72 $closing->addWords($edit->closing);
74 elseif ($edit->closing) {
75 $closing->addWords($edit->closing, 'mark');
78 $lines = $closing->getLines();