5
5
namespace jblond \Diff ;
6
6
7
7
use InvalidArgumentException ;
8
+ use jblond \Diff \Renderer \SequenceMatcherHelper ;
8
9
9
10
/**
10
11
* Sequence matcher for Diff
20
21
* @version 2.0.0
21
22
* @link https://github.com/JBlond/php-diff
22
23
*/
23
- class SequenceMatcher
24
+ class SequenceMatcher extends SequenceMatcherHelper
24
25
{
25
26
/**
26
27
* @var string|array Either a string or an array containing a callback function to determine
@@ -236,32 +237,32 @@ private function isBJunk(string $bString): bool
236
237
* that the junk element appears in the block. Extend it as far as possible
237
238
* by matching only junk elements in both $a and $b.
238
239
*
239
- * @param int $alo The lower constraint for the first sequence.
240
- * @param int $ahi The upper constraint for the first sequence.
241
- * @param int $blo The lower constraint for the second sequence.
242
- * @param int $bhi The upper constraint for the second sequence.
240
+ * @param int $aLower The lower constraint for the first sequence.
241
+ * @param int $aUpper The upper constraint for the first sequence.
242
+ * @param int $bLower The lower constraint for the second sequence.
243
+ * @param int $bUpper The upper constraint for the second sequence.
243
244
* @return array Array containing the longest match that includes the starting position in $a,
244
245
* start in $b and the length/size.
245
246
*/
246
- public function findLongestMatch (int $ alo , int $ ahi , int $ blo , int $ bhi ): array
247
+ public function findLongestMatch (int $ aLower , int $ aUpper , int $ bLower , int $ bUpper ): array
247
248
{
248
249
$ old = $ this ->old ;
249
250
$ new = $ this ->new ;
250
251
251
- $ bestI = $ alo ;
252
- $ bestJ = $ blo ;
252
+ $ bestI = $ aLower ;
253
+ $ bestJ = $ bLower ;
253
254
$ bestSize = 0 ;
254
255
255
256
$ j2Len = [];
256
257
$ nothing = [];
257
258
258
- for ($ i = $ alo ; $ i < $ ahi ; ++$ i ) {
259
+ for ($ i = $ aLower ; $ i < $ aUpper ; ++$ i ) {
259
260
$ newJ2Len = [];
260
261
$ jDict = $ this ->arrayGetDefault ($ this ->b2j , $ old [$ i ], $ nothing );
261
262
foreach ($ jDict as $ j ) {
262
- if ($ j < $ blo ) {
263
+ if ($ j < $ bLower ) {
263
264
continue ;
264
- } elseif ($ j >= $ bhi ) {
265
+ } elseif ($ j >= $ bUpper ) {
265
266
break ;
266
267
}
267
268
@@ -278,8 +279,8 @@ public function findLongestMatch(int $alo, int $ahi, int $blo, int $bhi): array
278
279
}
279
280
280
281
while (
281
- $ bestI > $ alo &&
282
- $ bestJ > $ blo &&
282
+ $ bestI > $ aLower &&
283
+ $ bestJ > $ bLower &&
283
284
!$ this ->isBJunk ($ new [$ bestJ - 1 ]) &&
284
285
!$ this ->linesAreDifferent ($ bestI - 1 , $ bestJ - 1 )
285
286
) {
@@ -289,17 +290,17 @@ public function findLongestMatch(int $alo, int $ahi, int $blo, int $bhi): array
289
290
}
290
291
291
292
while (
292
- $ bestI + $ bestSize < $ ahi &&
293
- ($ bestJ + $ bestSize ) < $ bhi &&
293
+ $ bestI + $ bestSize < $ aUpper &&
294
+ ($ bestJ + $ bestSize ) < $ bUpper &&
294
295
!$ this ->isBJunk ($ new [$ bestJ + $ bestSize ]) &&
295
296
!$ this ->linesAreDifferent ($ bestI + $ bestSize , $ bestJ + $ bestSize )
296
297
) {
297
298
++$ bestSize ;
298
299
}
299
300
300
301
while (
301
- $ bestI > $ alo &&
302
- $ bestJ > $ blo &&
302
+ $ bestI > $ aLower &&
303
+ $ bestJ > $ bLower &&
303
304
$ this ->isBJunk ($ new [$ bestJ - 1 ]) &&
304
305
!$ this ->linesAreDifferent ($ bestI - 1 , $ bestJ - 1 )
305
306
) {
@@ -309,8 +310,8 @@ public function findLongestMatch(int $alo, int $ahi, int $blo, int $bhi): array
309
310
}
310
311
311
312
while (
312
- $ bestI + $ bestSize < $ ahi &&
313
- $ bestJ + $ bestSize < $ bhi &&
313
+ $ bestI + $ bestSize < $ aUpper &&
314
+ $ bestJ + $ bestSize < $ bUpper &&
314
315
$ this ->isBJunk ($ new [$ bestJ + $ bestSize ]) &&
315
316
!$ this ->linesAreDifferent ($ bestI + $ bestSize , $ bestJ + $ bestSize )
316
317
) {
@@ -384,26 +385,26 @@ public function getMatchingBlocks(): array
384
385
385
386
$ matchingBlocks = [];
386
387
while (!empty ($ queue )) {
387
- [$ alo , $ ahi , $ blo , $ bhi ] = array_pop ($ queue );
388
- $ longestMatch = $ this ->findLongestMatch ($ alo , $ ahi , $ blo , $ bhi );
388
+ [$ aLower , $ aUpper , $ bLower , $ bUpper ] = array_pop ($ queue );
389
+ $ longestMatch = $ this ->findLongestMatch ($ aLower , $ aUpper , $ bLower , $ bUpper );
389
390
[$ list1 , $ list2 , $ list3 ] = $ longestMatch ;
390
391
if ($ list3 ) {
391
392
$ matchingBlocks [] = $ longestMatch ;
392
- if ($ alo < $ list1 && $ blo < $ list2 ) {
393
+ if ($ aLower < $ list1 && $ bLower < $ list2 ) {
393
394
$ queue [] = [
394
- $ alo ,
395
+ $ aLower ,
395
396
$ list1 ,
396
- $ blo ,
397
+ $ bLower ,
397
398
$ list2
398
399
];
399
400
}
400
401
401
- if ($ list1 + $ list3 < $ ahi && $ list2 + $ list3 < $ bhi ) {
402
+ if ($ list1 + $ list3 < $ aUpper && $ list2 + $ list3 < $ bUpper ) {
402
403
$ queue [] = [
403
404
$ list1 + $ list3 ,
404
- $ ahi ,
405
+ $ aUpper ,
405
406
$ list2 + $ list3 ,
406
- $ bhi
407
+ $ bUpper
407
408
];
408
409
}
409
410
}
@@ -614,49 +615,4 @@ public function getGroupedOpCodes(): array
614
615
615
616
return $ groups ;
616
617
}
617
-
618
- /**
619
- * Helper function that provides the ability to return the value for a key
620
- * in an array of it exists, or if it doesn't then return a default value.
621
- * Essentially cleaner than doing a series of if (isset()) {} else {} calls.
622
- *
623
- * @param array $array The array to search.
624
- * @param string|int $key The key to check that exists.
625
- * @param mixed $default The value to return as the default value if the key doesn't exist.
626
- * @return mixed The value from the array if the key exists or otherwise the default.
627
- */
628
- private function arrayGetDefault (array $ array , $ key , $ default )
629
- {
630
- if (isset ($ array [$ key ])) {
631
- return $ array [$ key ];
632
- }
633
- return $ default ;
634
- }
635
-
636
- /**
637
- * Sort an array by the nested arrays it contains. Helper function for getMatchingBlocks
638
- *
639
- * @param array $aArray First array to compare.
640
- * @param array $bArray Second array to compare.
641
- * @return int -1, 0 or 1, as expected by the usort function.
642
- */
643
- private function tupleSort (array $ aArray , array $ bArray ): int
644
- {
645
- $ max = max (count ($ aArray ), count ($ bArray ));
646
- for ($ counter = 0 ; $ counter < $ max ; ++$ counter ) {
647
- if ($ aArray [$ counter ] < $ bArray [$ counter ]) {
648
- return -1 ;
649
- } elseif ($ aArray [$ counter ] > $ bArray [$ counter ]) {
650
- return 1 ;
651
- }
652
- }
653
-
654
- if (count ($ aArray ) == count ($ bArray )) {
655
- return 0 ;
656
- }
657
- if (count ($ aArray ) < count ($ bArray )) {
658
- return -1 ;
659
- }
660
- return 1 ;
661
- }
662
618
}
0 commit comments