Skip to content

Commit

Permalink
Merge branch 'pullrequest_7'
Browse files Browse the repository at this point in the history
  • Loading branch information
yetanotherape committed Feb 11, 2017
2 parents 2b5d300 + efd1062 commit b00d838
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 44 deletions.
9 changes: 8 additions & 1 deletion src/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class Diff
const INSERT = 1;
const EQUAL = 0;

/**
* Number of characters at which line mode speedup is employed.
*/
const LINEMODE_THRESOLD = 100;

/**
* @var float Number of seconds to map a diff before giving up (0 for infinity).
*/
Expand Down Expand Up @@ -1099,7 +1104,9 @@ protected function compute($text1, $text2, $checklines, $deadline)
}
}

if ($checklines && mb_strlen($text1) > 100 && mb_strlen($text2) > 100) {
if ($checklines
&& mb_strlen($text1) > Diff::LINEMODE_THRESOLD
&& mb_strlen($text2) > Diff::LINEMODE_THRESOLD) {
return $this->lineMode($text1, $text2, $deadline);
}

Expand Down
47 changes: 13 additions & 34 deletions src/DiffToolkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,44 +293,23 @@ protected function linesToCharsMunge($text, array &$lineArray, array &$lineHash)
// explode('\n', $text) would temporarily double our memory footprint,
// but mb_strpos() and mb_substr() work slow
$lines = explode($delimiter, $text);

$last_line_has_delimiter = end($lines) === '';
if ($last_line_has_delimiter) {
array_pop($lines);
}

foreach ($lines as $i => $line) {
if (mb_strlen($line)) {
if (isset($lines[$i + 1])) {
$line .= $delimiter;
}
if (isset($lineHash[$line])) {
$chars .= Utils::unicodeChr($lineHash[$line]);
} else {
$lineArray[] = $line;
$lineHash[$line] = count($lineArray) - 1;
$chars .= Utils::unicodeChr(count($lineArray) - 1);
}
if (isset($lines[$i + 1]) || $last_line_has_delimiter) {
$line .= $delimiter;
}
if (!isset($lineHash[$line])) {
$lineArray[] = $line;
$lineHash[$line] = count($lineArray) - 1;
}
$chars .= Utils::unicodeChr($lineHash[$line]);
}

// // Walk the text, pulling out a substring for each line.
// // explode('\n', $text) would temporarily double our memory footprint.
// // Modifying text would create many large strings to garbage collect.
// $lineStart = 0;
// $lineEnd = -1;
// $textLen = mb_strlen($text);
// while ($lineEnd < $textLen - 1) {
// $lineEnd = mb_strpos($text, "\n", $lineStart);
// if ($lineEnd === false) {
// $lineEnd = $textLen - 1;
// }
// $line = mb_substr($text, $lineStart, $lineEnd + 1 - $lineStart);
// $lineStart = $lineEnd + 1;
//
// if (isset($lineHash[$line])) {
// $chars .= Utils::unicodeChr($lineHash[$line]);
// } else {
// $lineArray[] = $line;
// $lineHash[$line] = count($lineArray) - 1;
// $chars .= Utils::unicodeChr(count($lineArray) - 1);
// }
// }

return $chars;
}

Expand Down
37 changes: 30 additions & 7 deletions tests/DiffMatchPatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,36 @@ public function testPatchMake()
$this->assertEquals($expected, $this->dmp->patch_toText($patches));
}

public function testPatchApply()
{
$patches = $this->dmp->patch_make("The quick brown fox jumps over the lazy dog.", "That quick brown fox jumped over a lazy dog.");
$this->assertEquals(
array("That quick red rabbit jumped over a tired tiger.", array(true, true,)),
$this->dmp->patch_apply($patches, "The quick red rabbit jumps over the tired tiger.")
);
protected function _testPatchApply($text1, $text2, $target = NULL, $expected = NULL) {
if ($target === NULL) {
$target = $text1;
}
if ($expected === NULL) {
$expected = $text2;
}

$patches = $this->dmp->patch_make($text1, $text2);
$this->assertEquals(
array($expected, array_map(function() { return TRUE; }, $patches)),
$this->dmp->patch_apply($patches, $target)
);
}

public function testPatchApply() {
$this->_testPatchApply(
"The quick brown fox jumps over the lazy dog.",
"That quick brown fox jumped over a lazy dog.",
"The quick red rabbit jumps over the tired tiger.",
"That quick red rabbit jumped over a tired tiger."
);
}

public function testPatchApply_2() {
$linemode_pad = str_pad('pad', Diff::LINEMODE_THRESOLD, 'x');
$this->_testPatchApply(
"line number one\n\nLine number three" . $linemode_pad . 'a',
"LINE number one\nThe second line\n\nThis is Line number three" . $linemode_pad . 'b'
);
}

}
24 changes: 22 additions & 2 deletions tests/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,28 @@ public function testMain()
// Test the linemode speedup.
// Must be long to pass the 100 char cutoff.
// Simple line-mode.
$a = str_repeat("1234567890\n", 13);
$b = str_repeat("abcdefghij\n", 13);
$a = str_repeat("1234567890\n", ceil(Diff::LINEMODE_THRESOLD / strlen($a)));
$b = str_repeat("abcdefghij\n", ceil(Diff::LINEMODE_THRESOLD / strlen($b)));
$this->assertEquals(
$this->d->main($a, $b, false)->getChanges(),
$this->d->main($a, $b, true)->getChanges()
);

// Simple line-mode with multiple line breaks.
$a = "12345\n\n67890\n";
$a = str_repeat($a, ceil(Diff::LINEMODE_THRESOLD / strlen($a)));
$b = "abcde\n\nfghij\n";
$b = str_repeat($b, ceil(Diff::LINEMODE_THRESOLD / strlen($b)));
$this->assertEquals(
$this->d->main($a, $b, false)->getChanges(),
$this->d->main($a, $b, true)->getChanges()
);

// Simple line-mode with multiple line breaks but without leading line break
$a = "12345\n\n67890\n";
$a = str_repeat($a, ceil(Diff::LINEMODE_THRESOLD / strlen($a))) . '0';
$b = "abcde\n\nfghij\n";
$b = str_repeat($b, ceil(Diff::LINEMODE_THRESOLD / strlen($b))) . 'a ';
$this->assertEquals(
$this->d->main($a, $b, false)->getChanges(),
$this->d->main($a, $b, true)->getChanges()
Expand Down

0 comments on commit b00d838

Please sign in to comment.