]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Use PhutilEditDistanceMatrix in ArcanistDiffUtils
authorepriestley <git@epriestley.com>
Thu, 23 May 2013 21:36:42 +0000 (14:36 -0700)
committerepriestley <git@epriestley.com>
Thu, 23 May 2013 21:36:42 +0000 (14:36 -0700)
Summary: Replace this old hard-coded implementation with the new vector-based, unicode-capable one.

Test Plan: Ran unit tests. Looked at revisions in Differential, using whitespace modes to bypass cache.

Reviewers: btrahan, Afaque_Hussain

Reviewed By: btrahan

CC: aran
Maniphest Tasks: T2379

Differential Revision: https://secure.phabricator.com/D6016

src/difference/ArcanistDiffUtils.php
src/difference/__tests__/ArcanistDiffUtilsTestCase.php
src/unit/engine/phutil/ArcanistPhutilTestCase.php

index 82e71e5dc9b4436c223a6893eb9f9b1d97e223fb..ae624eaa31fa388d90c8abd1eae316934066b85b 100644 (file)
@@ -210,133 +210,20 @@ final class ArcanistDiffUtils {
       return str_repeat('d', $olt);
     }
 
-    $min = min($olt, $nlt);
-    $t_start = microtime(true);
-
-    $pre = 0;
-    while ($pre < $min && $o[$pre] == $n[$pre]) {
-      $pre++;
-    }
-
-    $end = 0;
-    while ($end < $min && $o[($olt - 1) - $end] == $n[($nlt - 1) - $end]) {
-      $end++;
-    }
-
-    if ($end + $pre >= $min) {
-      $end = min($end, $min - $pre);
-      $prefix = str_repeat('s', $pre);
-      $suffix = str_repeat('s', $end);
-      $infix = null;
-      if ($olt > $nlt) {
-        $infix = str_repeat('d', $olt - ($end + $pre));
-      } else if ($nlt > $olt) {
-        $infix = str_repeat('i', $nlt - ($end + $pre));
-      }
-      return $prefix.$infix.$suffix;
-    }
-
-    if ($min - ($end + $pre) > 80) {
-      $max = max($olt, $nlt);
-      return str_repeat('x', $min) .
-          str_repeat($olt < $nlt ? 'i' : 'd', $max - $min);
-    }
-
-    $prefix = str_repeat('s', $pre);
-    $suffix = str_repeat('s', $end);
-    $o = substr($o, $pre, $olt - $end - $pre);
-    $n = substr($n, $pre, $nlt - $end - $pre);
-
-    $ol = strlen($o);
-    $nl = strlen($n);
-
-    $m = array_fill(0, $ol + 1, array_fill(0, $nl + 1, array()));
-
-    $t_d = 'd';
-    $t_i = 'i';
-    $t_s = 's';
-    $t_x = 'x';
-
-    $m[0][0] = array(
-      0,
-      null);
-
-    for ($ii = 1; $ii <= $ol; $ii++) {
-      $m[$ii][0] = array(
-        $ii * 1000,
-        $t_d);
+    if ($o === $n) {
+      return str_repeat('s', $olt);
     }
 
-    for ($jj = 1; $jj <= $nl; $jj++) {
-      $m[0][$jj] = array(
-        $jj * 1000,
-        $t_i);
-    }
-
-    $ii = 1;
-    do {
-      $jj = 1;
-      do {
-        if ($o[$ii - 1] == $n[$jj - 1]) {
-          $sub_t_cost = $m[$ii - 1][$jj - 1][0] + 0;
-          $sub_t      = $t_s;
-        } else {
-          $sub_t_cost = $m[$ii - 1][$jj - 1][0] + 2000;
-          $sub_t      = $t_x;
-        }
-
-        if ($m[$ii - 1][$jj - 1][1] != $sub_t) {
-          $sub_t_cost += 1;
-        }
-
-        $del_t_cost = $m[$ii - 1][$jj][0] + 1000;
-        if ($m[$ii - 1][$jj][1] != $t_d) {
-          $del_t_cost += 1;
-        }
-
-        $ins_t_cost = $m[$ii][$jj - 1][0] + 1000;
-        if ($m[$ii][$jj - 1][1] != $t_i) {
-          $ins_t_cost += 1;
-        }
-
-        if ($sub_t_cost <= $del_t_cost && $sub_t_cost <= $ins_t_cost) {
-          $m[$ii][$jj] = array(
-            $sub_t_cost,
-            $sub_t);
-        } else if ($ins_t_cost <= $del_t_cost) {
-          $m[$ii][$jj] = array(
-            $ins_t_cost,
-            $t_i);
-        } else {
-          $m[$ii][$jj] = array(
-            $del_t_cost,
-            $t_d);
-        }
-      } while ($jj++ < $nl);
-    } while ($ii++ < $ol);
-
-    $result = '';
-    $ii = $ol;
-    $jj = $nl;
-    do {
-      $r = $m[$ii][$jj][1];
-      $result .= $r;
-      switch ($r) {
-        case $t_s:
-        case $t_x:
-          $ii--;
-          $jj--;
-          break;
-        case $t_i:
-          $jj--;
-          break;
-        case $t_d:
-          $ii--;
-          break;
-      }
-    } while ($ii || $jj);
+    $ov = str_split($o);
+    $nv = str_split($n);
 
-    return $prefix.strrev($result).$suffix;
+    return id(new PhutilEditDistanceMatrix())
+      ->setComputeString(true)
+      ->setAlterCost(0.001)
+      ->setReplaceCost(2)
+      ->setMaximumLength(80)
+      ->setSequences($ov, $nv)
+      ->getEditString();
   }
 
   public static function generateUTF8IntralineDiff($o, $n) {
index dab25d532d019f495eeb9b065fc0b6ca84827b52..23ea0ea838f09d45382596c7dd5a2ec7b08fba18 100644 (file)
@@ -94,7 +94,8 @@ final class ArcanistDiffUtilsTestCase extends ArcanistTestCase {
         $test[2],
         ArcanistDiffUtils::buildLevenshteinDifferenceString(
           $test[0],
-          $test[1]));
+          $test[1]),
+        "'{$test[0]}' vs '{$test[1]}'");
     }
   }
 
index ba5e2a45b604d54c5d0b5645eead005035542210..9b4f2bdabe77dc1ff6352f0e969ea4282f2c80b6 100644 (file)
@@ -68,7 +68,7 @@ abstract class ArcanistPhutilTestCase {
 
     if (strpos($expect, "\n") === false && strpos($result, "\n") === false) {
       $output .= "Expected: {$expect}\n";
-      $output .= "Actual: {$result}";
+      $output .= "  Actual: {$result}";
     } else {
       $output .= "Expected vs Actual Output Diff\n";
       $output .= ArcanistDiffUtils::renderDifferences(