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

Method PatchApply

diffmatchpatch/patch.go:232–319  ·  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

230
231// 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.
232func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []bool) {
233 if len(patches) == 0 {
234 return text, []bool{}
235 }
236
237 // Deep copy the patches so that no changes are made to originals.
238 patches = dmp.PatchDeepCopy(patches)
239
240 nullPadding := dmp.PatchAddPadding(patches)
241 text = nullPadding + text + nullPadding
242 patches = dmp.PatchSplitMax(patches)
243
244 x := 0
245 // 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.
246 delta := 0
247 results := make([]bool, len(patches))
248 for _, aPatch := range patches {
249 expectedLoc := aPatch.Start2 + delta
250 text1 := dmp.DiffText1(aPatch.diffs)
251 var startLoc int
252 endLoc := -1
253 if len(text1) > dmp.MatchMaxBits {
254 // PatchSplitMax will only provide an oversized pattern in the case of a monster delete.
255 startLoc = dmp.MatchMain(text, text1[:dmp.MatchMaxBits], expectedLoc)
256 if startLoc != -1 {
257 endLoc = dmp.MatchMain(text,
258 text1[len(text1)-dmp.MatchMaxBits:], expectedLoc+len(text1)-dmp.MatchMaxBits)
259 if endLoc == -1 || startLoc >= endLoc {
260 // Can't find valid trailing context. Drop this patch.
261 startLoc = -1
262 }
263 }
264 } else {
265 startLoc = dmp.MatchMain(text, text1, expectedLoc)
266 }
267 if startLoc == -1 {
268 // No match found. :(
269 results[x] = false
270 // Subtract the delta for this failed patch from subsequent patches.
271 delta -= aPatch.Length2 - aPatch.Length1
272 } else {
273 // Found a match. :)
274 results[x] = true
275 delta = startLoc - expectedLoc
276 var text2 string
277 if endLoc == -1 {
278 text2 = text[startLoc:int(math.Min(float64(startLoc+len(text1)), float64(len(text))))]
279 } else {
280 text2 = text[startLoc:int(math.Min(float64(endLoc+dmp.MatchMaxBits), float64(len(text))))]
281 }
282 if text1 == text2 {
283 // Perfect match, just shove the Replacement text in.
284 text = text[:startLoc] + dmp.DiffText2(aPatch.diffs) + text[startLoc+len(text1):]
285 } else {
286 // Imperfect match. Run a diff to get a framework of equivalent indices.
287 diffs := dmp.DiffMain(text1, text2, false)
288 if len(text1) > dmp.MatchMaxBits && float64(dmp.DiffLevenshtein(diffs))/float64(len(text1)) > dmp.PatchDeleteThreshold {
289 // The end points match, but the content is unacceptably bad.

Callers 1

TestPatchApplyFunction · 0.80

Calls 10

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

Tested by 1

TestPatchApplyFunction · 0.64