Skip to content

Commit 49cd8f1

Browse files
committed
Merge branch 'fix-strcmp'
# Conflicts: # src/Diff.php
2 parents abe9960 + 573a901 commit 49cd8f1

6 files changed

Lines changed: 78 additions & 43 deletions

File tree

src/Diff.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public function cleanupMerge()
270270
}
271271
}
272272

273-
if ($diffs[count($diffs) - 1][1] == '') {
273+
if ($diffs[count($diffs) - 1][1] === '') {
274274
array_pop($diffs);
275275
}
276276

@@ -283,13 +283,13 @@ public function cleanupMerge()
283283
while ($pointer < count($diffs) - 1) {
284284
if ($diffs[$pointer - 1][0] == self::EQUAL && $diffs[$pointer + 1][0] == self::EQUAL) {
285285
// This is a single edit surrounded by equalities.
286-
if (mb_substr($diffs[$pointer][1], -mb_strlen($diffs[$pointer - 1][1])) == $diffs[$pointer - 1][1]) {
286+
if (mb_substr($diffs[$pointer][1], -mb_strlen($diffs[$pointer - 1][1])) === $diffs[$pointer - 1][1]) {
287287
// Shift the edit over the previous equality.
288288
$diffs[$pointer][1] = $diffs[$pointer - 1][1] . mb_substr($diffs[$pointer][1], 0, -mb_strlen($diffs[$pointer - 1][1]));
289289
$diffs[$pointer + 1][1] = $diffs[$pointer - 1][1] . $diffs[$pointer + 1][1];
290290
array_splice($diffs, $pointer - 1, 1);
291291
$changes = true;
292-
} elseif (mb_substr($diffs[$pointer][1], 0, mb_strlen($diffs[$pointer + 1][1])) == $diffs[$pointer + 1][1]) {
292+
} elseif (mb_substr($diffs[$pointer][1], 0, mb_strlen($diffs[$pointer + 1][1])) === $diffs[$pointer + 1][1]) {
293293
// Shift the edit over the next equality.
294294
$diffs[$pointer - 1][1] = $diffs[$pointer - 1][1] . $diffs[$pointer + 1][1];
295295
$diffs[$pointer][1] = mb_substr($diffs[$pointer][1], mb_strlen($diffs[$pointer + 1][1])) . $diffs[$pointer + 1][1];
@@ -343,7 +343,7 @@ public function cleanupSemanticLossless()
343343
$bestEdit = $edit;
344344
$bestEquality2 = $equality2;
345345
$bestScore = $this->cleanupSemanticScore($equality1, $edit) + $this->cleanupSemanticScore($edit, $equality2);
346-
while ($edit && $equality2 && mb_substr($edit, 0, 1) == mb_substr($equality2, 0, 1)) {
346+
while ($edit && $equality2 && mb_substr($edit, 0, 1) === mb_substr($equality2, 0, 1)) {
347347
$equality1 .= mb_substr($edit, 0, 1);
348348
$edit = mb_substr($edit, 1) . mb_substr($equality2, 0, 1);
349349
$equality2 = mb_substr($equality2, 1);
@@ -356,16 +356,16 @@ public function cleanupSemanticLossless()
356356
$bestEquality2 = $equality2;
357357
}
358358
}
359-
if ($diffs[$pointer - 1][1] != $bestEquality1) {
359+
if ($diffs[$pointer - 1][1] !== $bestEquality1) {
360360
// We have an improvement, save it back to the diff.
361-
if ($bestEquality1 != '') {
361+
if ($bestEquality1 !== '') {
362362
$diffs[$pointer - 1][1] = $bestEquality1;
363363
} else {
364364
array_splice($diffs, $pointer - 1, 1);
365365
$pointer -= 1;
366366
}
367367
$diffs[$pointer][1] = $bestEdit;
368-
if ($bestEquality2 != '') {
368+
if ($bestEquality2 !== '') {
369369
$diffs[$pointer + 1][1] = $bestEquality2;
370370
} else {
371371
array_splice($diffs, $pointer + 1, 1);
@@ -391,7 +391,7 @@ public function cleanupSemanticLossless()
391391
*/
392392
protected function cleanupSemanticScore($one, $two)
393393
{
394-
if ($one == '' || $two == '') {
394+
if ($one === '' || $two === '') {
395395
// Edges are the best.
396396
return 6;
397397
}
@@ -472,7 +472,7 @@ public function cleanupSemantic()
472472
}
473473
// Eliminate an equality that is smaller or equal to the edits on both sides of it.
474474
if (
475-
$lastequality != '' &&
475+
$lastequality !== null &&
476476
mb_strlen($lastequality) <= max($length_insertions1, $length_deletions1) &&
477477
mb_strlen($lastequality) <= max($length_insertions2, $length_deletions2)
478478
) {
@@ -623,7 +623,7 @@ public function cleanupEfficiency() {
623623
// <ins>A</ins><del>B</del>X<del>C</del>
624624
// TODO refactor condition
625625
if (
626-
$lastequality != '' &&
626+
$lastequality !== null &&
627627
(
628628
($pre_ins && $pre_del && $post_ins && $post_del) ||
629629
(
@@ -797,7 +797,7 @@ public function fromDelta($text1, $delta)
797797
$pointer = 0;
798798
$tokens = explode("\t", $delta);
799799
foreach ($tokens as $token) {
800-
if ($token == '') {
800+
if ($token === '') {
801801
// Blank tokens are ok (from a trailing \t).
802802
continue;
803803
}
@@ -824,7 +824,7 @@ public function fromDelta($text1, $delta)
824824
$text = mb_substr($text1, $pointer, $n);
825825
$pointer += $n;
826826
$diffs[] = array(
827-
$op == '=' ? self::EQUAL : self::DELETE,
827+
$op === '=' ? self::EQUAL : self::DELETE,
828828
$text,
829829
);
830830
break;
@@ -954,8 +954,8 @@ public function main($text1, $text2, $checklines = true, $deadline = null)
954954
}
955955

956956
// Check for equality (speedup).
957-
if ($text1 == $text2) {
958-
if ($text1 != '') {
957+
if ($text1 === $text2) {
958+
if ($text1 !== '') {
959959
$this->setChanges(array(
960960
array(self::EQUAL, $text1),
961961
));
@@ -966,7 +966,7 @@ public function main($text1, $text2, $checklines = true, $deadline = null)
966966

967967
$prevInternalEncoding = mb_internal_encoding();
968968
$newInternalEncoding = 'UCS-2LE';
969-
if ($prevInternalEncoding != $newInternalEncoding) {
969+
if ($prevInternalEncoding !== $newInternalEncoding) {
970970
$errorReportingLevel = error_reporting();
971971
error_reporting($errorReportingLevel & ~E_NOTICE);
972972

@@ -1011,14 +1011,14 @@ public function main($text1, $text2, $checklines = true, $deadline = null)
10111011
$diffs = $this->compute($text1, $text2, $checklines, $deadline);
10121012

10131013
// Restore the prefix and suffix.
1014-
if ($commonPrefix != '') {
1014+
if ($commonPrefix !== '') {
10151015
array_unshift($diffs, array(self::EQUAL, $commonPrefix));
10161016
}
1017-
if ($commonSuffix != '') {
1017+
if ($commonSuffix !== '') {
10181018
array_push($diffs, array(self::EQUAL, $commonSuffix));
10191019
}
10201020

1021-
if ($newInternalEncoding != $prevInternalEncoding) {
1021+
if ($newInternalEncoding !== $prevInternalEncoding) {
10221022
mb_internal_encoding($prevInternalEncoding);
10231023
foreach ($diffs as &$change) {
10241024
$change[1] = iconv($newInternalEncoding, $prevInternalEncoding, $change[1]);
@@ -1048,14 +1048,14 @@ public function main($text1, $text2, $checklines = true, $deadline = null)
10481048
*/
10491049
protected function compute($text1, $text2, $checklines, $deadline)
10501050
{
1051-
if ($text1 == '') {
1051+
if ($text1 === '') {
10521052
// Just add some text (speedup).
10531053
return array(
10541054
array(self::INSERT, $text2),
10551055
);
10561056
}
10571057

1058-
if ($text2 == '') {
1058+
if ($text2 === '') {
10591059
// Just delete some text (speedup).
10601060
return array(
10611061
array(self::DELETE, $text1),
@@ -1248,7 +1248,7 @@ protected function bisect($text1, $text2, $deadline)
12481248
$x1 = $v1[$k1Offset - 1] + 1;
12491249
}
12501250
$y1 = $x1 - $k1;
1251-
while ($x1 < $text1Length && $y1 < $text2Length && mb_substr($text1, $x1, 1) == mb_substr($text2, $y1, 1)) {
1251+
while ($x1 < $text1Length && $y1 < $text2Length && mb_substr($text1, $x1, 1) === mb_substr($text2, $y1, 1)) {
12521252
$x1++;
12531253
$y1++;
12541254
}
@@ -1281,7 +1281,7 @@ protected function bisect($text1, $text2, $deadline)
12811281
$x2 = $v2[$k2Offset - 1] + 1;
12821282
}
12831283
$y2 = $x2 - $k2;
1284-
while ($x2 < $text1Length && $y2 < $text2Length && mb_substr($text1, -$x2 - 1, 1) == mb_substr($text2, -$y2 - 1, 1)) {
1284+
while ($x2 < $text1Length && $y2 < $text2Length && mb_substr($text1, -$x2 - 1, 1) === mb_substr($text2, -$y2 - 1, 1)) {
12851285
$x2++;
12861286
$y2++;
12871287
}

src/DiffToolkit.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class DiffToolkit {
4040
public function commonPrefix($text1, $text2)
4141
{
4242
// Quick check for common null cases.
43-
if ($text1 == '' || $text2 == '' || mb_substr($text1, 0, 1) != mb_substr($text2, 0, 1)) {
43+
if ($text1 === '' || $text2 === '' || mb_substr($text1, 0, 1) !== mb_substr($text2, 0, 1)) {
4444
return 0;
4545
}
4646
// Binary search.
@@ -50,7 +50,7 @@ public function commonPrefix($text1, $text2)
5050
$pointermid = $pointermax;
5151
$pointerstart = 0;
5252
while ($pointermin < $pointermid) {
53-
if (mb_substr($text1, $pointerstart, $pointermid - $pointerstart) == mb_substr($text2, $pointerstart,
53+
if (mb_substr($text1, $pointerstart, $pointermid - $pointerstart) === mb_substr($text2, $pointerstart,
5454
$pointermid - $pointerstart)
5555
) {
5656
$pointermin = $pointermid;
@@ -75,7 +75,7 @@ public function commonPrefix($text1, $text2)
7575
public function commonSuffix($text1, $text2)
7676
{
7777
// Quick check for common null cases.
78-
if ($text1 == '' || $text2 == '' || mb_substr($text1, -1, 1) != mb_substr($text2, -1, 1)) {
78+
if ($text1 === '' || $text2 === '' || mb_substr($text1, -1, 1) !== mb_substr($text2, -1, 1)) {
7979
return 0;
8080
}
8181
// Binary search.
@@ -85,7 +85,7 @@ public function commonSuffix($text1, $text2)
8585
$pointermid = $pointermax;
8686
$pointerend = 0;
8787
while ($pointermin < $pointermid) {
88-
if (mb_substr($text1, -$pointermid, $pointermid - $pointerend) == mb_substr($text2, -$pointermid,
88+
if (mb_substr($text1, -$pointermid, $pointermid - $pointerend) === mb_substr($text2, -$pointermid,
8989
$pointermid - $pointerend)
9090
) {
9191
$pointermin = $pointermid;
@@ -127,7 +127,7 @@ public function commontOverlap($text1, $text2)
127127
$text_length = min($text1_length, $text2_length);
128128

129129
// Quick check for the worst case.
130-
if ($text1 == $text2) {
130+
if ($text1 === $text2) {
131131
return $text_length;
132132
}
133133

@@ -143,7 +143,7 @@ public function commontOverlap($text1, $text2)
143143
break;
144144
}
145145
$length += $found;
146-
if ($found == 0 || mb_substr($text1, -$length) == mb_substr($text2, 0, $length)) {
146+
if ($found == 0 || mb_substr($text1, -$length) === mb_substr($text2, 0, $length)) {
147147
$best = $length;
148148
$length += 1;
149149
}
@@ -256,7 +256,7 @@ protected function halfMatchI($longtext, $shorttext, $i)
256256
*/
257257
public function linesToChars($text1, $text2)
258258
{
259-
// e.g. $lineArray[4] == "Hello\n"
259+
// e.g. $lineArray[4] === "Hello\n"
260260
$lineArray = array();
261261
// e.g. $lineHash["Hello\n"] == 4
262262
$lineHash = array();

src/Matcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ public function main($text, $pattern, $loc = 0){
125125
}
126126

127127
$loc = max(0, min($loc, mb_strlen($text)));
128-
if ($text == $pattern) {
128+
if ($text === $pattern) {
129129
// Shortcut (potentially not guaranteed by the algorithm)
130130
return 0;
131-
} elseif ($text == '') {
131+
} elseif ($text === '') {
132132
// Nothing to match.
133133
return -1;
134134
} elseif (mb_substr($text, $loc, mb_strlen($pattern)) == $pattern) {

src/Patch.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,20 @@ public function fromText($patchText){
140140
}
141141
$patch = new PatchObject();
142142
$patch->setStart1($m[1]);
143-
if ($m[2] == '') {
143+
if ($m[2] === '') {
144144
$patch->setStart1($patch->getStart1() - 1);
145145
$patch->setLength1(1);
146-
} elseif ($m[2] == '0') {
146+
} elseif ($m[2] === '0') {
147147
$patch->setLength1(0);
148148
} else {
149149
$patch->setStart1($patch->getStart1() - 1);
150150
$patch->setLength1($m[2]);
151151
}
152152
$patch->setStart2($m[3]);
153-
if ($m[4] == '') {
153+
if ($m[4] === '') {
154154
$patch->setStart2($patch->getStart2() - 1);
155155
$patch->setLength2(1);
156-
} elseif ($m[4] == '0') {
156+
} elseif ($m[4] === '0') {
157157
$patch->setLength2(0);
158158
} else {
159159
$patch->setStart2($patch->getStart2() - 1);
@@ -235,7 +235,7 @@ public function addContext(PatchObject $patch, $text)
235235
// If two different matches are found, increase the pattern length.
236236
$matcher = $this->getMatcher();
237237
while (
238-
(!$pattern || mb_strpos($text, $pattern) !== mb_strrpos($text, $pattern)) &&
238+
($pattern === '' || mb_strpos($text, $pattern) !== mb_strrpos($text, $pattern)) &&
239239
($matcher->getMaxBits() == 0 || mb_strlen($pattern) < $matcher->getMaxBits() - 2 * $this->getMargin())
240240
) {
241241
$padding += $this->getMargin();
@@ -250,12 +250,12 @@ public function addContext(PatchObject $patch, $text)
250250

251251
// Add the prefix.
252252
$prefix = mb_substr($text, max(0, $patch->getStart2() - $padding), min($patch->getStart2(), $padding));
253-
if ($prefix != '') {
253+
if ($prefix !== '') {
254254
$patch->prependChanges(array(Diff::EQUAL, $prefix));
255255
}
256256
// Add the suffix.
257257
$suffix = mb_substr($text, $patch->getStart2() + $patch->getLength1(), $padding);
258-
if ($suffix != '') {
258+
if ($suffix !== '') {
259259
$patch->appendChanges(array(Diff::EQUAL, $suffix));
260260
}
261261

@@ -437,7 +437,7 @@ public function splitMax(&$patches)
437437
$patch->setStart1($start1 - $preContextLen);
438438
$patch->setStart2($start2 - $preContextLen);
439439

440-
if ($preContext != '') {
440+
if ($preContext !== '') {
441441
$patch->setLength1($preContextLen);
442442
$patch->setLength2($preContextLen);
443443
$patch->appendChanges(array(Diff::EQUAL, $preContext));
@@ -478,7 +478,7 @@ public function splitMax(&$patches)
478478
$empty = false;
479479
}
480480

481-
if ($diffText == $bigPatchDiffs[0][1]) {
481+
if ($diffText === $bigPatchDiffs[0][1]) {
482482
array_shift($bigPatchDiffs);
483483
} else {
484484
$bigPatchDiffs[0][1] = mb_substr($bigPatchDiffs[0][1], $diffTextLen);
@@ -497,7 +497,7 @@ public function splitMax(&$patches)
497497
$diff->setChanges($bigPatchDiffs);
498498
$postContext = $diff->text1();
499499
$postContext = mb_substr($postContext, 0, $this->getMargin());
500-
if ($postContext != '') {
500+
if ($postContext !== '') {
501501
$patch->setLength1($patch->getLength1() + mb_strlen($postContext));
502502
$patch->setLength2($patch->getLength2() + mb_strlen($postContext));
503503
if (
@@ -660,7 +660,7 @@ public function apply($patches, $text)
660660
} else {
661661
$text2 = mb_substr($text, $startLoc, $endLoc + $maxBits - $startLoc);
662662
}
663-
if ($text1 == $text2) {
663+
if ($text1 === $text2) {
664664
// Perfect match, just shove the replacement text in.
665665
$text = mb_substr($text, 0, $startLoc) . $diff->text2() .
666666
mb_substr($text, $startLoc + $text1Len);

tests/ChrBenchmark.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function unicodeChr5($code)
7575

7676
function unicodeOrd1($char)
7777
{
78-
if (mb_internal_encoding() != 'UCS-4LE') {
78+
if (mb_internal_encoding() !== 'UCS-4LE') {
7979
$char = iconv(mb_internal_encoding(), 'UCS-4LE', $char);
8080
}
8181
$code = 0;

tests/DiffTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,15 @@ public function testMain()
715715
$this->d->main("abc", "abc", false)->getChanges()
716716
);
717717

718+
// Check ===
719+
$this->assertEquals(
720+
array(
721+
array(Diff::INSERT, "0"),
722+
array(Diff::EQUAL, "1"),
723+
),
724+
$this->d->main("1", "01", false)->getChanges()
725+
);
726+
718727
// Check '0' strings
719728
$this->assertEquals(
720729
array(
@@ -921,4 +930,30 @@ function rebuildtexts($diffs) {
921930
}
922931
}
923932

933+
public function testNumeric()
934+
{
935+
$this->assertEquals(
936+
array(
937+
array(Diff::DELETE, "0"),
938+
array(Diff::INSERT, "1"),
939+
),
940+
$this->d->main("0", "1", false)->getChanges()
941+
);
942+
943+
$this->assertEquals(
944+
array(
945+
array(Diff::EQUAL, "0"),
946+
array(Diff::INSERT, "0"),
947+
),
948+
$this->d->main("0", "00", false)->getChanges()
949+
);
950+
951+
$this->assertEquals(
952+
array(
953+
array(Diff::INSERT, "0"),
954+
array(Diff::EQUAL, "1"),
955+
),
956+
$this->d->main("1", "01", false)->getChanges()
957+
);
958+
}
924959
}

0 commit comments

Comments
 (0)