mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-27 16:38:41 +00:00
Add diff function for str. changes
This commit is contained in:
parent
8d15c645a9
commit
9661171851
|
|
@ -337,7 +337,7 @@ final class StringUtils
|
|||
break;
|
||||
}
|
||||
|
||||
$count++;
|
||||
++$count;
|
||||
}
|
||||
|
||||
return $count;
|
||||
|
|
@ -426,4 +426,70 @@ final class StringUtils
|
|||
return $element->__toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static function computeLCSDiff(string $from, string $to) : array
|
||||
{
|
||||
$diffValues = [];
|
||||
$diffMask = [];
|
||||
|
||||
$dm = [];
|
||||
$n1 = \count($from);
|
||||
$n2 = \count($to);
|
||||
|
||||
for ($j = -1; $j < $n2; $j++) {
|
||||
$dm[-1][$j] = 0;
|
||||
}
|
||||
|
||||
for ($i = -1; $i < $n1; $i++) {
|
||||
$dm[$i][-1] = 0;
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $n1; $i++) {
|
||||
for ($j = 0; $j < $n2; $j++) {
|
||||
if ($from[$i] == $to[$j]) {
|
||||
$ad = $dm[$i - 1][$j - 1];
|
||||
$dm[$i][$j] = $ad + 1;
|
||||
}
|
||||
else {
|
||||
$a1 = $dm[$i - 1][$j];
|
||||
$a2 = $dm[$i][$j - 1];
|
||||
$dm[$i][$j] = \max($a1, $a2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$i = $n1 - 1;
|
||||
$j = $n2 - 1;
|
||||
while (($i > -1) || ($j > -1)) {
|
||||
if ($j > -1) {
|
||||
if ($dm[$i][$j - 1] == $dm[$i][$j]) {
|
||||
$diffValues[] = $to[$j];
|
||||
$diffMask[] = 1;
|
||||
--$j;
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if ($i > -1) {
|
||||
if ($dm[$i - 1][$j] == $dm[$i][$j]) {
|
||||
$diffValues[] = $from[$i];
|
||||
$diffMask[] = -1;
|
||||
--$i;
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$diffValues[] = $from[$i];
|
||||
$diffMask[] = 0;
|
||||
--$i;
|
||||
--$j;
|
||||
}
|
||||
|
||||
$diffValues = \array_reverse($diffValues);
|
||||
$diffMask = \array_reverse($diffMask);
|
||||
|
||||
return ['values' => $diffValues, 'mask' => $diffMask];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user