Changeset View
Changeset View
Standalone View
Standalone View
src/difference/ArcanistDiffUtils.php
Show First 20 Lines • Show All 145 Lines • ▼ Show 20 Lines | final class ArcanistDiffUtils extends Phobject { | ||||
public static function generateEditString(array $ov, array $nv, $max = 80) { | public static function generateEditString(array $ov, array $nv, $max = 80) { | ||||
return id(new PhutilEditDistanceMatrix()) | return id(new PhutilEditDistanceMatrix()) | ||||
->setComputeString(true) | ->setComputeString(true) | ||||
->setAlterCost(1 / ($max * 2)) | ->setAlterCost(1 / ($max * 2)) | ||||
->setReplaceCost(2) | ->setReplaceCost(2) | ||||
->setMaximumLength($max) | ->setMaximumLength($max) | ||||
->setSequences($ov, $nv) | ->setSequences($ov, $nv) | ||||
->setApplySmoothing(true) | |||||
->getEditString(); | ->getEditString(); | ||||
} | } | ||||
public static function computeIntralineEdits($o, $n) { | public static function computeIntralineEdits($o, $n) { | ||||
if (preg_match('/[\x80-\xFF]/', $o.$n)) { | if (preg_match('/[\x80-\xFF]/', $o.$n)) { | ||||
$ov = phutil_utf8v_combined($o); | $ov = phutil_utf8v_combined($o); | ||||
$nv = phutil_utf8v_combined($n); | $nv = phutil_utf8v_combined($n); | ||||
$multibyte = true; | $multibyte = true; | ||||
} else { | } else { | ||||
$ov = str_split($o); | $ov = str_split($o); | ||||
$nv = str_split($n); | $nv = str_split($n); | ||||
$multibyte = false; | $multibyte = false; | ||||
} | } | ||||
$result = self::generateEditString($ov, $nv); | $result = self::generateEditString($ov, $nv); | ||||
// Smooth the string out, by replacing short runs of similar characters | |||||
// with 'x' operations. This makes the result more readable to humans, since | |||||
// there are fewer choppy runs of short added and removed substrings. | |||||
do { | |||||
$original = $result; | |||||
$result = preg_replace( | |||||
'/([xdi])(s{3})([xdi])/', | |||||
'$1xxx$3', | |||||
$result); | |||||
$result = preg_replace( | |||||
'/([xdi])(s{2})([xdi])/', | |||||
'$1xx$3', | |||||
$result); | |||||
$result = preg_replace( | |||||
'/([xdi])(s{1})([xdi])/', | |||||
'$1x$3', | |||||
$result); | |||||
} while ($result != $original); | |||||
// Now we have a character-based description of the edit. We need to | // Now we have a character-based description of the edit. We need to | ||||
// convert into a byte-based description. Walk through the edit string and | // convert into a byte-based description. Walk through the edit string and | ||||
// adjust each operation to reflect the number of bytes in the underlying | // adjust each operation to reflect the number of bytes in the underlying | ||||
// character. | // character. | ||||
$o_pos = 0; | $o_pos = 0; | ||||
$n_pos = 0; | $n_pos = 0; | ||||
$result_len = strlen($result); | $result_len = strlen($result); | ||||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |