Skip to content

Commit da64554

Browse files
authored
Merge pull request #90 from vmarkovtsev/patch-1
Fix DiffLevenshtein counting single runes as multiple edits
2 parents 217d392 + f7f9a5c commit da64554

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

diffmatchpatch/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,9 +1236,9 @@ func (dmp *DiffMatchPatch) DiffLevenshtein(diffs []Diff) int {
12361236
for _, aDiff := range diffs {
12371237
switch aDiff.Type {
12381238
case DiffInsert:
1239-
insertions += len(aDiff.Text)
1239+
insertions += utf8.RuneCountInString(aDiff.Text)
12401240
case DiffDelete:
1241-
deletions += len(aDiff.Text)
1241+
deletions += utf8.RuneCountInString(aDiff.Text)
12421242
case DiffEqual:
12431243
// A deletion and an insertion is one substitution.
12441244
levenshtein += max(insertions, deletions)

diffmatchpatch/diff_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,9 +1153,9 @@ func TestDiffLevenshtein(t *testing.T) {
11531153
dmp := New()
11541154

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

0 commit comments

Comments
 (0)