diff --git a/Utils/StringUtils.php b/Utils/StringUtils.php
index 37f458b47..d9b287538 100644
--- a/Utils/StringUtils.php
+++ b/Utils/StringUtils.php
@@ -427,45 +427,87 @@ final class StringUtils
}
}
- public static function diffline(string $line1, string $line2) : string
+ /**
+ * Create string difference markup
+ *
+ * @param string $old Old strings
+ * @param string $new New strings
+ *
+ * @return string Markup using and tags
+ *
+ * @since 1.0.0
+ */
+ public static function createDiffMarkup(string $old, string $new) : string
{
- $diff = self::computeLCSDiff(str_split($line1), str_split($line2));
- $diffval = $diff['values'];
+ $splitOld = \str_split($old);
+ $splitNew = \str_split($new);
+
+ if ($splitOld === false) {
+ return '' . $new . '';
+ }
+
+ if ($splitNew === false) {
+ return '' . $old . '';
+ }
+
+ $diff = self::computeLCSDiff($splitOld, $splitNew);
+ $diffval = $diff['values'];
$diffmask = $diff['mask'];
- $n = count($diffval);
- $pmc = 0;
+ $n = \count($diffval);
+ $pmc = 0;
$result = '';
- for ($i = 0; $i < $n; $i++)
- {
+
+ for ($i = 0; $i < $n; ++$i) {
$mc = $diffmask[$i];
- if ($mc != $pmc)
- {
- switch ($pmc)
- {
- case -1: $result .= ''; break;
- case 1: $result .= ''; break;
+
+ if ($mc != $pmc) {
+ switch ($pmc) {
+ case -1:
+ $result .= '';
+ break;
+ case 1:
+ $result .= '';
+ break;
}
- switch ($mc)
- {
- case -1: $result .= ''; break;
- case 1: $result .= ''; break;
+
+ switch ($mc) {
+ case -1:
+ $result .= '';
+ break;
+ case 1:
+ $result .= '';
+ break;
}
}
- $result .= $diffval[$i];
- $pmc = $mc;
+ $result .= $diffval[$i];
+ $pmc = $mc;
}
- switch ($pmc)
- {
- case -1: $result .= ''; break;
- case 1: $result .= ''; break;
+
+ switch ($pmc) {
+ case -1:
+ $result .= '';
+ break;
+ case 1:
+ $result .= '';
+ break;
}
return $result;
}
- private static function computeLCSDiff(string $from, string $to) : array
+ /**
+ * Create LCS diff masks
+ *
+ * @param array $from From/old strings
+ * @param array $to To/new strings
+ *
+ * @return array
+ *
+ * @since 1.0.0
+ */
+ private static function computeLCSDiff(array $from, array $to) : array
{
$diffValues = [];
$diffMask = [];
@@ -474,23 +516,26 @@ final class StringUtils
$n1 = \count($from);
$n2 = \count($to);
- for ($j = -1; $j < $n2; $j++) {
+ if ($n1 < 1 || $n2 < 1) {
+ throw new \Exception();
+ }
+
+ for ($j = -1; $j < $n2; ++$j) {
$dm[-1][$j] = 0;
}
- for ($i = -1; $i < $n1; $i++) {
+ 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];
+ 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];
+ } else {
+ $a1 = $dm[$i - 1][$j];
+ $a2 = $dm[$i][$j - 1];
$dm[$i][$j] = \max($a1, $a2);
}
}
@@ -498,29 +543,25 @@ final class StringUtils
$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;
+ while ($i > -1 || $j > -1) {
+ if ($j > -1 && $dm[$i][$j - 1] === $dm[$i][$j]) {
+ $diffValues[] = $to[$j];
+ $diffMask[] = 1;
+ --$j;
- continue;
- }
+ continue;
}
- if ($i > -1) {
- if ($dm[$i - 1][$j] == $dm[$i][$j]) {
- $diffValues[] = $from[$i];
- $diffMask[] = -1;
- --$i;
+ if ($i > -1 && $dm[$i - 1][$j] === $dm[$i][$j]) {
+ $diffValues[] = $from[$i];
+ $diffMask[] = -1;
+ --$i;
- continue;
- }
+ continue;
}
$diffValues[] = $from[$i];
- $diffMask[] = 0;
+ $diffMask[] = 0;
--$i;
--$j;
}