diff --git a/src/Command/SnipUpdateContentCommand.php b/src/Command/SnipUpdateContentCommand.php new file mode 100644 index 0000000..0f07322 --- /dev/null +++ b/src/Command/SnipUpdateContentCommand.php @@ -0,0 +1,45 @@ +snipContentRepository->createQueryBuilder('s'); + $qb->where('s.text IS NOT NULL'); + + $c = 0; + /** @var SnipContent $snipContent */ + foreach ($qb->getQuery()->getResult() as $snipContent) { + $text = $snipContent->getText(); + $text = Lexer::reconstruct(Lexer::tokenize($text)); + $snipContent->setText($text); + $this->snipContentRepository->save($snipContent); + } + + return Command::SUCCESS; + } +} diff --git a/src/Service/SnipContent/Lexer.php b/src/Service/SnipContent/Lexer.php index e0acbfe..cafecf5 100644 --- a/src/Service/SnipContent/Lexer.php +++ b/src/Service/SnipContent/Lexer.php @@ -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); + } } \ No newline at end of file diff --git a/src/Service/SnipContent/MyersDiff.php b/src/Service/SnipContent/MyersDiff.php index 5f52d3c..7e672ec 100644 --- a/src/Service/SnipContent/MyersDiff.php +++ b/src/Service/SnipContent/MyersDiff.php @@ -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); - } } \ No newline at end of file