Skip to content

Commit d37421a

Browse files
committed
Exposed DiffLinesToRunes and did other minor cleanup.
If clients clients use DiffLinesToRunes they can avoid an extra string to rune conversion. Also fixed some comments and got rid of an unnecessary helper function.
1 parent 4f4dcee commit d37421a

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

diffmatchpatch/dmp.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,22 @@ func (dmp *DiffMatchPatch) DiffMain(text1, text2 string, checklines bool) []Diff
245245
return dmp.diffMain(text1, text2, checklines, deadline)
246246
}
247247

248-
// DiffMain finds the differences between two rune sequences.
249248
func (dmp *DiffMatchPatch) diffMain(text1, text2 string, checklines bool, deadline time.Time) []Diff {
250-
diffs := []Diff{}
251-
if text1 == text2 {
252-
if len(text1) > 0 {
253-
diffs = append(diffs, Diff{DiffEqual, text1})
254-
}
255-
return diffs
249+
return dmp.diffMainRunes([]rune(text1), []rune(text2), checklines, deadline)
250+
}
251+
252+
// DiffMainRunes finds the differences between two rune sequences.
253+
func (dmp *DiffMatchPatch) DiffMainRunes(text1, text2 []rune, checklines bool) []Diff {
254+
var deadline time.Time
255+
if dmp.DiffTimeout <= 0 {
256+
deadline = time.Now().Add(24 * 365 * time.Hour)
257+
} else {
258+
deadline = time.Now().Add(dmp.DiffTimeout)
256259
}
257-
return dmp.diffMainRunes1([]rune(text1), []rune(text2), checklines, deadline)
260+
return dmp.diffMainRunes(text1, text2, checklines, deadline)
258261
}
259262

263+
260264
func (dmp *DiffMatchPatch) diffMainRunes(text1, text2 []rune, checklines bool, deadline time.Time) []Diff {
261265
if runesEqual(text1, text2) {
262266
var diffs []Diff
@@ -265,11 +269,6 @@ func (dmp *DiffMatchPatch) diffMainRunes(text1, text2 []rune, checklines bool, d
265269
}
266270
return diffs
267271
}
268-
return dmp.diffMainRunes1(text1, text2, checklines, deadline)
269-
}
270-
271-
272-
func (dmp *DiffMatchPatch) diffMainRunes1(text1, text2 []rune, checklines bool, deadline time.Time) []Diff {
273272
// Trim off common prefix (speedup).
274273
commonlength := commonPrefixLength(text1, text2)
275274
commonprefix := text1[:commonlength]
@@ -418,9 +417,13 @@ func (dmp *DiffMatchPatch) diffLineMode(text1, text2 []rune, deadline time.Time)
418417
// and return the recursively constructed diff.
419418
// See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations.
420419
func (dmp *DiffMatchPatch) DiffBisect(text1, text2 string, deadline time.Time) []Diff {
420+
// Unused in this code, but retained for interface compatibility.
421421
return dmp.diffBisect([]rune(text1), []rune(text2), deadline)
422422
}
423423

424+
// diffBisect finds the 'middle snake' of a diff, splits the problem in two
425+
// and returns the recursively constructed diff.
426+
// See Myers's 1986 paper: An O(ND) Difference Algorithm and Its Variations.
424427
func (dmp *DiffMatchPatch) diffBisect(text1, text2 []rune, deadline time.Time) []Diff {
425428
// Cache the text lengths to prevent multiple calls.
426429
runes1_len, runes2_len := len(runes1), len(runes2)
@@ -560,8 +563,7 @@ func (dmp *DiffMatchPatch) DiffLinesToChars(text1, text2 string) (string, string
560563
return string(chars1), string(chars2), lineArray
561564
}
562565

563-
// DiffLinesToChars split two texts into a list of []runes. Reduces the texts to a string of
564-
// hashes where each Unicode character represents one line.
566+
// DiffLinesToRunes splits two texts into a list of runes. Each rune represents one line.
565567
func (dmp *DiffMatchPatch) DiffLinesToRunes(text1, text2 string) ([]rune, []rune, []string) {
566568
// '\x00' is a valid character, but various debuggers don't like it.
567569
// So we'll insert a junk entry to avoid generating a null character.

diffmatchpatch/dmp_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,8 +1434,8 @@ func Benchmark_DiffMainLargeLines(b *testing.B) {
14341434
dmp := New()
14351435
b.ResetTimer()
14361436
for i := 0; i < b.N; i++ {
1437-
text1, text2, linearray := dmp.DiffLinesToChars(s1, s2)
1438-
diffs := dmp.DiffMain(text1, text2, false)
1437+
text1, text2, linearray := dmp.DiffLinesToRunes(s1, s2)
1438+
diffs := dmp.DiffMainRunes(text1, text2, false)
14391439
diffs = dmp.DiffCharsToLines(diffs, linearray)
14401440
}
14411441
}

0 commit comments

Comments
 (0)