@@ -24,24 +24,27 @@ public function render(MbString $mbOld, MbString $mbNew): LineRendererInterface
2424
2525 // using PREG_SPLIT_NO_EMPTY will make "wordGlues" work wrongly under some rare cases
2626 // failure case:
27- // old: "good-looking-"
28- // new: "good--"
29- // notice that after glueing, the 1st "-" in the new should be in the diff segment
27+ // old: "good-looking-x "
28+ // new: "good--y "
29+ // notice that after glueing, the 2nd "-" in the new should be in the diff segment
3030 $ pregFlag = \PREG_SPLIT_DELIM_CAPTURE ;
3131 $ oldWords = $ mbOld ->toArraySplit ($ splitRegex , -1 , $ pregFlag );
3232 $ newWords = $ mbNew ->toArraySplit ($ splitRegex , -1 , $ pregFlag );
3333
3434 $ hunk = $ this ->getChangedExtentSegments ($ oldWords , $ newWords );
3535
3636 // reversely iterate hunk
37- $ dummyDelIdxes = $ dummyInsIdxes = [];
3837 foreach (ReverseIterator::fromArray ($ hunk ) as [$ op , $ i1 , $ i2 , $ j1 , $ j2 ]) {
3938 if ($ op & (SequenceMatcher::OP_REP | SequenceMatcher::OP_DEL )) {
4039 $ oldWords [$ i1 ] = RendererConstant::HTML_CLOSURES [0 ] . $ oldWords [$ i1 ];
4140 $ oldWords [$ i2 - 1 ] .= RendererConstant::HTML_CLOSURES [1 ];
4241
42+ // insert dummy HTML closure to ensure there are always
43+ // the same amounts of HTML closures in $oldWords and $newWords
44+ // thus, this should make that "wordGlues" work correctly
45+ // @see https://github.com/jfcherng/php-diff/pull/25
4346 if ($ op === SequenceMatcher::OP_DEL ) {
44- $ dummyInsIdxes [] = $ j1 ;
47+ \array_splice ( $ newWords , $ j1, 0 , [ $ dummyHtmlClosure ]) ;
4548 }
4649 }
4750
@@ -50,22 +53,11 @@ public function render(MbString $mbOld, MbString $mbNew): LineRendererInterface
5053 $ newWords [$ j2 - 1 ] .= RendererConstant::HTML_CLOSURES [1 ];
5154
5255 if ($ op === SequenceMatcher::OP_INS ) {
53- $ dummyDelIdxes [] = $ i1 ;
56+ \array_splice ( $ oldWords , $ i1, 0 , [ $ dummyHtmlClosure ]) ;
5457 }
5558 }
5659 }
5760
58- // insert dummy HTML closure to make sure there are always
59- // the same amounts of HTML closures in $oldWords and $newWords
60- // thus, this should ensure that "wordGlues" works correctly
61- // @see https://github.com/jfcherng/php-diff/pull/25
62- foreach (ReverseIterator::fromArray ($ dummyDelIdxes ) as $ idx ) {
63- \array_splice ($ oldWords , $ idx , 0 , [$ dummyHtmlClosure ]);
64- }
65- foreach (ReverseIterator::fromArray ($ dummyInsIdxes ) as $ idx ) {
66- \array_splice ($ newWords , $ idx , 0 , [$ dummyHtmlClosure ]);
67- }
68-
6961 if (!empty ($ hunk ) && !empty ($ this ->rendererOptions ['wordGlues ' ])) {
7062 $ regexGlues = \array_map (
7163 function (string $ glue ): string {
0 commit comments