3 namespace Drupal\Component\Diff;
5 use Drupal\Component\Diff\Engine\HWLDFWordAccumulator;
10 * @subpackage DifferenceEngine
12 class WordLevelDiff extends MappedDiff {
14 const MAX_LINE_LENGTH = 10000;
16 public function __construct($orig_lines, $closing_lines) {
17 list($orig_words, $orig_stripped) = $this->_split($orig_lines);
18 list($closing_words, $closing_stripped) = $this->_split($closing_lines);
20 parent::__construct($orig_words, $closing_words, $orig_stripped, $closing_stripped);
23 protected function _split($lines) {
27 foreach ($lines as $line) {
28 // If the line is too long, just pretend the entire line is one big word
29 // This prevents resource exhaustion problems
37 if (mb_strlen($line) > $this::MAX_LINE_LENGTH) {
42 if (preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs', $line, $m)) {
43 $words = array_merge($words, $m[0]);
44 $stripped = array_merge($stripped, $m[1]);
48 return [$words, $stripped];
51 public function orig() {
52 $orig = new HWLDFWordAccumulator();
54 foreach ($this->edits as $edit) {
55 if ($edit->type == 'copy') {
56 $orig->addWords($edit->orig);
58 elseif ($edit->orig) {
59 $orig->addWords($edit->orig, 'mark');
62 $lines = $orig->getLines();
66 public function closing() {
67 $closing = new HWLDFWordAccumulator();
69 foreach ($this->edits as $edit) {
70 if ($edit->type == 'copy') {
71 $closing->addWords($edit->closing);
73 elseif ($edit->closing) {
74 $closing->addWords($edit->closing, 'mark');
77 $lines = $closing->getLines();