Implement lexer class for tokenization and replace all line endings by PHP_EOL
This commit is contained in:
@ -4,5 +4,13 @@ namespace App\Service\SnipContent;
|
||||
|
||||
class Lexer
|
||||
{
|
||||
public static function tokenize(string $text): array {
|
||||
$text = str_replace("\r", '', $text);
|
||||
return explode(PHP_EOL, $text);
|
||||
}
|
||||
|
||||
public static function reconstruct(array $tokens): string
|
||||
{
|
||||
return implode(PHP_EOL, $tokens);
|
||||
}
|
||||
}
|
@ -4,8 +4,6 @@ namespace App\Service\SnipContent;
|
||||
|
||||
class MyersDiff
|
||||
{
|
||||
private const string NEWLINE = "\r\n";
|
||||
|
||||
/**
|
||||
* Backtrack through the intermediate results to extract the "snakes" that
|
||||
* are visited on the chosen "D-path".
|
||||
@ -97,20 +95,18 @@ class MyersDiff
|
||||
public static function calculate(string|array $textFrom, string|array $textTo, ?callable $compare = null): array
|
||||
{
|
||||
if (is_string($textFrom)) {
|
||||
$a = self::explode($textFrom);
|
||||
$a = Lexer::tokenize($textFrom);
|
||||
} else {
|
||||
$a = $textFrom;
|
||||
}
|
||||
if (is_string($textTo)) {
|
||||
$b = self::explode($textTo);
|
||||
$b = Lexer::tokenize($textTo);
|
||||
} else {
|
||||
$b = $textTo;
|
||||
}
|
||||
|
||||
if ($compare === null) {
|
||||
$compare = function ($x, $y) {
|
||||
return $x === $y;
|
||||
};
|
||||
$compare = fn($x, $y) => $x === $y;
|
||||
}
|
||||
|
||||
$n = count($a);
|
||||
@ -147,7 +143,7 @@ class MyersDiff
|
||||
|
||||
public static function rebuildBFromCompact(string $textFrom, array $diff): string
|
||||
{
|
||||
$a = self::explode($textFrom);
|
||||
$a = Lexer::tokenize($textFrom);
|
||||
$b = [];
|
||||
$x = 0;
|
||||
|
||||
@ -171,13 +167,13 @@ class MyersDiff
|
||||
}
|
||||
}
|
||||
|
||||
return self::implode($b);
|
||||
return Lexer::reconstruct($b);
|
||||
}
|
||||
|
||||
public static function buildDiffLines(string $textFrom, string $textTo): array
|
||||
{
|
||||
$a = self::explode($textFrom);
|
||||
$b = self::explode($textTo);
|
||||
$a = Lexer::tokenize($textFrom);
|
||||
$b = Lexer::tokenize($textTo);
|
||||
$diff = MyersDiff::calculate($a, $b);
|
||||
|
||||
$lines = [];
|
||||
@ -223,14 +219,4 @@ class MyersDiff
|
||||
|
||||
return $lines;
|
||||
}
|
||||
|
||||
private static function explode(string $text): array
|
||||
{
|
||||
return explode(self::NEWLINE, $text);
|
||||
}
|
||||
|
||||
private static function implode(array $text): string
|
||||
{
|
||||
return implode(self::NEWLINE, $text);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user