66 *
77 * The algorithm used here is mostly lifted from the perl module
88 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
9- * http ://www.perl.com/CPAN /authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
9+ * https ://cpan.metacpan.org /authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
1010 *
11- * More ideas are taken from: http ://www.ics.uci.edu/~eppstein/161/960229.html
11+ * More ideas are taken from: https ://www.ics.uci.edu/~eppstein/161/960229.html
1212 *
1313 * Some ideas (and a bit of code) are taken from analyze.c, of GNU
1414 * diffutils-2.7, which can be found at:
2020 *
2121 * $Horde: framework/Text_Diff/Diff/Engine/native.php,v 1.7.2.5 2009/01/06 15:23:41 jan Exp $
2222 *
23- * Copyright 2004-2009 The Horde Project (http ://www.horde.org/)
23+ * Copyright 2004-2009 The Horde Project (https ://www.horde.org/)
2424 *
2525 * See the enclosed file COPYING for license information (LGPL). If you did
26- * not receive this file, see http ://opensource.org/licenses /lgpl-license.php .
26+ * not receive this file, see https ://opensource.org/license /lgpl-3-0/ .
2727 *
2828 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
2929 * @package Text_Diff
3030 */
31- class Text_Diff_Engine_native {
31+ class Text_Diff_Engine_native
32+ {
33+
34+ public $ xchanged ;
35+ public $ ychanged ;
36+ public $ xv ;
37+ public $ yv ;
38+ public $ xind ;
39+ public $ yind ;
40+ public $ seq ;
41+ public $ in_seq ;
42+ public $ lcs ;
3243
3344 function diff ($ from_lines , $ to_lines )
3445 {
@@ -63,9 +74,11 @@ function diff($from_lines, $to_lines)
6374 }
6475
6576 // Ignore lines which do not exist in both files.
77+ $ xhash = [];
6678 for ($ xi = $ skip ; $ xi < $ n_from - $ endskip ; $ xi ++) {
6779 $ xhash [$ from_lines [$ xi ]] = 1 ;
6880 }
81+ $ yhash = [];
6982 for ($ yi = $ skip ; $ yi < $ n_to - $ endskip ; $ yi ++) {
7083 $ line = $ to_lines [$ yi ];
7184 if (($ this ->ychanged [$ yi ] = empty ($ xhash [$ line ]))) {
@@ -148,7 +161,7 @@ function diff($from_lines, $to_lines)
148161 * match. The caller must trim matching lines from the beginning and end
149162 * of the portions it is going to specify.
150163 */
151- function _diag ($ xoff , $ xlim , $ yoff , $ ylim , $ nchunks )
164+ function _diag ($ xoff , $ xlim , $ yoff , $ ylim , $ nchunks )
152165 {
153166 $ flip = false ;
154167
@@ -160,6 +173,7 @@ function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks)
160173 = array ($ yoff , $ ylim , $ xoff , $ xlim );
161174 }
162175
176+ $ ymatches = array ();
163177 if ($ flip ) {
164178 for ($ i = $ ylim - 1 ; $ i >= $ yoff ; $ i --) {
165179 $ ymatches [$ this ->xv [$ i ]][] = $ i ;
@@ -173,7 +187,7 @@ function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks)
173187 $ this ->lcs = 0 ;
174188 $ this ->seq [0 ]= $ yoff - 1 ;
175189 $ this ->in_seq = array ();
176- $ ymids[ 0 ] = array ();
190+ $ ymids = array (array () );
177191
178192 $ numer = $ xlim - $ xoff + $ nchunks - 1 ;
179193 $ x = $ xoff ;
@@ -192,15 +206,16 @@ function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks)
192206 }
193207 $ matches = $ ymatches [$ line ];
194208 reset ($ matches );
195- foreach ($ matches as list (, $ y )) {
209+ while ($ y = current ( $ matches )) {
196210 if (empty ($ this ->in_seq [$ y ])) {
197211 $ k = $ this ->_lcsPos ($ y );
198212 assert ($ k > 0 );
199213 $ ymids [$ k ] = $ ymids [$ k - 1 ];
200214 break ;
201215 }
216+ next ($ matches );
202217 }
203- foreach ($ matches as list (, $ y )) {
218+ while ($ y = current ( $ matches )) {
204219 if ($ y > $ this ->seq [$ k - 1 ]) {
205220 assert ($ y <= $ this ->seq [$ k ]);
206221 /* Optimization: this is a common case: next match is
@@ -213,11 +228,12 @@ function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks)
213228 assert ($ k > 0 );
214229 $ ymids [$ k ] = $ ymids [$ k - 1 ];
215230 }
231+ next ($ matches );
216232 }
217233 }
218234 }
219235
220- $ seps[] = $ flip ? array ($ yoff , $ xoff ) : array ($ xoff , $ yoff );
236+ $ seps = array ( $ flip ? array ($ yoff , $ xoff ) : array ($ xoff , $ yoff) );
221237 $ ymid = $ ymids [$ this ->lcs ];
222238 for ($ n = 0 ; $ n < $ nchunks - 1 ; $ n ++) {
223239 $ x1 = $ xoff + (int )(($ numer + ($ xlim - $ xoff ) * $ n ) / $ nchunks );
@@ -268,7 +284,7 @@ function _lcsPos($ypos)
268284 * Note that XLIM, YLIM are exclusive bounds. All line numbers are
269285 * origin-0 and discarded lines are not counted.
270286 */
271- function _compareseq ($ xoff , $ xlim , $ yoff , $ ylim )
287+ function _compareseq ($ xoff , $ xlim , $ yoff , $ ylim )
272288 {
273289 /* Slide down the bottom initial diagonal. */
274290 while ($ xoff < $ xlim && $ yoff < $ ylim
@@ -309,7 +325,7 @@ function _compareseq ($xoff, $xlim, $yoff, $ylim)
309325 reset ($ seps );
310326 $ pt1 = $ seps [0 ];
311327 while ($ pt2 = next ($ seps )) {
312- $ this ->_compareseq ($ pt1 [0 ], $ pt2 [0 ], $ pt1 [1 ], $ pt2 [1 ]);
328+ $ this ->_compareseq ($ pt1 [0 ], $ pt2 [0 ], $ pt1 [1 ], $ pt2 [1 ]);
313329 $ pt1 = $ pt2 ;
314330 }
315331 }
0 commit comments