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

Method PatchSplitMax

diffmatchpatch/patch.go:375–466  ·  view source on GitHub ↗

PatchSplitMax looks through the patches and breaks up any which are longer than the maximum limit of the match algorithm. Intended to be called only from within patchApply.

(patches []Patch)

Source from the content-addressed store, hash-verified

373// PatchSplitMax looks through the patches and breaks up any which are longer than the maximum limit of the match algorithm.
374// Intended to be called only from within patchApply.
375func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
376 patchSize := dmp.MatchMaxBits
377 for x := 0; x < len(patches); x++ {
378 if patches[x].Length1 <= patchSize {
379 continue
380 }
381 bigpatch := patches[x]
382 // Remove the big old patch.
383 patches = append(patches[:x], patches[x+1:]...)
384 x--
385
386 Start1 := bigpatch.Start1
387 Start2 := bigpatch.Start2
388 precontext := ""
389 for len(bigpatch.diffs) != 0 {
390 // Create one of several smaller patches.
391 patch := Patch{}
392 empty := true
393 patch.Start1 = Start1 - len(precontext)
394 patch.Start2 = Start2 - len(precontext)
395 if len(precontext) != 0 {
396 patch.Length1 = len(precontext)
397 patch.Length2 = len(precontext)
398 patch.diffs = append(patch.diffs, Diff{DiffEqual, precontext})
399 }
400 for len(bigpatch.diffs) != 0 && patch.Length1 < patchSize-dmp.PatchMargin {
401 diffType := bigpatch.diffs[0].Type
402 diffText := bigpatch.diffs[0].Text
403 if diffType == DiffInsert {
404 // Insertions are harmless.
405 patch.Length2 += len(diffText)
406 Start2 += len(diffText)
407 patch.diffs = append(patch.diffs, bigpatch.diffs[0])
408 bigpatch.diffs = bigpatch.diffs[1:]
409 empty = false
410 } else if diffType == DiffDelete && len(patch.diffs) == 1 && patch.diffs[0].Type == DiffEqual && len(diffText) > 2*patchSize {
411 // This is a large deletion. Let it pass in one chunk.
412 patch.Length1 += len(diffText)
413 Start1 += len(diffText)
414 empty = false
415 patch.diffs = append(patch.diffs, Diff{diffType, diffText})
416 bigpatch.diffs = bigpatch.diffs[1:]
417 } else {
418 // Deletion or equality. Only take as much as we can stomach.
419 diffText = diffText[:min(len(diffText), patchSize-patch.Length1-dmp.PatchMargin)]
420
421 patch.Length1 += len(diffText)
422 Start1 += len(diffText)
423 if diffType == DiffEqual {
424 patch.Length2 += len(diffText)
425 Start2 += len(diffText)
426 } else {
427 empty = false
428 }
429 patch.diffs = append(patch.diffs, Diff{diffType, diffText})
430 if diffText == bigpatch.diffs[0].Text {
431 bigpatch.diffs = bigpatch.diffs[1:]
432 } else {

Callers 2

PatchApplyMethod · 0.95
TestPatchSplitMaxFunction · 0.80

Calls 4

DiffText2Method · 0.95
DiffText1Method · 0.95
minFunction · 0.85
maxFunction · 0.85

Tested by 1

TestPatchSplitMaxFunction · 0.64