|
indvdum@0
|
1 |
<?php
|
|
indvdum@0
|
2 |
/**
|
|
indvdum@0
|
3 |
*
|
|
indvdum@0
|
4 |
* @package diff
|
|
indvdum@0
|
5 |
* @version $Id$
|
|
indvdum@0
|
6 |
* @copyright (c) 2006 phpBB Group
|
|
indvdum@0
|
7 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
indvdum@0
|
8 |
*
|
|
indvdum@0
|
9 |
*/
|
|
indvdum@0
|
10 |
|
|
indvdum@0
|
11 |
/**
|
|
indvdum@0
|
12 |
* @ignore
|
|
indvdum@0
|
13 |
*/
|
|
indvdum@0
|
14 |
if (!defined('IN_PHPBB'))
|
|
indvdum@0
|
15 |
{
|
|
indvdum@0
|
16 |
exit;
|
|
indvdum@0
|
17 |
}
|
|
indvdum@0
|
18 |
|
|
indvdum@0
|
19 |
/**
|
|
indvdum@0
|
20 |
* Code from pear.php.net, Text_Diff-1.1.0 package
|
|
indvdum@0
|
21 |
* http://pear.php.net/package/Text_Diff/ (native engine)
|
|
indvdum@0
|
22 |
*
|
|
indvdum@0
|
23 |
* Modified by phpBB Group to meet our coding standards
|
|
indvdum@0
|
24 |
* and being able to integrate into phpBB
|
|
indvdum@0
|
25 |
*
|
|
indvdum@0
|
26 |
* Class used internally by Text_Diff to actually compute the diffs. This
|
|
indvdum@0
|
27 |
* class is implemented using native PHP code.
|
|
indvdum@0
|
28 |
*
|
|
indvdum@0
|
29 |
* The algorithm used here is mostly lifted from the perl module
|
|
indvdum@0
|
30 |
* Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
|
|
indvdum@0
|
31 |
* http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
|
|
indvdum@0
|
32 |
*
|
|
indvdum@0
|
33 |
* More ideas are taken from: http://www.ics.uci.edu/~eppstein/161/960229.html
|
|
indvdum@0
|
34 |
*
|
|
indvdum@0
|
35 |
* Some ideas (and a bit of code) are taken from analyze.c, of GNU
|
|
indvdum@0
|
36 |
* diffutils-2.7, which can be found at:
|
|
indvdum@0
|
37 |
* ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
|
|
indvdum@0
|
38 |
*
|
|
indvdum@0
|
39 |
* Some ideas (subdivision by NCHUNKS > 2, and some optimizations) are from
|
|
indvdum@0
|
40 |
* Geoffrey T. Dairiki <dairiki@dairiki.org>. The original PHP version of this
|
|
indvdum@0
|
41 |
* code was written by him, and is used/adapted with his permission.
|
|
indvdum@0
|
42 |
*
|
|
indvdum@0
|
43 |
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
|
|
indvdum@0
|
44 |
*
|
|
indvdum@0
|
45 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
indvdum@0
|
46 |
* @package diff
|
|
indvdum@0
|
47 |
*
|
|
indvdum@0
|
48 |
* @access private
|
|
indvdum@0
|
49 |
*/
|
|
indvdum@0
|
50 |
class diff_engine
|
|
indvdum@0
|
51 |
{
|
|
indvdum@0
|
52 |
/**
|
|
indvdum@0
|
53 |
* If set to true we trim all lines before we compare them. This ensures that sole space/tab changes do not trigger diffs.
|
|
indvdum@0
|
54 |
*/
|
|
indvdum@0
|
55 |
var $skip_whitespace_changes = true;
|
|
indvdum@0
|
56 |
|
|
indvdum@0
|
57 |
function diff(&$from_lines, &$to_lines, $preserve_cr = true)
|
|
indvdum@0
|
58 |
{
|
|
indvdum@0
|
59 |
// Remove empty lines...
|
|
indvdum@0
|
60 |
// If preserve_cr is true, we basically only change \r\n and bare \r to \n to get the same carriage returns for both files
|
|
indvdum@0
|
61 |
// If it is false, we try to only use \n once per line and ommit all empty lines to be able to get a proper data diff
|
|
indvdum@0
|
62 |
|
|
indvdum@0
|
63 |
if (is_array($from_lines))
|
|
indvdum@0
|
64 |
{
|
|
indvdum@0
|
65 |
$from_lines = implode("\n", $from_lines);
|
|
indvdum@0
|
66 |
}
|
|
indvdum@0
|
67 |
|
|
indvdum@0
|
68 |
if (is_array($to_lines))
|
|
indvdum@0
|
69 |
{
|
|
indvdum@0
|
70 |
$to_lines = implode("\n", $to_lines);
|
|
indvdum@0
|
71 |
}
|
|
indvdum@0
|
72 |
|
|
indvdum@0
|
73 |
if ($preserve_cr)
|
|
indvdum@0
|
74 |
{
|
|
indvdum@0
|
75 |
$from_lines = explode("\n", str_replace("\r", "\n", str_replace("\r\n", "\n", $from_lines)));
|
|
indvdum@0
|
76 |
$to_lines = explode("\n", str_replace("\r", "\n", str_replace("\r\n", "\n", $to_lines)));
|
|
indvdum@0
|
77 |
}
|
|
indvdum@0
|
78 |
else
|
|
indvdum@0
|
79 |
{
|
|
indvdum@0
|
80 |
$from_lines = explode("\n", preg_replace('#[\n\r]+#', "\n", $from_lines));
|
|
indvdum@0
|
81 |
$to_lines = explode("\n", preg_replace('#[\n\r]+#', "\n", $to_lines));
|
|
indvdum@0
|
82 |
}
|
|
indvdum@0
|
83 |
|
|
indvdum@0
|
84 |
$n_from = sizeof($from_lines);
|
|
indvdum@0
|
85 |
$n_to = sizeof($to_lines);
|
|
indvdum@0
|
86 |
|
|
indvdum@0
|
87 |
$this->xchanged = $this->ychanged = $this->xv = $this->yv = $this->xind = $this->yind = array();
|
|
indvdum@0
|
88 |
unset($this->seq, $this->in_seq, $this->lcs);
|
|
indvdum@0
|
89 |
|
|
indvdum@0
|
90 |
// Skip leading common lines.
|
|
indvdum@0
|
91 |
for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++)
|
|
indvdum@0
|
92 |
{
|
|
indvdum@0
|
93 |
if (trim($from_lines[$skip]) !== trim($to_lines[$skip]))
|
|
indvdum@0
|
94 |
{
|
|
indvdum@0
|
95 |
break;
|
|
indvdum@0
|
96 |
}
|
|
indvdum@0
|
97 |
$this->xchanged[$skip] = $this->ychanged[$skip] = false;
|
|
indvdum@0
|
98 |
}
|
|
indvdum@0
|
99 |
|
|
indvdum@0
|
100 |
// Skip trailing common lines.
|
|
indvdum@0
|
101 |
$xi = $n_from;
|
|
indvdum@0
|
102 |
$yi = $n_to;
|
|
indvdum@0
|
103 |
|
|
indvdum@0
|
104 |
for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++)
|
|
indvdum@0
|
105 |
{
|
|
indvdum@0
|
106 |
if (trim($from_lines[$xi]) !== trim($to_lines[$yi]))
|
|
indvdum@0
|
107 |
{
|
|
indvdum@0
|
108 |
break;
|
|
indvdum@0
|
109 |
}
|
|
indvdum@0
|
110 |
$this->xchanged[$xi] = $this->ychanged[$yi] = false;
|
|
indvdum@0
|
111 |
}
|
|
indvdum@0
|
112 |
|
|
indvdum@0
|
113 |
// Ignore lines which do not exist in both files.
|
|
indvdum@0
|
114 |
for ($xi = $skip; $xi < $n_from - $endskip; $xi++)
|
|
indvdum@0
|
115 |
{
|
|
indvdum@0
|
116 |
if ($this->skip_whitespace_changes) $xhash[trim($from_lines[$xi])] = 1; else $xhash[$from_lines[$xi]] = 1;
|
|
indvdum@0
|
117 |
}
|
|
indvdum@0
|
118 |
|
|
indvdum@0
|
119 |
for ($yi = $skip; $yi < $n_to - $endskip; $yi++)
|
|
indvdum@0
|
120 |
{
|
|
indvdum@0
|
121 |
$line = ($this->skip_whitespace_changes) ? trim($to_lines[$yi]) : $to_lines[$yi];
|
|
indvdum@0
|
122 |
|
|
indvdum@0
|
123 |
if (($this->ychanged[$yi] = empty($xhash[$line])))
|
|
indvdum@0
|
124 |
{
|
|
indvdum@0
|
125 |
continue;
|
|
indvdum@0
|
126 |
}
|
|
indvdum@0
|
127 |
$yhash[$line] = 1;
|
|
indvdum@0
|
128 |
$this->yv[] = $line;
|
|
indvdum@0
|
129 |
$this->yind[] = $yi;
|
|
indvdum@0
|
130 |
}
|
|
indvdum@0
|
131 |
|
|
indvdum@0
|
132 |
for ($xi = $skip; $xi < $n_from - $endskip; $xi++)
|
|
indvdum@0
|
133 |
{
|
|
indvdum@0
|
134 |
$line = ($this->skip_whitespace_changes) ? trim($from_lines[$xi]) : $from_lines[$xi];
|
|
indvdum@0
|
135 |
|
|
indvdum@0
|
136 |
if (($this->xchanged[$xi] = empty($yhash[$line])))
|
|
indvdum@0
|
137 |
{
|
|
indvdum@0
|
138 |
continue;
|
|
indvdum@0
|
139 |
}
|
|
indvdum@0
|
140 |
$this->xv[] = $line;
|
|
indvdum@0
|
141 |
$this->xind[] = $xi;
|
|
indvdum@0
|
142 |
}
|
|
indvdum@0
|
143 |
|
|
indvdum@0
|
144 |
// Find the LCS.
|
|
indvdum@0
|
145 |
$this->_compareseq(0, sizeof($this->xv), 0, sizeof($this->yv));
|
|
indvdum@0
|
146 |
|
|
indvdum@0
|
147 |
// Merge edits when possible.
|
|
indvdum@0
|
148 |
if ($this->skip_whitespace_changes)
|
|
indvdum@0
|
149 |
{
|
|
indvdum@0
|
150 |
$from_lines_clean = array_map('trim', $from_lines);
|
|
indvdum@0
|
151 |
$to_lines_clean = array_map('trim', $to_lines);
|
|
indvdum@0
|
152 |
|
|
indvdum@0
|
153 |
$this->_shift_boundaries($from_lines_clean, $this->xchanged, $this->ychanged);
|
|
indvdum@0
|
154 |
$this->_shift_boundaries($to_lines_clean, $this->ychanged, $this->xchanged);
|
|
indvdum@0
|
155 |
|
|
indvdum@0
|
156 |
unset($from_lines_clean, $to_lines_clean);
|
|
indvdum@0
|
157 |
}
|
|
indvdum@0
|
158 |
else
|
|
indvdum@0
|
159 |
{
|
|
indvdum@0
|
160 |
$this->_shift_boundaries($from_lines, $this->xchanged, $this->ychanged);
|
|
indvdum@0
|
161 |
$this->_shift_boundaries($to_lines, $this->ychanged, $this->xchanged);
|
|
indvdum@0
|
162 |
}
|
|
indvdum@0
|
163 |
|
|
indvdum@0
|
164 |
// Compute the edit operations.
|
|
indvdum@0
|
165 |
$edits = array();
|
|
indvdum@0
|
166 |
$xi = $yi = 0;
|
|
indvdum@0
|
167 |
|
|
indvdum@0
|
168 |
while ($xi < $n_from || $yi < $n_to)
|
|
indvdum@0
|
169 |
{
|
|
indvdum@0
|
170 |
// Skip matching "snake".
|
|
indvdum@0
|
171 |
$copy = array();
|
|
indvdum@0
|
172 |
|
|
indvdum@0
|
173 |
while ($xi < $n_from && $yi < $n_to && !$this->xchanged[$xi] && !$this->ychanged[$yi])
|
|
indvdum@0
|
174 |
{
|
|
indvdum@0
|
175 |
$copy[] = $from_lines[$xi++];
|
|
indvdum@0
|
176 |
$yi++;
|
|
indvdum@0
|
177 |
}
|
|
indvdum@0
|
178 |
|
|
indvdum@0
|
179 |
if ($copy)
|
|
indvdum@0
|
180 |
{
|
|
indvdum@0
|
181 |
$edits[] = new diff_op_copy($copy);
|
|
indvdum@0
|
182 |
}
|
|
indvdum@0
|
183 |
|
|
indvdum@0
|
184 |
// Find deletes & adds.
|
|
indvdum@0
|
185 |
$delete = array();
|
|
indvdum@0
|
186 |
while ($xi < $n_from && $this->xchanged[$xi])
|
|
indvdum@0
|
187 |
{
|
|
indvdum@0
|
188 |
$delete[] = $from_lines[$xi++];
|
|
indvdum@0
|
189 |
}
|
|
indvdum@0
|
190 |
|
|
indvdum@0
|
191 |
$add = array();
|
|
indvdum@0
|
192 |
while ($yi < $n_to && $this->ychanged[$yi])
|
|
indvdum@0
|
193 |
{
|
|
indvdum@0
|
194 |
$add[] = $to_lines[$yi++];
|
|
indvdum@0
|
195 |
}
|
|
indvdum@0
|
196 |
|
|
indvdum@0
|
197 |
if ($delete && $add)
|
|
indvdum@0
|
198 |
{
|
|
indvdum@0
|
199 |
$edits[] = new diff_op_change($delete, $add);
|
|
indvdum@0
|
200 |
}
|
|
indvdum@0
|
201 |
else if ($delete)
|
|
indvdum@0
|
202 |
{
|
|
indvdum@0
|
203 |
$edits[] = new diff_op_delete($delete);
|
|
indvdum@0
|
204 |
}
|
|
indvdum@0
|
205 |
else if ($add)
|
|
indvdum@0
|
206 |
{
|
|
indvdum@0
|
207 |
$edits[] = new diff_op_add($add);
|
|
indvdum@0
|
208 |
}
|
|
indvdum@0
|
209 |
}
|
|
indvdum@0
|
210 |
|
|
indvdum@0
|
211 |
return $edits;
|
|
indvdum@0
|
212 |
}
|
|
indvdum@0
|
213 |
|
|
indvdum@0
|
214 |
/**
|
|
indvdum@0
|
215 |
* Divides the Largest Common Subsequence (LCS) of the sequences (XOFF,
|
|
indvdum@0
|
216 |
* XLIM) and (YOFF, YLIM) into NCHUNKS approximately equally sized segments.
|
|
indvdum@0
|
217 |
*
|
|
indvdum@0
|
218 |
* Returns (LCS, PTS). LCS is the length of the LCS. PTS is an array of
|
|
indvdum@0
|
219 |
* NCHUNKS+1 (X, Y) indexes giving the diving points between sub
|
|
indvdum@0
|
220 |
* sequences. The first sub-sequence is contained in (X0, X1), (Y0, Y1),
|
|
indvdum@0
|
221 |
* the second in (X1, X2), (Y1, Y2) and so on. Note that (X0, Y0) ==
|
|
indvdum@0
|
222 |
* (XOFF, YOFF) and (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
|
|
indvdum@0
|
223 |
*
|
|
indvdum@0
|
224 |
* This function assumes that the first lines of the specified portions of
|
|
indvdum@0
|
225 |
* the two files do not match, and likewise that the last lines do not
|
|
indvdum@0
|
226 |
* match. The caller must trim matching lines from the beginning and end
|
|
indvdum@0
|
227 |
* of the portions it is going to specify.
|
|
indvdum@0
|
228 |
*/
|
|
indvdum@0
|
229 |
function _diag($xoff, $xlim, $yoff, $ylim, $nchunks)
|
|
indvdum@0
|
230 |
{
|
|
indvdum@0
|
231 |
$flip = false;
|
|
indvdum@0
|
232 |
|
|
indvdum@0
|
233 |
if ($xlim - $xoff > $ylim - $yoff)
|
|
indvdum@0
|
234 |
{
|
|
indvdum@0
|
235 |
// Things seems faster (I'm not sure I understand why) when the shortest sequence is in X.
|
|
indvdum@0
|
236 |
$flip = true;
|
|
indvdum@0
|
237 |
list($xoff, $xlim, $yoff, $ylim) = array($yoff, $ylim, $xoff, $xlim);
|
|
indvdum@0
|
238 |
}
|
|
indvdum@0
|
239 |
|
|
indvdum@0
|
240 |
if ($flip)
|
|
indvdum@0
|
241 |
{
|
|
indvdum@0
|
242 |
for ($i = $ylim - 1; $i >= $yoff; $i--)
|
|
indvdum@0
|
243 |
{
|
|
indvdum@0
|
244 |
$ymatches[$this->xv[$i]][] = $i;
|
|
indvdum@0
|
245 |
}
|
|
indvdum@0
|
246 |
}
|
|
indvdum@0
|
247 |
else
|
|
indvdum@0
|
248 |
{
|
|
indvdum@0
|
249 |
for ($i = $ylim - 1; $i >= $yoff; $i--)
|
|
indvdum@0
|
250 |
{
|
|
indvdum@0
|
251 |
$ymatches[$this->yv[$i]][] = $i;
|
|
indvdum@0
|
252 |
}
|
|
indvdum@0
|
253 |
}
|
|
indvdum@0
|
254 |
|
|
indvdum@0
|
255 |
$this->lcs = 0;
|
|
indvdum@0
|
256 |
$this->seq[0]= $yoff - 1;
|
|
indvdum@0
|
257 |
$this->in_seq = array();
|
|
indvdum@0
|
258 |
$ymids[0] = array();
|
|
indvdum@0
|
259 |
|
|
indvdum@0
|
260 |
$numer = $xlim - $xoff + $nchunks - 1;
|
|
indvdum@0
|
261 |
$x = $xoff;
|
|
indvdum@0
|
262 |
|
|
indvdum@0
|
263 |
for ($chunk = 0; $chunk < $nchunks; $chunk++)
|
|
indvdum@0
|
264 |
{
|
|
indvdum@0
|
265 |
if ($chunk > 0)
|
|
indvdum@0
|
266 |
{
|
|
indvdum@0
|
267 |
for ($i = 0; $i <= $this->lcs; $i++)
|
|
indvdum@0
|
268 |
{
|
|
indvdum@0
|
269 |
$ymids[$i][$chunk - 1] = $this->seq[$i];
|
|
indvdum@0
|
270 |
}
|
|
indvdum@0
|
271 |
}
|
|
indvdum@0
|
272 |
|
|
indvdum@0
|
273 |
$x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $chunk) / $nchunks);
|
|
indvdum@0
|
274 |
|
|
indvdum@0
|
275 |
for (; $x < $x1; $x++)
|
|
indvdum@0
|
276 |
{
|
|
indvdum@0
|
277 |
$line = $flip ? $this->yv[$x] : $this->xv[$x];
|
|
indvdum@0
|
278 |
if (empty($ymatches[$line]))
|
|
indvdum@0
|
279 |
{
|
|
indvdum@0
|
280 |
continue;
|
|
indvdum@0
|
281 |
}
|
|
indvdum@0
|
282 |
$matches = $ymatches[$line];
|
|
indvdum@0
|
283 |
|
|
indvdum@0
|
284 |
reset($matches);
|
|
indvdum@0
|
285 |
while (list(, $y) = each($matches))
|
|
indvdum@0
|
286 |
{
|
|
indvdum@0
|
287 |
if (empty($this->in_seq[$y]))
|
|
indvdum@0
|
288 |
{
|
|
indvdum@0
|
289 |
$k = $this->_lcs_pos($y);
|
|
indvdum@0
|
290 |
$ymids[$k] = $ymids[$k - 1];
|
|
indvdum@0
|
291 |
break;
|
|
indvdum@0
|
292 |
}
|
|
indvdum@0
|
293 |
}
|
|
indvdum@0
|
294 |
|
|
indvdum@0
|
295 |
// no reset() here
|
|
indvdum@0
|
296 |
while (list(, $y) = each($matches))
|
|
indvdum@0
|
297 |
{
|
|
indvdum@0
|
298 |
if ($y > $this->seq[$k - 1])
|
|
indvdum@0
|
299 |
{
|
|
indvdum@0
|
300 |
// Optimization: this is a common case: next match is just replacing previous match.
|
|
indvdum@0
|
301 |
$this->in_seq[$this->seq[$k]] = false;
|
|
indvdum@0
|
302 |
$this->seq[$k] = $y;
|
|
indvdum@0
|
303 |
$this->in_seq[$y] = 1;
|
|
indvdum@0
|
304 |
}
|
|
indvdum@0
|
305 |
else if (empty($this->in_seq[$y]))
|
|
indvdum@0
|
306 |
{
|
|
indvdum@0
|
307 |
$k = $this->_lcs_pos($y);
|
|
indvdum@0
|
308 |
$ymids[$k] = $ymids[$k - 1];
|
|
indvdum@0
|
309 |
}
|
|
indvdum@0
|
310 |
}
|
|
indvdum@0
|
311 |
}
|
|
indvdum@0
|
312 |
}
|
|
indvdum@0
|
313 |
|
|
indvdum@0
|
314 |
$seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff);
|
|
indvdum@0
|
315 |
$ymid = $ymids[$this->lcs];
|
|
indvdum@0
|
316 |
|
|
indvdum@0
|
317 |
for ($n = 0; $n < $nchunks - 1; $n++)
|
|
indvdum@0
|
318 |
{
|
|
indvdum@0
|
319 |
$x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks);
|
|
indvdum@0
|
320 |
$y1 = $ymid[$n] + 1;
|
|
indvdum@0
|
321 |
$seps[] = $flip ? array($y1, $x1) : array($x1, $y1);
|
|
indvdum@0
|
322 |
}
|
|
indvdum@0
|
323 |
$seps[] = $flip ? array($ylim, $xlim) : array($xlim, $ylim);
|
|
indvdum@0
|
324 |
|
|
indvdum@0
|
325 |
return array($this->lcs, $seps);
|
|
indvdum@0
|
326 |
}
|
|
indvdum@0
|
327 |
|
|
indvdum@0
|
328 |
function _lcs_pos($ypos)
|
|
indvdum@0
|
329 |
{
|
|
indvdum@0
|
330 |
$end = $this->lcs;
|
|
indvdum@0
|
331 |
|
|
indvdum@0
|
332 |
if ($end == 0 || $ypos > $this->seq[$end])
|
|
indvdum@0
|
333 |
{
|
|
indvdum@0
|
334 |
$this->seq[++$this->lcs] = $ypos;
|
|
indvdum@0
|
335 |
$this->in_seq[$ypos] = 1;
|
|
indvdum@0
|
336 |
return $this->lcs;
|
|
indvdum@0
|
337 |
}
|
|
indvdum@0
|
338 |
|
|
indvdum@0
|
339 |
$beg = 1;
|
|
indvdum@0
|
340 |
while ($beg < $end)
|
|
indvdum@0
|
341 |
{
|
|
indvdum@0
|
342 |
$mid = (int)(($beg + $end) / 2);
|
|
indvdum@0
|
343 |
if ($ypos > $this->seq[$mid])
|
|
indvdum@0
|
344 |
{
|
|
indvdum@0
|
345 |
$beg = $mid + 1;
|
|
indvdum@0
|
346 |
}
|
|
indvdum@0
|
347 |
else
|
|
indvdum@0
|
348 |
{
|
|
indvdum@0
|
349 |
$end = $mid;
|
|
indvdum@0
|
350 |
}
|
|
indvdum@0
|
351 |
}
|
|
indvdum@0
|
352 |
|
|
indvdum@0
|
353 |
$this->in_seq[$this->seq[$end]] = false;
|
|
indvdum@0
|
354 |
$this->seq[$end] = $ypos;
|
|
indvdum@0
|
355 |
$this->in_seq[$ypos] = 1;
|
|
indvdum@0
|
356 |
|
|
indvdum@0
|
357 |
return $end;
|
|
indvdum@0
|
358 |
}
|
|
indvdum@0
|
359 |
|
|
indvdum@0
|
360 |
/**
|
|
indvdum@0
|
361 |
* Finds LCS of two sequences.
|
|
indvdum@0
|
362 |
*
|
|
indvdum@0
|
363 |
* The results are recorded in the vectors $this->{x,y}changed[], by
|
|
indvdum@0
|
364 |
* storing a 1 in the element for each line that is an insertion or
|
|
indvdum@0
|
365 |
* deletion (ie. is not in the LCS).
|
|
indvdum@0
|
366 |
*
|
|
indvdum@0
|
367 |
* The subsequence of file 0 is (XOFF, XLIM) and likewise for file 1.
|
|
indvdum@0
|
368 |
*
|
|
indvdum@0
|
369 |
* Note that XLIM, YLIM are exclusive bounds. All line numbers are
|
|
indvdum@0
|
370 |
* origin-0 and discarded lines are not counted.
|
|
indvdum@0
|
371 |
*/
|
|
indvdum@0
|
372 |
function _compareseq($xoff, $xlim, $yoff, $ylim)
|
|
indvdum@0
|
373 |
{
|
|
indvdum@0
|
374 |
// Slide down the bottom initial diagonal.
|
|
indvdum@0
|
375 |
while ($xoff < $xlim && $yoff < $ylim && $this->xv[$xoff] == $this->yv[$yoff])
|
|
indvdum@0
|
376 |
{
|
|
indvdum@0
|
377 |
++$xoff;
|
|
indvdum@0
|
378 |
++$yoff;
|
|
indvdum@0
|
379 |
}
|
|
indvdum@0
|
380 |
|
|
indvdum@0
|
381 |
// Slide up the top initial diagonal.
|
|
indvdum@0
|
382 |
while ($xlim > $xoff && $ylim > $yoff && $this->xv[$xlim - 1] == $this->yv[$ylim - 1])
|
|
indvdum@0
|
383 |
{
|
|
indvdum@0
|
384 |
--$xlim;
|
|
indvdum@0
|
385 |
--$ylim;
|
|
indvdum@0
|
386 |
}
|
|
indvdum@0
|
387 |
|
|
indvdum@0
|
388 |
if ($xoff == $xlim || $yoff == $ylim)
|
|
indvdum@0
|
389 |
{
|
|
indvdum@0
|
390 |
$lcs = 0;
|
|
indvdum@0
|
391 |
}
|
|
indvdum@0
|
392 |
else
|
|
indvdum@0
|
393 |
{
|
|
indvdum@0
|
394 |
// This is ad hoc but seems to work well.
|
|
indvdum@0
|
395 |
// $nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
|
|
indvdum@0
|
396 |
// $nchunks = max(2,min(8,(int)$nchunks));
|
|
indvdum@0
|
397 |
$nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1;
|
|
indvdum@0
|
398 |
list($lcs, $seps) = $this->_diag($xoff, $xlim, $yoff, $ylim, $nchunks);
|
|
indvdum@0
|
399 |
}
|
|
indvdum@0
|
400 |
|
|
indvdum@0
|
401 |
if ($lcs == 0)
|
|
indvdum@0
|
402 |
{
|
|
indvdum@0
|
403 |
// X and Y sequences have no common subsequence: mark all changed.
|
|
indvdum@0
|
404 |
while ($yoff < $ylim)
|
|
indvdum@0
|
405 |
{
|
|
indvdum@0
|
406 |
$this->ychanged[$this->yind[$yoff++]] = 1;
|
|
indvdum@0
|
407 |
}
|
|
indvdum@0
|
408 |
|
|
indvdum@0
|
409 |
while ($xoff < $xlim)
|
|
indvdum@0
|
410 |
{
|
|
indvdum@0
|
411 |
$this->xchanged[$this->xind[$xoff++]] = 1;
|
|
indvdum@0
|
412 |
}
|
|
indvdum@0
|
413 |
}
|
|
indvdum@0
|
414 |
else
|
|
indvdum@0
|
415 |
{
|
|
indvdum@0
|
416 |
// Use the partitions to split this problem into subproblems.
|
|
indvdum@0
|
417 |
reset($seps);
|
|
indvdum@0
|
418 |
$pt1 = $seps[0];
|
|
indvdum@0
|
419 |
|
|
indvdum@0
|
420 |
while ($pt2 = next($seps))
|
|
indvdum@0
|
421 |
{
|
|
indvdum@0
|
422 |
$this->_compareseq($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
|
|
indvdum@0
|
423 |
$pt1 = $pt2;
|
|
indvdum@0
|
424 |
}
|
|
indvdum@0
|
425 |
}
|
|
indvdum@0
|
426 |
}
|
|
indvdum@0
|
427 |
|
|
indvdum@0
|
428 |
/**
|
|
indvdum@0
|
429 |
* Adjusts inserts/deletes of identical lines to join changes as much as possible.
|
|
indvdum@0
|
430 |
*
|
|
indvdum@0
|
431 |
* We do something when a run of changed lines include a line at one end
|
|
indvdum@0
|
432 |
* and has an excluded, identical line at the other. We are free to
|
|
indvdum@0
|
433 |
* choose which identical line is included. 'compareseq' usually chooses
|
|
indvdum@0
|
434 |
* the one at the beginning, but usually it is cleaner to consider the
|
|
indvdum@0
|
435 |
* following identical line to be the "change".
|
|
indvdum@0
|
436 |
*
|
|
indvdum@0
|
437 |
* This is extracted verbatim from analyze.c (GNU diffutils-2.7).
|
|
indvdum@0
|
438 |
*/
|
|
indvdum@0
|
439 |
function _shift_boundaries($lines, &$changed, $other_changed)
|
|
indvdum@0
|
440 |
{
|
|
indvdum@0
|
441 |
$i = 0;
|
|
indvdum@0
|
442 |
$j = 0;
|
|
indvdum@0
|
443 |
|
|
indvdum@0
|
444 |
$len = sizeof($lines);
|
|
indvdum@0
|
445 |
$other_len = sizeof($other_changed);
|
|
indvdum@0
|
446 |
|
|
indvdum@0
|
447 |
while (1)
|
|
indvdum@0
|
448 |
{
|
|
indvdum@0
|
449 |
// Scan forward to find the beginning of another run of
|
|
indvdum@0
|
450 |
// changes. Also keep track of the corresponding point in the other file.
|
|
indvdum@0
|
451 |
//
|
|
indvdum@0
|
452 |
// Throughout this code, $i and $j are adjusted together so that
|
|
indvdum@0
|
453 |
// the first $i elements of $changed and the first $j elements of
|
|
indvdum@0
|
454 |
// $other_changed both contain the same number of zeros (unchanged lines).
|
|
indvdum@0
|
455 |
//
|
|
indvdum@0
|
456 |
// Furthermore, $j is always kept so that $j == $other_len or $other_changed[$j] == false.
|
|
indvdum@0
|
457 |
while ($j < $other_len && $other_changed[$j])
|
|
indvdum@0
|
458 |
{
|
|
indvdum@0
|
459 |
$j++;
|
|
indvdum@0
|
460 |
}
|
|
indvdum@0
|
461 |
|
|
indvdum@0
|
462 |
while ($i < $len && ! $changed[$i])
|
|
indvdum@0
|
463 |
{
|
|
indvdum@0
|
464 |
$i++;
|
|
indvdum@0
|
465 |
$j++;
|
|
indvdum@0
|
466 |
|
|
indvdum@0
|
467 |
while ($j < $other_len && $other_changed[$j])
|
|
indvdum@0
|
468 |
{
|
|
indvdum@0
|
469 |
$j++;
|
|
indvdum@0
|
470 |
}
|
|
indvdum@0
|
471 |
}
|
|
indvdum@0
|
472 |
|
|
indvdum@0
|
473 |
if ($i == $len)
|
|
indvdum@0
|
474 |
{
|
|
indvdum@0
|
475 |
break;
|
|
indvdum@0
|
476 |
}
|
|
indvdum@0
|
477 |
|
|
indvdum@0
|
478 |
$start = $i;
|
|
indvdum@0
|
479 |
|
|
indvdum@0
|
480 |
// Find the end of this run of changes.
|
|
indvdum@0
|
481 |
while (++$i < $len && $changed[$i])
|
|
indvdum@0
|
482 |
{
|
|
indvdum@0
|
483 |
continue;
|
|
indvdum@0
|
484 |
}
|
|
indvdum@0
|
485 |
|
|
indvdum@0
|
486 |
do
|
|
indvdum@0
|
487 |
{
|
|
indvdum@0
|
488 |
// Record the length of this run of changes, so that we can later determine whether the run has grown.
|
|
indvdum@0
|
489 |
$runlength = $i - $start;
|
|
indvdum@0
|
490 |
|
|
indvdum@0
|
491 |
// Move the changed region back, so long as the previous unchanged line matches the last changed one.
|
|
indvdum@0
|
492 |
// This merges with previous changed regions.
|
|
indvdum@0
|
493 |
while ($start > 0 && $lines[$start - 1] == $lines[$i - 1])
|
|
indvdum@0
|
494 |
{
|
|
indvdum@0
|
495 |
$changed[--$start] = 1;
|
|
indvdum@0
|
496 |
$changed[--$i] = false;
|
|
indvdum@0
|
497 |
|
|
indvdum@0
|
498 |
while ($start > 0 && $changed[$start - 1])
|
|
indvdum@0
|
499 |
{
|
|
indvdum@0
|
500 |
$start--;
|
|
indvdum@0
|
501 |
}
|
|
indvdum@0
|
502 |
|
|
indvdum@0
|
503 |
while ($other_changed[--$j])
|
|
indvdum@0
|
504 |
{
|
|
indvdum@0
|
505 |
continue;
|
|
indvdum@0
|
506 |
}
|
|
indvdum@0
|
507 |
}
|
|
indvdum@0
|
508 |
|
|
indvdum@0
|
509 |
// Set CORRESPONDING to the end of the changed run, at the last point where it corresponds to a changed run in the
|
|
indvdum@0
|
510 |
// other file. CORRESPONDING == LEN means no such point has been found.
|
|
indvdum@0
|
511 |
$corresponding = $j < $other_len ? $i : $len;
|
|
indvdum@0
|
512 |
|
|
indvdum@0
|
513 |
// Move the changed region forward, so long as the first changed line matches the following unchanged one.
|
|
indvdum@0
|
514 |
// This merges with following changed regions.
|
|
indvdum@0
|
515 |
// Do this second, so that if there are no merges, the changed region is moved forward as far as possible.
|
|
indvdum@0
|
516 |
while ($i < $len && $lines[$start] == $lines[$i])
|
|
indvdum@0
|
517 |
{
|
|
indvdum@0
|
518 |
$changed[$start++] = false;
|
|
indvdum@0
|
519 |
$changed[$i++] = 1;
|
|
indvdum@0
|
520 |
|
|
indvdum@0
|
521 |
while ($i < $len && $changed[$i])
|
|
indvdum@0
|
522 |
{
|
|
indvdum@0
|
523 |
$i++;
|
|
indvdum@0
|
524 |
}
|
|
indvdum@0
|
525 |
|
|
indvdum@0
|
526 |
$j++;
|
|
indvdum@0
|
527 |
if ($j < $other_len && $other_changed[$j])
|
|
indvdum@0
|
528 |
{
|
|
indvdum@0
|
529 |
$corresponding = $i;
|
|
indvdum@0
|
530 |
while ($j < $other_len && $other_changed[$j])
|
|
indvdum@0
|
531 |
{
|
|
indvdum@0
|
532 |
$j++;
|
|
indvdum@0
|
533 |
}
|
|
indvdum@0
|
534 |
}
|
|
indvdum@0
|
535 |
}
|
|
indvdum@0
|
536 |
}
|
|
indvdum@0
|
537 |
while ($runlength != $i - $start);
|
|
indvdum@0
|
538 |
|
|
indvdum@0
|
539 |
// If possible, move the fully-merged run of changes back to a corresponding run in the other file.
|
|
indvdum@0
|
540 |
while ($corresponding < $i)
|
|
indvdum@0
|
541 |
{
|
|
indvdum@0
|
542 |
$changed[--$start] = 1;
|
|
indvdum@0
|
543 |
$changed[--$i] = 0;
|
|
indvdum@0
|
544 |
|
|
indvdum@0
|
545 |
while ($other_changed[--$j])
|
|
indvdum@0
|
546 |
{
|
|
indvdum@0
|
547 |
continue;
|
|
indvdum@0
|
548 |
}
|
|
indvdum@0
|
549 |
}
|
|
indvdum@0
|
550 |
}
|
|
indvdum@0
|
551 |
}
|
|
indvdum@0
|
552 |
}
|
|
indvdum@0
|
553 |
|
|
indvdum@0
|
554 |
?> |