Skip to content

Commit

Permalink
Merge pull request #90 from vmarkovtsev/patch-1
Browse files Browse the repository at this point in the history
Fix DiffLevenshtein counting single runes as multiple edits
  • Loading branch information
sergi authored Feb 5, 2018
2 parents 217d392 + f7f9a5c commit da64554
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions diffmatchpatch/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1236,9 +1236,9 @@ func (dmp *DiffMatchPatch) DiffLevenshtein(diffs []Diff) int {
for _, aDiff := range diffs {
switch aDiff.Type {
case DiffInsert:
insertions += len(aDiff.Text)
insertions += utf8.RuneCountInString(aDiff.Text)
case DiffDelete:
deletions += len(aDiff.Text)
deletions += utf8.RuneCountInString(aDiff.Text)
case DiffEqual:
// A deletion and an insertion is one substitution.
levenshtein += max(insertions, deletions)
Expand Down
6 changes: 3 additions & 3 deletions diffmatchpatch/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1153,9 +1153,9 @@ func TestDiffLevenshtein(t *testing.T) {
dmp := New()

for i, tc := range []TestCase{
{"Levenshtein with trailing equality", []Diff{{DiffDelete, "abc"}, {DiffInsert, "1234"}, {DiffEqual, "xyz"}}, 4},
{"Levenshtein with leading equality", []Diff{{DiffEqual, "xyz"}, {DiffDelete, "abc"}, {DiffInsert, "1234"}}, 4},
{"Levenshtein with middle equality", []Diff{{DiffDelete, "abc"}, {DiffEqual, "xyz"}, {DiffInsert, "1234"}}, 7},
{"Levenshtein with trailing equality", []Diff{{DiffDelete, "абв"}, {DiffInsert, "1234"}, {DiffEqual, "эюя"}}, 4},
{"Levenshtein with leading equality", []Diff{{DiffEqual, "эюя"}, {DiffDelete, "абв"}, {DiffInsert, "1234"}}, 4},
{"Levenshtein with middle equality", []Diff{{DiffDelete, "абв"}, {DiffEqual, "эюя"}, {DiffInsert, "1234"}}, 7},
} {
actual := dmp.DiffLevenshtein(tc.Diffs)
assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %s", i, tc.Name))
Expand Down

0 comments on commit da64554

Please sign in to comment.