diff --git a/internal/parser/diff/myers.go b/internal/parser/diff/myers.go index 7ded268..405f271 100644 --- a/internal/parser/diff/myers.go +++ b/internal/parser/diff/myers.go @@ -119,11 +119,13 @@ func diffInternal(e, f []any, equals func(any, any) bool, i, j int) []Op { * e.g -1%3 = 2. * In golang it matches the sign of the numerator. * See https://en.wikipedia.org/wiki/Modulo_operation#Variants_of_the_definition - * Since we always have a positive denominator here, we can emulate the - * pyMod x%y as (x+y) % y */ func pyMod(x, y int) int { - return (x + y) % y + mod := x % y + if mod < 0 { + mod += y + } + return mod } // Let us map element in same way as in diff --git a/internal/parser/diff/myers_test.go b/internal/parser/diff/myers_test.go index 3b0d4ed..b32e125 100644 --- a/internal/parser/diff/myers_test.go +++ b/internal/parser/diff/myers_test.go @@ -18,6 +18,7 @@ func TestDiff(t *t.T) { testCases := []TestCase{ {[]string{}, []string{}, []Op{}}, {[]string{}, []string{"foo"}, []Op{{OpInsert, 0, 0, "foo"}}}, + {[]string{"foo"}, []string{}, []Op{{OpDelete, 0, -1, "foo"}}}, {[]string{"foo", "bar", "baz"}, []string{"foo", "bar", "baz"}, []Op{}}, {[]string{"foo", "bar", "baz"}, []string{"foo", "baz"}, []Op{{OpDelete, 1, -1, "bar"}}}, {[]string{"baz"}, []string{"foo", "baz"}, []Op{{OpInsert, 0, 0, "foo"}}},