@@ -8,14 +8,30 @@ package diff
8
8
9
9
import (
10
10
"bytes"
11
+ "time"
11
12
12
13
"github.com/sergi/go-diff/diffmatchpatch"
13
14
)
14
15
15
16
// Do computes the (line oriented) modifications needed to turn the src
16
- // string into the dst string.
17
+ // string into the dst string. The underlying algorithm is Meyers,
18
+ // its complexity is O(N*d) where N is min(lines(src), lines(dst)) and d
19
+ // is the size of the diff.
17
20
func Do (src , dst string ) (diffs []diffmatchpatch.Diff ) {
21
+ // the default timeout is time.Second which may be too small under heavy load
22
+ return DoWithTimeout (src , dst , time .Hour )
23
+ }
24
+
25
+ // DoWithTimeout computes the (line oriented) modifications needed to turn the src
26
+ // string into the dst string. The `timeout` argument specifies the maximum
27
+ // amount of time it is allowed to spend in this function. If the timeout
28
+ // is exceeded, the parts of the strings which were not considered are turned into
29
+ // a bulk delete+insert and the half-baked suboptimal result is returned at once.
30
+ // The underlying algorithm is Meyers, its complexity is O(N*d) where N is
31
+ // min(lines(src), lines(dst)) and d is the size of the diff.
32
+ func DoWithTimeout (src , dst string , timeout time.Duration ) (diffs []diffmatchpatch.Diff ) {
18
33
dmp := diffmatchpatch .New ()
34
+ dmp .DiffTimeout = timeout
19
35
wSrc , wDst , warray := dmp .DiffLinesToRunes (src , dst )
20
36
diffs = dmp .DiffMainRunes (wSrc , wDst , false )
21
37
diffs = dmp .DiffCharsToLines (diffs , warray )
0 commit comments