Eloquent\Phony\Difference\DifferenceSequenceMatcher::findLongestMatch PHP Method

findLongestMatch() private method

Essentially, of all of the maximal matching blocks, return the one that startest earliest in $a, and all of those maximal matching blocks that start earliest in $a, return the one that starts earliest in $b. If the junk callback is defined, do the above but with the restriction that the junk element appears in the block. Extend it as far as possible by matching only junk elements in both $a and $b.
private findLongestMatch ( integer $alo, integer $ahi, integer $blo, integer $bhi ) : array
$alo integer The lower constraint for the first sequence.
$ahi integer The upper constraint for the first sequence.
$blo integer The lower constraint for the second sequence.
$bhi integer The upper constraint for the second sequence.
return array Array containing the longest match that includes the starting position in $a, start in $b and the length/size.
    private function findLongestMatch($alo, $ahi, $blo, $bhi)
    {
        $a = $this->a;
        $bestI = $alo;
        $bestJ = $blo;
        $bestSize = 0;
        $j2Len = array();
        $nothing = array();
        for ($i = $alo; $i < $ahi; ++$i) {
            $newJ2Len = array();
            if (isset($this->b2j[$a[$i]])) {
                $jDict = $this->b2j[$a[$i]];
            } else {
                $jDict = $nothing;
            }
            foreach ($jDict as $j) {
                if ($j < $blo) {
                    continue;
                }
                if ($j >= $bhi) {
                    break;
                }
                if (isset($j2Len[$j - 1])) {
                    $k = $j2Len[$j - 1] + 1;
                } else {
                    $k = 1;
                }
                $newJ2Len[$j] = $k;
                if ($k > $bestSize) {
                    $bestI = $i - $k + 1;
                    $bestJ = $j - $k + 1;
                    $bestSize = $k;
                }
            }
            $j2Len = $newJ2Len;
        }
        while ($bestI > $alo && $bestJ > $blo && $this->a[$bestI - 1] === $this->b[$bestJ - 1]) {
            --$bestI;
            --$bestJ;
            ++$bestSize;
        }
        while ($bestI + $bestSize < $ahi && $bestJ + $bestSize < $bhi && $this->a[$bestI + $bestSize] === $this->b[$bestJ + $bestSize]) {
            ++$bestSize;
        }
        while ($bestI > $alo && $bestJ > $blo && $this->a[$bestI - 1] === $this->b[$bestJ - 1]) {
            --$bestI;
            --$bestJ;
            ++$bestSize;
        }
        while ($bestI + $bestSize < $ahi && $bestJ + $bestSize < $bhi && $this->a[$bestI + $bestSize] === $this->b[$bestJ + $bestSize]) {
            ++$bestSize;
        }
        return array($bestI, $bestJ, $bestSize);
    }