Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/diff/prose/PhutilProseDifferenceEngine.php
Show First 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | private function stitchPieces(array $pieces, $level) { | ||||
$count = count($pieces); | $count = count($pieces); | ||||
for ($ii = 0; $ii < $count; $ii += 2) { | for ($ii = 0; $ii < $count; $ii += 2) { | ||||
$result = $pieces[$ii]; | $result = $pieces[$ii]; | ||||
if ($ii + 1 < $count) { | if ($ii + 1 < $count) { | ||||
$result .= $pieces[$ii + 1]; | $result .= $pieces[$ii + 1]; | ||||
} | } | ||||
if ($level < 2) { | if ($level < 2) { | ||||
// Split pieces into separate text and whitespace sections: make one | $trimmed_pieces = $this->trimApart($result); | ||||
// piece out of all the whitespace at the beginning, one piece out of | foreach ($trimmed_pieces as $trimmed_piece) { | ||||
// all the actual text in the middle, and one piece out of all the | $results[] = $trimmed_piece; | ||||
// whitespace at the end. | |||||
$matches = null; | |||||
preg_match('/^(\s*)(.*?)(\s*)\z/s', $result, $matches); | |||||
if (strlen($matches[1])) { | |||||
$results[] = $matches[1]; | |||||
} | |||||
if (strlen($matches[2])) { | |||||
$results[] = $matches[2]; | |||||
} | |||||
if (strlen($matches[3])) { | |||||
$results[] = $matches[3]; | |||||
} | } | ||||
} else { | } else { | ||||
$results[] = $result; | $results[] = $result; | ||||
} | } | ||||
} | } | ||||
// If the input ended with a delimiter, we can get an empty final piece. | // If the input ended with a delimiter, we can get an empty final piece. | ||||
// Just discard it. | // Just discard it. | ||||
▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | foreach ($parts as $part) { | ||||
$blocks[] = id(new PhabricatorDocumentEngineBlock()) | $blocks[] = id(new PhabricatorDocumentEngineBlock()) | ||||
->setContent($part) | ->setContent($part) | ||||
->setDifferenceHash($hash); | ->setDifferenceHash($hash); | ||||
} | } | ||||
return $blocks; | return $blocks; | ||||
} | } | ||||
public static function trimApart($input) { | |||||
// Split pieces into separate text and whitespace sections: make one | |||||
// piece out of all the whitespace at the beginning, one piece out of | |||||
// all the actual text in the middle, and one piece out of all the | |||||
// whitespace at the end. | |||||
$parts = array(); | |||||
$length = strlen($input); | |||||
$corpus = ltrim($input); | |||||
$l_length = strlen($corpus); | |||||
if ($l_length !== $length) { | |||||
$parts[] = substr($input, 0, $length - $l_length); | |||||
} | |||||
$corpus = rtrim($corpus); | |||||
$lr_length = strlen($corpus); | |||||
if ($lr_length) { | |||||
$parts[] = $corpus; | |||||
} | |||||
if ($lr_length !== $l_length) { | |||||
// NOTE: This will be a negative value; we're slicing from the end of | |||||
// the input string. | |||||
$parts[] = substr($input, $lr_length - $l_length); | |||||
} | |||||
return $parts; | |||||
} | |||||
} | } |