Skip to content

Fix replacements mispairing #57

Open
humorless wants to merge 2 commits intomainfrom
laurence/Fix-replacements-mispairing-when-second-seq-is-longer
Open

Fix replacements mispairing #57
humorless wants to merge 2 commits intomainfrom
laurence/Fix-replacements-mispairing-when-second-seq-is-longer

Conversation

@humorless
Copy link
Member

Fix replacements mispairing deletions and insertions when second seq is longer

When deep-diff places all insertions at the last index of the original
sequence, early deletions couldn't find their matching insertion because
replacements only checked positions d and d-1. Add a fallback that finds
the nearest insertion key > d, but only when all indices between d and
that key are also being deleted (no surviving elements in between).

This ensures correct positional pairing without false matches.

Example: (diff '({:a 1} {:b 1}) '({:a 2} {:b 2} {:c 2}))

Before (wrong):
[Deletion{:a 1} {Deletion{:b} 1, Insertion{:a} 2} Insertion{:b 2} Insertion{:c 2}]

After (correct):
[{:a Mismatch{1→2}} {:b Mismatch{1→2}} Insertion{:c 2}]

…is longer

When deep-diff places all insertions at the last index of the original
 sequence, early deletions couldn't find their matching insertion because
 replacements only checked positions d and d-1. Add a fallback that finds
 the nearest insertion key > d, but only when all indices between d and
 that key are also being deleted (no surviving elements in between).

 This ensures correct positional pairing without false matches.

 Example: (diff '({:a 1} {:b 1}) '({:a 2} {:b 2} {:c 2}))

 Before (wrong):
 [Deletion{:a 1}  {Deletion{:b} 1, Insertion{:a} 2}  Insertion{:b 2}  Insertion{:c 2}]

 After (correct):
 [{:a Mismatch{1→2}}  {:b Mismatch{1→2}}  Insertion{:c 2}]
@humorless humorless requested a review from joannecheng March 5, 2026 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant