Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F15410312
D21422.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D21422.diff
View Options
diff --git a/src/infrastructure/diff/prose/PhutilProseDifferenceEngine.php b/src/infrastructure/diff/prose/PhutilProseDifferenceEngine.php
--- a/src/infrastructure/diff/prose/PhutilProseDifferenceEngine.php
+++ b/src/infrastructure/diff/prose/PhutilProseDifferenceEngine.php
@@ -142,22 +142,9 @@
}
if ($level < 2) {
- // 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.
-
- $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];
+ $trimmed_pieces = $this->trimApart($result);
+ foreach ($trimmed_pieces as $trimmed_piece) {
+ $results[] = $trimmed_piece;
}
} else {
$results[] = $result;
@@ -272,4 +259,36 @@
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;
+ }
+
}
diff --git a/src/infrastructure/diff/prose/__tests__/PhutilProseDiffTestCase.php b/src/infrastructure/diff/prose/__tests__/PhutilProseDiffTestCase.php
--- a/src/infrastructure/diff/prose/__tests__/PhutilProseDiffTestCase.php
+++ b/src/infrastructure/diff/prose/__tests__/PhutilProseDiffTestCase.php
@@ -3,6 +3,39 @@
final class PhutilProseDiffTestCase
extends PhabricatorTestCase {
+ public function testTrimApart() {
+ $map = array(
+ '' => array(),
+ 'a' => array('a'),
+ ' a ' => array(
+ ' ',
+ 'a',
+ ' ',
+ ),
+ ' a' => array(
+ ' ',
+ 'a',
+ ),
+ 'a ' => array(
+ 'a',
+ ' ',
+ ),
+ ' a b ' => array(
+ ' ',
+ 'a b',
+ ' ',
+ ),
+ );
+
+ foreach ($map as $input => $expect) {
+ $actual = PhutilProseDifferenceEngine::trimApart($input);
+ $this->assertEqual(
+ $expect,
+ $actual,
+ pht('Trim Apart: %s', $input));
+ }
+ }
+
public function testProseDiffsDistance() {
$this->assertProseParts(
'',
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Mar 20, 7:03 AM (19 h, 1 m ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7347567
Default Alt Text
D21422.diff (3 KB)
Attached To
Mode
D21422: Fix an issue where prose diffing may fail after hitting the PCRE backtracking limit
Attached
Detach File
Event Timeline
Log In to Comment