| | } |
| | |
| | - private function buildFallbackDiff( |
| | - array $old, |
| | - array $new, |
| | - int $offset |
| | - ): array { |
| | - $stream = []; |
| | - $currO = $offset + 1; |
| | - $currN = $offset + 1; |
| | - |
| | - foreach( $old as $line ) { |
| | - $stream[] = [ 't' => '-', 'l' => $line, 'no' => $currO++, 'nn' => null ]; |
| | - } |
| | - |
| | - foreach( $new as $line ) { |
| | - $stream[] = [ 't' => '+', 'l' => $line, 'no' => null, 'nn' => $currN++ ]; |
| | - } |
| | - |
| | - return $stream; |
| | - } |
| | - |
| | - private function buildDiffStream( array $ops, int $start ): array { |
| | - $stream = []; |
| | - $currO = $start + 1; |
| | - $currN = $start + 1; |
| | - |
| | - foreach( $ops as $op ) { |
| | - $stream[] = [ |
| | - 't' => $op['t'], |
| | - 'l' => $op['l'], |
| | - 'no' => $op['t'] === '+' ? null : $currO++, |
| | - 'nn' => $op['t'] === '-' ? null : $currN++ |
| | - ]; |
| | - } |
| | - |
| | - return $stream; |
| | - } |
| | - |
| | -private function calculateDiff( string $old, string $new ): array { |
| | - $oldLines = explode( "\n", str_replace( "\r\n", "\n", $old ) ); |
| | - $newLines = explode( "\n", str_replace( "\r\n", "\n", $new ) ); |
| | - $m = count( $oldLines ); |
| | - $n = count( $newLines ); |
| | - $start = 0; |
| | - $end = 0; |
| | - |
| | - while( $start < $m && $start < $n && |
| | - $oldLines[$start] === $newLines[$start] ) { |
| | - $start++; |
| | - } |
| | - |
| | - while( $m - $end > $start && $n - $end > $start && |
| | - $oldLines[$m - 1 - $end] === $newLines[$n - 1 - $end] ) { |
| | - $end++; |
| | - } |
| | - |
| | - $oldSlice = array_slice( $oldLines, $start, $m - $start - $end ); |
| | - $newSlice = array_slice( $newLines, $start, $n - $start - $end ); |
| | - $limit = 100000; |
| | - $stream = []; |
| | - |
| | - for( $i = 0; $i < $start; $i++ ) { |
| | - $stream[] = [ |
| | - 't' => ' ', |
| | - 'l' => $oldLines[$i], |
| | - 'no' => $i + 1, |
| | - 'nn' => $i + 1 |
| | - ]; |
| | - } |
| | - |
| | - $mid = []; |
| | - |
| | - if( (count( $oldSlice ) * count( $newSlice )) > $limit ) { |
| | - $mid = $this->buildFallbackDiff( $oldSlice, $newSlice, $start ); |
| | - } else { |
| | - $ops = $this->computeLCS( $oldSlice, $newSlice ); |
| | - $mid = $this->buildDiffStream( $ops, $start ); |
| | - } |
| | - |
| | - foreach( $mid as $line ) { |
| | - $stream[] = $line; |
| | - } |
| | - |
| | - for( $i = 0; $i < $end; $i++ ) { |
| | - $idxO = $m - $end + $i; |
| | - $idxN = $n - $end + $i; |
| | - $stream[] = [ |
| | - 't' => ' ', |
| | - 'l' => $oldLines[$idxO], |
| | - 'no' => $idxO + 1, |
| | - 'nn' => $idxN + 1 |
| | - ]; |
| | - } |
| | - |
| | - return $this->formatDiffOutput( $stream ); |
| | - } |
| | - |
| | private function formatDiffOutput( array $stream ): array { |
| | $n = count( $stream ); |
 |
| | |
| | return $result; |
| | + } |
| | + |
| | + private function buildFallbackDiff( |
| | + array $old, |
| | + array $new, |
| | + int $offset |
| | + ): array { |
| | + $stream = []; |
| | + $currO = $offset + 1; |
| | + $currN = $offset + 1; |
| | + |
| | + foreach( $old as $line ) { |
| | + $stream[] = [ |
| | + 't' => '-', |
| | + 'l' => $line, |
| | + 'no' => $currO++, |
| | + 'nn' => null |
| | + ]; |
| | + } |
| | + |
| | + foreach( $new as $line ) { |
| | + $stream[] = [ |
| | + 't' => '+', |
| | + 'l' => $line, |
| | + 'no' => null, |
| | + 'nn' => $currN++ |
| | + ]; |
| | + } |
| | + |
| | + return $stream; |
| | + } |
| | + |
| | + private function buildDiffStream( array $ops, int $start ): array { |
| | + $stream = []; |
| | + $currO = $start + 1; |
| | + $currN = $start + 1; |
| | + |
| | + foreach( $ops as $op ) { |
| | + $stream[] = [ |
| | + 't' => $op['t'], |
| | + 'l' => $op['l'], |
| | + 'no' => $op['t'] === '+' ? null : $currO++, |
| | + 'nn' => $op['t'] === '-' ? null : $currN++ |
| | + ]; |
| | + } |
| | + |
| | + return $stream; |
| | } |
| | |