Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Component / Diff / WordLevelDiff.php
1 <?php
2
3 namespace Drupal\Component\Diff;
4
5 use Drupal\Component\Diff\Engine\HWLDFWordAccumulator;
6 use Drupal\Component\Utility\Unicode;
7
8 /**
9  * @todo document
10  * @private
11  * @subpackage DifferenceEngine
12  */
13 class WordLevelDiff extends MappedDiff {
14
15   const MAX_LINE_LENGTH = 10000;
16
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);
20
21     parent::__construct($orig_words, $closing_words, $orig_stripped, $closing_stripped);
22   }
23
24   protected function _split($lines) {
25     $words = [];
26     $stripped = [];
27     $first = TRUE;
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
31       if ( $first ) {
32         $first = FALSE;
33       }
34       else {
35         $words[] = "\n";
36         $stripped[] = "\n";
37       }
38       if (Unicode::strlen($line) > $this::MAX_LINE_LENGTH) {
39         $words[] = $line;
40         $stripped[] = $line;
41       }
42       else {
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]);
46         }
47       }
48     }
49     return [$words, $stripped];
50   }
51
52   public function orig() {
53     $orig = new HWLDFWordAccumulator();
54
55     foreach ($this->edits as $edit) {
56       if ($edit->type == 'copy') {
57         $orig->addWords($edit->orig);
58       }
59       elseif ($edit->orig) {
60         $orig->addWords($edit->orig, 'mark');
61       }
62     }
63     $lines = $orig->getLines();
64     return $lines;
65   }
66
67   public function closing() {
68     $closing = new HWLDFWordAccumulator();
69
70     foreach ($this->edits as $edit) {
71       if ($edit->type == 'copy') {
72         $closing->addWords($edit->closing);
73       }
74       elseif ($edit->closing) {
75         $closing->addWords($edit->closing, 'mark');
76       }
77     }
78     $lines = $closing->getLines();
79     return $lines;
80   }
81
82 }