MCPcopy Create free account
hub / github.com/sergi/go-diff / PatchApply

Method PatchApply

diffmatchpatch/patch.go:285–396  ·  view source on GitHub ↗

PatchApply merges a set of patches onto the text. Returns a patched text, as well as an array of true/false values indicating which patches were applied.

(patches []Patch, text string)

Source from the content-addressed store, hash-verified

283
284// PatchApply merges a set of patches onto the text. Returns a patched text, as well as an array of true/false values indicating which patches were applied.
285func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []bool) {
286 if len(patches) == 0 {
287 return text, []bool{}
288 }
289
290 // Deep copy the patches so that no changes are made to originals.
291 patches = dmp.PatchDeepCopy(patches)
292
293 nullPadding := dmp.PatchAddPadding(patches)
294 text = nullPadding + text + nullPadding
295 patches = dmp.PatchSplitMax(patches)
296
297 x := 0
298 // delta keeps track of the offset between the expected and actual location of the previous patch. If there are patches expected at positions 10 and 20, but the first patch was found at 12, delta is 2 and the second patch has an effective expected position of 22.
299 delta := 0
300 results := make([]bool, len(patches))
301 for _, aPatch := range patches {
302 expectedLoc := aPatch.Start2 + delta
303 text1 := dmp.DiffText1(aPatch.diffs)
304 var startLoc int
305 endLoc := -1
306 if len(text1) > dmp.MatchMaxBits {
307 // PatchSplitMax will only provide an oversized pattern in the case of a monster delete.
308 startLoc = dmp.MatchMain(text, text1[:dmp.MatchMaxBits], expectedLoc)
309 if startLoc != -1 {
310 endLoc = dmp.MatchMain(text,
311 text1[len(text1)-dmp.MatchMaxBits:], expectedLoc+len(text1)-dmp.MatchMaxBits)
312 if endLoc == -1 || startLoc >= endLoc {
313 // Can't find valid trailing context. Drop this patch.
314 startLoc = -1
315 }
316 }
317 } else {
318 startLoc = dmp.MatchMain(text, text1, expectedLoc)
319 }
320 // rune-safety: Bitap-returned byte position may land in the middle of a
321 // multi-byte character. Back it up to the nearest rune start so the
322 // subsequent text[startLoc:...] / text[:startLoc] slices never produce
323 // invalid UTF-8 (root cause of issue #132 panic).
324 if startLoc != -1 {
325 startLoc = alignRuneStart(text, startLoc)
326 }
327 if startLoc == -1 {
328 // No match found. :(
329 results[x] = false
330 // Subtract the delta for this failed patch from subsequent patches.
331 delta -= aPatch.Length2 - aPatch.Length1
332 } else {
333 // Found a match. :)
334 results[x] = true
335 delta = startLoc - expectedLoc
336 var text2 string
337 var text2End int
338 // rune-safety: round the end of text2 forward so it keeps the whole
339 // last rune; rounding back would drop it and a deletion of that rune
340 // would then silently do nothing.
341 if endLoc == -1 {
342 text2End = alignRuneEnd(text, int(math.Min(float64(startLoc+len(text1)), float64(len(text)))))

Calls 14

PatchDeepCopyMethod · 0.95
PatchAddPaddingMethod · 0.95
PatchSplitMaxMethod · 0.95
DiffText1Method · 0.95
MatchMainMethod · 0.95
DiffText2Method · 0.95
DiffMainMethod · 0.95
DiffLevenshteinMethod · 0.95
DiffXIndexMethod · 0.95
alignRuneStartFunction · 0.85
alignRuneEndFunction · 0.85