Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class Differ
'groupedOpcodesGnu' => [],
'oldNoEolAtEofIdx' => -1,
'newNoEolAtEofIdx' => -1,
'oldNewComparison' => 0,
'oldNewAreSame' => false,
];

/**
Expand Down Expand Up @@ -84,10 +84,9 @@ final class Differ
private int $newNoEolAtEofIdx = -1;

/**
* @var int the result of comparing the old and the new with the spaceship operator
* `-1` means `old < new`, `0` means `old == new`, `1` means `old > new`
* @var bool the result of spaceship operator comparing old and new (0 if they are the same, 1 if they are different)
*/
private int $oldNewComparison = 0;
private bool $oldNewAreSame = false;

/**
* @var int[][][] array containing the generated opcodes for the differences between the two items
Expand Down Expand Up @@ -232,12 +231,22 @@ public function getNewNoEolAtEofIdx(): int
return $this->finalize()->newNoEolAtEofIdx;
}

/**
* Check whether the old and the new are the same.
*/
public function getOldNewAreSame(): bool
{
return $this->finalize()->oldNewAreSame;
}

/**
* Compare the old and the new with the spaceship operator.
*
* @deprecated Use getOldNewAreSame() instead
*/
public function getOldNewComparison(): int
{
return $this->finalize()->oldNewComparison;
return $this->getOldNewAreSame() ? 0 : 1;
}

/**
Expand Down Expand Up @@ -302,7 +311,7 @@ public function getGroupedOpcodes(): array

$this->getGroupedOpcodesPre($old, $new);

if ($this->oldNewComparison === 0 && $this->options->fullContextIfIdentical) {
if ($this->oldNewAreSame && $this->options->fullContextIfIdentical) {
$opcodes = [
[
[SequenceMatcher::OP_EQ, 0, \count($old), 0, \count($new)],
Expand Down Expand Up @@ -338,7 +347,7 @@ public function getGroupedOpcodesGnu(): array

$this->getGroupedOpcodesGnuPre($old, $new);

if ($this->oldNewComparison === 0 && $this->options->fullContextIfIdentical) {
if ($this->oldNewAreSame && $this->options->fullContextIfIdentical) {
$opcodes = [
[
[SequenceMatcher::OP_EQ, 0, \count($old), 0, \count($new)],
Expand Down Expand Up @@ -483,7 +492,7 @@ private function finalize(): self

$this->oldNoEolAtEofIdx = $this->getOld(-1) === [''] ? -1 : \count($this->old);
$this->newNoEolAtEofIdx = $this->getNew(-1) === [''] ? -1 : \count($this->new);
$this->oldNewComparison = $this->old <=> $this->new;
$this->oldNewAreSame = $this->old === $this->new;

$this->sequenceMatcher->setOptions($this->options->toSequenceMatcherOptions());
}
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ final public function render(Differ $differ): string
$this->changesAreRaw = true;

// the "no difference" situation may happen frequently
return $differ->getOldNewComparison() === 0 && !$differ->options->fullContextIfIdentical
return $differ->getOldNewAreSame() && !$differ->options->fullContextIfIdentical
? $this->getResultForIdenticals()
: $this->renderWorker($differ);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/DifferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,32 @@ public static function provideGetGroupedOpcodesCases(): iterable
],
];
}

/**
* Test that numeric strings are compared as strings, not numbers.
*
* @covers \Jfcherng\Diff\Differ::getOldNewAreSame
*
* @see https://github.com/jfcherng/php-diff/issues/94
*/
#[DataProvider('provideNumericStringComparisonCases')]
public function testNumericStringComparison(string $old, string $new, bool $expectedAreSame): void
{
$differ = new Differ(explode("\n", $old), explode("\n", $new));

self::assertSame($expectedAreSame, $differ->getOldNewAreSame());
}

/**
* Data provider for testNumericStringComparison.
*/
public static function provideNumericStringComparisonCases(): iterable
{
return [
'zero vs multiple zeros' => ['0', '00000', false],
'identical zeros' => ['0', '0', true],
'numeric strings with different values' => ['1', '01', false],
'leading zeros' => ['007', '7', false],
];
}
}
Loading