Merge pull request #8111 from open-sausages/pulls/4/7.2-is-just-out-of-each

BUGFIX: Refactor difflib to not use each()
This commit is contained in:
Daniel Hensby 2018-06-04 12:47:59 +01:00 committed by GitHub
commit 0bec6e07dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View File

@ -54,6 +54,27 @@ class DiffTest extends SapphireTest
$compare = preg_replace('/[\s\t\n\r]*/', '', $compare);
$expected = preg_replace('/[\s\t\n\r]*/', '', $expected);
$this->assertEquals($compare, $expected);
$this->assertEquals($expected, $compare);
}
/**
* @see https://github.com/silverstripe/silverstripe-framework/issues/8053
*/
public function testLegacyEachStatement()
{
$sentenceOne =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
$sentenceTwo =
'Nulla porttitor, ex quis commodo pharetra, diam dui efficitur justo, eu gravida elit eros vel libero.';
$from = "$sentenceOne $sentenceTwo";
$to = "$sentenceTwo $sentenceOne";
// We're cheating our test a little bit here, because depending on what HTML cleaner you have, you'll get
// spaces added or not added around the tags.
$expected = "/^ *<del>$sentenceOne<\/del> *$sentenceTwo *<ins>$sentenceOne<\/ins> *$/";
$actual = Diff::compareHTML($from, $to);
$this->assertRegExp($expected, $actual);
}
}

View File

@ -283,14 +283,17 @@ class DiffEngine
continue;
$matches = $ymatches[$line];
reset($matches);
while (list ($junk, $y) = each($matches))
$pointer = 0;
foreach ($matches as $y) {
$pointer++;
if (empty($this->in_seq[$y])) {
$k = $this->lcs_pos($y);
assert($k > 0);
$ymids[$k] = $ymids[$k - 1];
break;
}
while (list ($junk, $y) = each($matches)) {
}
foreach (array_slice($matches, $pointer) as $y) {
if ($y > $this->seq[$k - 1]) {
assert($y < $this->seq[$k]);
// Optimization: this is a common case: