@@ -8,14 +8,30 @@ package diff
88
99import (
1010 "bytes"
11+ "time"
1112
1213 "github.com/sergi/go-diff/diffmatchpatch"
1314)
1415
1516// 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.
1720func 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 ) {
1833 dmp := diffmatchpatch .New ()
34+ dmp .DiffTimeout = timeout
1935 wSrc , wDst , warray := dmp .DiffLinesToRunes (src , dst )
2036 diffs = dmp .DiffMainRunes (wSrc , wDst , false )
2137 diffs = dmp .DiffCharsToLines (diffs , warray )
0 commit comments