commitall
This commit is contained in:
103
hostel/vendor/sebastian/diff/src/Chunk.php
vendored
Normal file
103
hostel/vendor/sebastian/diff/src/Chunk.php
vendored
Normal file
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
class Chunk
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $start;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $startRange;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $end;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $endRange;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $lines;
|
||||
|
||||
/**
|
||||
* @param int $start
|
||||
* @param int $startRange
|
||||
* @param int $end
|
||||
* @param int $endRange
|
||||
* @param array $lines
|
||||
*/
|
||||
public function __construct($start = 0, $startRange = 1, $end = 0, $endRange = 1, array $lines = array())
|
||||
{
|
||||
$this->start = (int) $start;
|
||||
$this->startRange = (int) $startRange;
|
||||
$this->end = (int) $end;
|
||||
$this->endRange = (int) $endRange;
|
||||
$this->lines = $lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStart()
|
||||
{
|
||||
return $this->start;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getStartRange()
|
||||
{
|
||||
return $this->startRange;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getEnd()
|
||||
{
|
||||
return $this->end;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getEndRange()
|
||||
{
|
||||
return $this->endRange;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getLines()
|
||||
{
|
||||
return $this->lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $lines
|
||||
*/
|
||||
public function setLines(array $lines)
|
||||
{
|
||||
$this->lines = $lines;
|
||||
}
|
||||
}
|
73
hostel/vendor/sebastian/diff/src/Diff.php
vendored
Normal file
73
hostel/vendor/sebastian/diff/src/Diff.php
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
class Diff
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $from;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $to;
|
||||
|
||||
/**
|
||||
* @var Chunk[]
|
||||
*/
|
||||
private $chunks;
|
||||
|
||||
/**
|
||||
* @param string $from
|
||||
* @param string $to
|
||||
* @param Chunk[] $chunks
|
||||
*/
|
||||
public function __construct($from, $to, array $chunks = array())
|
||||
{
|
||||
$this->from = $from;
|
||||
$this->to = $to;
|
||||
$this->chunks = $chunks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFrom()
|
||||
{
|
||||
return $this->from;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTo()
|
||||
{
|
||||
return $this->to;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Chunk[]
|
||||
*/
|
||||
public function getChunks()
|
||||
{
|
||||
return $this->chunks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Chunk[] $chunks
|
||||
*/
|
||||
public function setChunks(array $chunks)
|
||||
{
|
||||
$this->chunks = $chunks;
|
||||
}
|
||||
}
|
399
hostel/vendor/sebastian/diff/src/Differ.php
vendored
Normal file
399
hostel/vendor/sebastian/diff/src/Differ.php
vendored
Normal file
@ -0,0 +1,399 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
use SebastianBergmann\Diff\LCS\LongestCommonSubsequence;
|
||||
use SebastianBergmann\Diff\LCS\TimeEfficientImplementation;
|
||||
use SebastianBergmann\Diff\LCS\MemoryEfficientImplementation;
|
||||
|
||||
/**
|
||||
* Diff implementation.
|
||||
*/
|
||||
class Differ
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $header;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $showNonDiffLines;
|
||||
|
||||
/**
|
||||
* @param string $header
|
||||
* @param bool $showNonDiffLines
|
||||
*/
|
||||
public function __construct($header = "--- Original\n+++ New\n", $showNonDiffLines = true)
|
||||
{
|
||||
$this->header = $header;
|
||||
$this->showNonDiffLines = $showNonDiffLines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the diff between two arrays or strings as string.
|
||||
*
|
||||
* @param array|string $from
|
||||
* @param array|string $to
|
||||
* @param LongestCommonSubsequence $lcs
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function diff($from, $to, LongestCommonSubsequence $lcs = null)
|
||||
{
|
||||
$from = $this->validateDiffInput($from);
|
||||
$to = $this->validateDiffInput($to);
|
||||
$diff = $this->diffToArray($from, $to, $lcs);
|
||||
$old = $this->checkIfDiffInOld($diff);
|
||||
$start = isset($old[0]) ? $old[0] : 0;
|
||||
$end = \count($diff);
|
||||
|
||||
if ($tmp = \array_search($end, $old)) {
|
||||
$end = $tmp;
|
||||
}
|
||||
|
||||
return $this->getBuffer($diff, $old, $start, $end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts variable to string if it is not a string or array.
|
||||
*
|
||||
* @param mixed $input
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function validateDiffInput($input)
|
||||
{
|
||||
if (!\is_array($input) && !\is_string($input)) {
|
||||
return (string) $input;
|
||||
}
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes input of the diff array and returns the old array.
|
||||
* Iterates through diff line by line,
|
||||
*
|
||||
* @param array $diff
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function checkIfDiffInOld(array $diff)
|
||||
{
|
||||
$inOld = false;
|
||||
$i = 0;
|
||||
$old = array();
|
||||
|
||||
foreach ($diff as $line) {
|
||||
if ($line[1] === 0 /* OLD */) {
|
||||
if ($inOld === false) {
|
||||
$inOld = $i;
|
||||
}
|
||||
} elseif ($inOld !== false) {
|
||||
if (($i - $inOld) > 5) {
|
||||
$old[$inOld] = $i - 1;
|
||||
}
|
||||
|
||||
$inOld = false;
|
||||
}
|
||||
|
||||
++$i;
|
||||
}
|
||||
|
||||
return $old;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates buffer in string format, returning the patch.
|
||||
*
|
||||
* @param array $diff
|
||||
* @param array $old
|
||||
* @param int $start
|
||||
* @param int $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getBuffer(array $diff, array $old, $start, $end)
|
||||
{
|
||||
$buffer = $this->header;
|
||||
|
||||
if (!isset($old[$start])) {
|
||||
$buffer = $this->getDiffBufferElementNew($diff, $buffer, $start);
|
||||
++$start;
|
||||
}
|
||||
|
||||
for ($i = $start; $i < $end; $i++) {
|
||||
if (isset($old[$i])) {
|
||||
$i = $old[$i];
|
||||
$buffer = $this->getDiffBufferElementNew($diff, $buffer, $i);
|
||||
} else {
|
||||
$buffer = $this->getDiffBufferElement($diff, $buffer, $i);
|
||||
}
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets individual buffer element.
|
||||
*
|
||||
* @param array $diff
|
||||
* @param string $buffer
|
||||
* @param int $diffIndex
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getDiffBufferElement(array $diff, $buffer, $diffIndex)
|
||||
{
|
||||
if ($diff[$diffIndex][1] === 1 /* ADDED */) {
|
||||
$buffer .= '+' . $diff[$diffIndex][0] . "\n";
|
||||
} elseif ($diff[$diffIndex][1] === 2 /* REMOVED */) {
|
||||
$buffer .= '-' . $diff[$diffIndex][0] . "\n";
|
||||
} elseif ($this->showNonDiffLines === true) {
|
||||
$buffer .= ' ' . $diff[$diffIndex][0] . "\n";
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets individual buffer element with opening.
|
||||
*
|
||||
* @param array $diff
|
||||
* @param string $buffer
|
||||
* @param int $diffIndex
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getDiffBufferElementNew(array $diff, $buffer, $diffIndex)
|
||||
{
|
||||
if ($this->showNonDiffLines === true) {
|
||||
$buffer .= "@@ @@\n";
|
||||
}
|
||||
|
||||
return $this->getDiffBufferElement($diff, $buffer, $diffIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the diff between two arrays or strings as array.
|
||||
*
|
||||
* Each array element contains two elements:
|
||||
* - [0] => mixed $token
|
||||
* - [1] => 2|1|0
|
||||
*
|
||||
* - 2: REMOVED: $token was removed from $from
|
||||
* - 1: ADDED: $token was added to $from
|
||||
* - 0: OLD: $token is not changed in $to
|
||||
*
|
||||
* @param array|string $from
|
||||
* @param array|string $to
|
||||
* @param LongestCommonSubsequence $lcs
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function diffToArray($from, $to, LongestCommonSubsequence $lcs = null)
|
||||
{
|
||||
if (\is_string($from)) {
|
||||
$fromMatches = $this->getNewLineMatches($from);
|
||||
$from = $this->splitStringByLines($from);
|
||||
} elseif (\is_array($from)) {
|
||||
$fromMatches = array();
|
||||
} else {
|
||||
throw new \InvalidArgumentException('"from" must be an array or string.');
|
||||
}
|
||||
|
||||
if (\is_string($to)) {
|
||||
$toMatches = $this->getNewLineMatches($to);
|
||||
$to = $this->splitStringByLines($to);
|
||||
} elseif (\is_array($to)) {
|
||||
$toMatches = array();
|
||||
} else {
|
||||
throw new \InvalidArgumentException('"to" must be an array or string.');
|
||||
}
|
||||
|
||||
list($from, $to, $start, $end) = self::getArrayDiffParted($from, $to);
|
||||
|
||||
if ($lcs === null) {
|
||||
$lcs = $this->selectLcsImplementation($from, $to);
|
||||
}
|
||||
|
||||
$common = $lcs->calculate(\array_values($from), \array_values($to));
|
||||
$diff = array();
|
||||
|
||||
if ($this->detectUnmatchedLineEndings($fromMatches, $toMatches)) {
|
||||
$diff[] = array(
|
||||
'#Warning: Strings contain different line endings!',
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($start as $token) {
|
||||
$diff[] = array($token, 0 /* OLD */);
|
||||
}
|
||||
|
||||
\reset($from);
|
||||
\reset($to);
|
||||
|
||||
foreach ($common as $token) {
|
||||
while (($fromToken = \reset($from)) !== $token) {
|
||||
$diff[] = array(\array_shift($from), 2 /* REMOVED */);
|
||||
}
|
||||
|
||||
while (($toToken = \reset($to)) !== $token) {
|
||||
$diff[] = array(\array_shift($to), 1 /* ADDED */);
|
||||
}
|
||||
|
||||
$diff[] = array($token, 0 /* OLD */);
|
||||
|
||||
\array_shift($from);
|
||||
\array_shift($to);
|
||||
}
|
||||
|
||||
while (($token = \array_shift($from)) !== null) {
|
||||
$diff[] = array($token, 2 /* REMOVED */);
|
||||
}
|
||||
|
||||
while (($token = \array_shift($to)) !== null) {
|
||||
$diff[] = array($token, 1 /* ADDED */);
|
||||
}
|
||||
|
||||
foreach ($end as $token) {
|
||||
$diff[] = array($token, 0 /* OLD */);
|
||||
}
|
||||
|
||||
return $diff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get new strings denoting new lines from a given string.
|
||||
*
|
||||
* @param string $string
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getNewLineMatches($string)
|
||||
{
|
||||
\preg_match_all('(\r\n|\r|\n)', $string, $stringMatches);
|
||||
|
||||
return $stringMatches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if input is string, if so it will split it line-by-line.
|
||||
*
|
||||
* @param string $input
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function splitStringByLines($input)
|
||||
{
|
||||
return \preg_split('(\r\n|\r|\n)', $input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $from
|
||||
* @param array $to
|
||||
*
|
||||
* @return LongestCommonSubsequence
|
||||
*/
|
||||
private function selectLcsImplementation(array $from, array $to)
|
||||
{
|
||||
// We do not want to use the time-efficient implementation if its memory
|
||||
// footprint will probably exceed this value. Note that the footprint
|
||||
// calculation is only an estimation for the matrix and the LCS method
|
||||
// will typically allocate a bit more memory than this.
|
||||
$memoryLimit = 100 * 1024 * 1024;
|
||||
|
||||
if ($this->calculateEstimatedFootprint($from, $to) > $memoryLimit) {
|
||||
return new MemoryEfficientImplementation;
|
||||
}
|
||||
|
||||
return new TimeEfficientImplementation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the estimated memory footprint for the DP-based method.
|
||||
*
|
||||
* @param array $from
|
||||
* @param array $to
|
||||
*
|
||||
* @return int|float
|
||||
*/
|
||||
private function calculateEstimatedFootprint(array $from, array $to)
|
||||
{
|
||||
$itemSize = PHP_INT_SIZE === 4 ? 76 : 144;
|
||||
|
||||
return $itemSize * \pow(\min(\count($from), \count($to)), 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if line ends don't match on fromMatches and toMatches.
|
||||
*
|
||||
* @param array $fromMatches
|
||||
* @param array $toMatches
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function detectUnmatchedLineEndings(array $fromMatches, array $toMatches)
|
||||
{
|
||||
return isset($fromMatches[0], $toMatches[0]) &&
|
||||
\count($fromMatches[0]) === \count($toMatches[0]) &&
|
||||
$fromMatches[0] !== $toMatches[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $from
|
||||
* @param array $to
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function getArrayDiffParted(array &$from, array &$to)
|
||||
{
|
||||
$start = array();
|
||||
$end = array();
|
||||
|
||||
\reset($to);
|
||||
|
||||
foreach ($from as $k => $v) {
|
||||
$toK = \key($to);
|
||||
|
||||
if ($toK === $k && $v === $to[$k]) {
|
||||
$start[$k] = $v;
|
||||
|
||||
unset($from[$k], $to[$k]);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
\end($from);
|
||||
\end($to);
|
||||
|
||||
do {
|
||||
$fromK = \key($from);
|
||||
$toK = \key($to);
|
||||
|
||||
if (null === $fromK || null === $toK || \current($from) !== \current($to)) {
|
||||
break;
|
||||
}
|
||||
|
||||
\prev($from);
|
||||
\prev($to);
|
||||
|
||||
$end = array($fromK => $from[$fromK]) + $end;
|
||||
unset($from[$fromK], $to[$toK]);
|
||||
} while (true);
|
||||
|
||||
return array($from, $to, $start, $end);
|
||||
}
|
||||
}
|
27
hostel/vendor/sebastian/diff/src/LCS/LongestCommonSubsequence.php
vendored
Normal file
27
hostel/vendor/sebastian/diff/src/LCS/LongestCommonSubsequence.php
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace SebastianBergmann\Diff\LCS;
|
||||
|
||||
/**
|
||||
* Interface for implementations of longest common subsequence calculation.
|
||||
*/
|
||||
interface LongestCommonSubsequence
|
||||
{
|
||||
/**
|
||||
* Calculates the longest common subsequence of two arrays.
|
||||
*
|
||||
* @param array $from
|
||||
* @param array $to
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function calculate(array $from, array $to);
|
||||
}
|
95
hostel/vendor/sebastian/diff/src/LCS/MemoryEfficientLongestCommonSubsequenceImplementation.php
vendored
Normal file
95
hostel/vendor/sebastian/diff/src/LCS/MemoryEfficientLongestCommonSubsequenceImplementation.php
vendored
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace SebastianBergmann\Diff\LCS;
|
||||
|
||||
/**
|
||||
* Memory-efficient implementation of longest common subsequence calculation.
|
||||
*/
|
||||
class MemoryEfficientImplementation implements LongestCommonSubsequence
|
||||
{
|
||||
/**
|
||||
* Calculates the longest common subsequence of two arrays.
|
||||
*
|
||||
* @param array $from
|
||||
* @param array $to
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function calculate(array $from, array $to)
|
||||
{
|
||||
$cFrom = \count($from);
|
||||
$cTo = \count($to);
|
||||
|
||||
if ($cFrom === 0) {
|
||||
return array();
|
||||
}
|
||||
|
||||
if ($cFrom === 1) {
|
||||
if (\in_array($from[0], $to, true)) {
|
||||
return array($from[0]);
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
$i = (int) ($cFrom / 2);
|
||||
$fromStart = \array_slice($from, 0, $i);
|
||||
$fromEnd = \array_slice($from, $i);
|
||||
$llB = $this->length($fromStart, $to);
|
||||
$llE = $this->length(\array_reverse($fromEnd), \array_reverse($to));
|
||||
$jMax = 0;
|
||||
$max = 0;
|
||||
|
||||
for ($j = 0; $j <= $cTo; $j++) {
|
||||
$m = $llB[$j] + $llE[$cTo - $j];
|
||||
|
||||
if ($m >= $max) {
|
||||
$max = $m;
|
||||
$jMax = $j;
|
||||
}
|
||||
}
|
||||
|
||||
$toStart = \array_slice($to, 0, $jMax);
|
||||
$toEnd = \array_slice($to, $jMax);
|
||||
|
||||
return \array_merge(
|
||||
$this->calculate($fromStart, $toStart),
|
||||
$this->calculate($fromEnd, $toEnd)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $from
|
||||
* @param array $to
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function length(array $from, array $to)
|
||||
{
|
||||
$current = \array_fill(0, \count($to) + 1, 0);
|
||||
$cFrom = \count($from);
|
||||
$cTo = \count($to);
|
||||
|
||||
for ($i = 0; $i < $cFrom; $i++) {
|
||||
$prev = $current;
|
||||
|
||||
for ($j = 0; $j < $cTo; $j++) {
|
||||
if ($from[$i] === $to[$j]) {
|
||||
$current[$j + 1] = $prev[$j] + 1;
|
||||
} else {
|
||||
$current[$j + 1] = \max($current[$j], $prev[$j + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $current;
|
||||
}
|
||||
}
|
74
hostel/vendor/sebastian/diff/src/LCS/TimeEfficientLongestCommonSubsequenceImplementation.php
vendored
Normal file
74
hostel/vendor/sebastian/diff/src/LCS/TimeEfficientLongestCommonSubsequenceImplementation.php
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace SebastianBergmann\Diff\LCS;
|
||||
|
||||
/**
|
||||
* Time-efficient implementation of longest common subsequence calculation.
|
||||
*/
|
||||
class TimeEfficientImplementation implements LongestCommonSubsequence
|
||||
{
|
||||
/**
|
||||
* Calculates the longest common subsequence of two arrays.
|
||||
*
|
||||
* @param array $from
|
||||
* @param array $to
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function calculate(array $from, array $to)
|
||||
{
|
||||
$common = array();
|
||||
$fromLength = \count($from);
|
||||
$toLength = \count($to);
|
||||
$width = $fromLength + 1;
|
||||
$matrix = new \SplFixedArray($width * ($toLength + 1));
|
||||
|
||||
for ($i = 0; $i <= $fromLength; ++$i) {
|
||||
$matrix[$i] = 0;
|
||||
}
|
||||
|
||||
for ($j = 0; $j <= $toLength; ++$j) {
|
||||
$matrix[$j * $width] = 0;
|
||||
}
|
||||
|
||||
for ($i = 1; $i <= $fromLength; ++$i) {
|
||||
for ($j = 1; $j <= $toLength; ++$j) {
|
||||
$o = ($j * $width) + $i;
|
||||
$matrix[$o] = \max(
|
||||
$matrix[$o - 1],
|
||||
$matrix[$o - $width],
|
||||
$from[$i - 1] === $to[$j - 1] ? $matrix[$o - $width - 1] + 1 : 0
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$i = $fromLength;
|
||||
$j = $toLength;
|
||||
|
||||
while ($i > 0 && $j > 0) {
|
||||
if ($from[$i - 1] === $to[$j - 1]) {
|
||||
$common[] = $from[$i - 1];
|
||||
--$i;
|
||||
--$j;
|
||||
} else {
|
||||
$o = ($j * $width) + $i;
|
||||
|
||||
if ($matrix[$o - $width] > $matrix[$o - 1]) {
|
||||
--$j;
|
||||
} else {
|
||||
--$i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return \array_reverse($common);
|
||||
}
|
||||
}
|
54
hostel/vendor/sebastian/diff/src/Line.php
vendored
Normal file
54
hostel/vendor/sebastian/diff/src/Line.php
vendored
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
class Line
|
||||
{
|
||||
const ADDED = 1;
|
||||
const REMOVED = 2;
|
||||
const UNCHANGED = 3;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $type;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $content;
|
||||
|
||||
/**
|
||||
* @param int $type
|
||||
* @param string $content
|
||||
*/
|
||||
public function __construct($type = self::UNCHANGED, $content = '')
|
||||
{
|
||||
$this->type = $type;
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContent()
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
}
|
110
hostel/vendor/sebastian/diff/src/Parser.php
vendored
Normal file
110
hostel/vendor/sebastian/diff/src/Parser.php
vendored
Normal file
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of sebastian/diff.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace SebastianBergmann\Diff;
|
||||
|
||||
/**
|
||||
* Unified diff parser.
|
||||
*/
|
||||
class Parser
|
||||
{
|
||||
/**
|
||||
* @param string $string
|
||||
*
|
||||
* @return Diff[]
|
||||
*/
|
||||
public function parse($string)
|
||||
{
|
||||
$lines = \preg_split('(\r\n|\r|\n)', $string);
|
||||
|
||||
if (!empty($lines) && $lines[\count($lines) - 1] == '') {
|
||||
\array_pop($lines);
|
||||
}
|
||||
|
||||
$lineCount = \count($lines);
|
||||
$diffs = array();
|
||||
$diff = null;
|
||||
$collected = array();
|
||||
|
||||
for ($i = 0; $i < $lineCount; ++$i) {
|
||||
if (\preg_match('(^---\\s+(?P<file>\\S+))', $lines[$i], $fromMatch) &&
|
||||
\preg_match('(^\\+\\+\\+\\s+(?P<file>\\S+))', $lines[$i + 1], $toMatch)) {
|
||||
if ($diff !== null) {
|
||||
$this->parseFileDiff($diff, $collected);
|
||||
|
||||
$diffs[] = $diff;
|
||||
$collected = array();
|
||||
}
|
||||
|
||||
$diff = new Diff($fromMatch['file'], $toMatch['file']);
|
||||
|
||||
++$i;
|
||||
} else {
|
||||
if (\preg_match('/^(?:diff --git |index [\da-f\.]+|[+-]{3} [ab])/', $lines[$i])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$collected[] = $lines[$i];
|
||||
}
|
||||
}
|
||||
|
||||
if ($diff !== null && \count($collected)) {
|
||||
$this->parseFileDiff($diff, $collected);
|
||||
|
||||
$diffs[] = $diff;
|
||||
}
|
||||
|
||||
return $diffs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Diff $diff
|
||||
* @param array $lines
|
||||
*/
|
||||
private function parseFileDiff(Diff $diff, array $lines)
|
||||
{
|
||||
$chunks = array();
|
||||
$chunk = null;
|
||||
|
||||
foreach ($lines as $line) {
|
||||
if (\preg_match('/^@@\s+-(?P<start>\d+)(?:,\s*(?P<startrange>\d+))?\s+\+(?P<end>\d+)(?:,\s*(?P<endrange>\d+))?\s+@@/', $line, $match)) {
|
||||
$chunk = new Chunk(
|
||||
$match['start'],
|
||||
isset($match['startrange']) ? \max(1, $match['startrange']) : 1,
|
||||
$match['end'],
|
||||
isset($match['endrange']) ? \max(1, $match['endrange']) : 1
|
||||
);
|
||||
|
||||
$chunks[] = $chunk;
|
||||
$diffLines = array();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (\preg_match('/^(?P<type>[+ -])?(?P<line>.*)/', $line, $match)) {
|
||||
$type = Line::UNCHANGED;
|
||||
|
||||
if ($match['type'] === '+') {
|
||||
$type = Line::ADDED;
|
||||
} elseif ($match['type'] === '-') {
|
||||
$type = Line::REMOVED;
|
||||
}
|
||||
|
||||
$diffLines[] = new Line($type, $match['line']);
|
||||
|
||||
if (null !== $chunk) {
|
||||
$chunk->setLines($diffLines);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$diff->setChunks($chunks);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user