Changeset View
Changeset View
Standalone View
Standalone View
src/utils/PhutilEditDistanceMatrix.php
Show First 20 Lines • Show All 282 Lines • ▼ Show 20 Lines | while (true) { | ||||
if ($xx <= 0 && $yy <= 0) { | if ($xx <= 0 && $yy <= 0) { | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
$str = strrev($str); | $str = strrev($str); | ||||
// We pad the edit string before smoothing it, so ranges of similar | |||||
// characters at the beginning or end of the string can also be smoothed. | |||||
$str = $this->padEditString($str); | |||||
if ($this->getApplySmoothing()) { | if ($this->getApplySmoothing()) { | ||||
$str = $this->applySmoothing($str); | $str = $this->applySmoothing($str); | ||||
} | } | ||||
return $this->padEditString($str); | return $str; | ||||
} | } | ||||
private function padEditString($str) { | private function padEditString($str) { | ||||
if ($this->prefix) { | if ($this->prefix) { | ||||
$str = str_repeat('s', count($this->prefix)).$str; | $str = str_repeat('s', count($this->prefix)).$str; | ||||
} | } | ||||
if ($this->suffix) { | if ($this->suffix) { | ||||
$str = $str.str_repeat('s', count($this->suffix)); | $str = $str.str_repeat('s', count($this->suffix)); | ||||
▲ Show 20 Lines • Show All 203 Lines • ▼ Show 20 Lines | final class PhutilEditDistanceMatrix extends Phobject { | ||||
private function applySmoothing($str) { | private function applySmoothing($str) { | ||||
$result = $str; | $result = $str; | ||||
// Smooth the string out, by replacing short runs of similar characters | // Smooth the string out, by replacing short runs of similar characters | ||||
// with 'x' operations. This makes the result more readable to humans, | // with 'x' operations. This makes the result more readable to humans, | ||||
// since there are fewer choppy runs of short added and removed substrings. | // since there are fewer choppy runs of short added and removed substrings. | ||||
do { | do { | ||||
$original = $result; | $original = $result; | ||||
$result = preg_replace('/([xdi])(s{3})([xdi])/', '$1xxx$3', $result); | $result = preg_replace('/(^|[xdi])(s{3})([xdi]|\z)/', '$1xxx$3', $result); | ||||
$result = preg_replace('/([xdi])(s{2})([xdi])/', '$1xx$3', $result); | $result = preg_replace('/(^|[xdi])(s{2})([xdi]|\z)/', '$1xx$3', $result); | ||||
$result = preg_replace('/([xdi])(s{1})([xdi])/', '$1x$3', $result); | $result = preg_replace('/(^|[xdi])(s{1})([xdi]|\z)/', '$1x$3', $result); | ||||
} while ($result != $original); | } while ($result != $original); | ||||
return $result; | return $result; | ||||
} | } | ||||
} | } |