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

Method PatchSplitMax

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

450// PatchSplitMax looks through the patches and breaks up any which are longer than the maximum limit of the match algorithm.
451// Intended to be called only from within patchApply.
452func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
453 patchSize := dmp.MatchMaxBits
454 for x := 0; x < len(patches); x++ {
455 if patches[x].Length1 <= patchSize {
456 continue
457 }
458 bigpatch := patches[x]
459 // Remove the big old patch.
460 patches = append(patches[:x], patches[x+1:]...)
461 x--
462
463 Start1 := bigpatch.Start1
464 Start2 := bigpatch.Start2
465 precontext := ""
466 for len(bigpatch.diffs) != 0 {
467 // Create one of several smaller patches.
468 patch := Patch{}
469 empty := true
470 patch.Start1 = Start1 - len(precontext)
471 patch.Start2 = Start2 - len(precontext)
472 if len(precontext) != 0 {
473 patch.Length1 = len(precontext)
474 patch.Length2 = len(precontext)
475 patch.diffs = append(patch.diffs, Diff{DiffEqual, precontext})
476 }
477 for len(bigpatch.diffs) != 0 && patch.Length1 < patchSize-dmp.PatchMargin {
478 diffType := bigpatch.diffs[0].Type
479 diffText := bigpatch.diffs[0].Text
480 if diffType == DiffInsert {
481 // Insertions are harmless.
482 patch.Length2 += len(diffText)
483 Start2 += len(diffText)
484 patch.diffs = append(patch.diffs, bigpatch.diffs[0])
485 bigpatch.diffs = bigpatch.diffs[1:]
486 empty = false
487 } else if diffType == DiffDelete && len(patch.diffs) == 1 && patch.diffs[0].Type == DiffEqual && len(diffText) > 2*patchSize {
488 // This is a large deletion. Let it pass in one chunk.
489 patch.Length1 += len(diffText)
490 Start1 += len(diffText)
491 empty = false
492 patch.diffs = append(patch.diffs, Diff{diffType, diffText})
493 bigpatch.diffs = bigpatch.diffs[1:]
494 } else {
495 // Deletion or equality. Only take as much as we can stomach.
496 // rune-safety: align cutAt to nearest rune boundary so we never
497 // slice diffText through a multi-byte character (the resulting
498 // invalid UTF-8 is the root cause of issue #132 panic).
499 cutAt := min(len(diffText), patchSize-patch.Length1-dmp.PatchMargin)
500 cutAt = alignRuneStart(diffText, cutAt)
501 // Loop-progress guard: when alignment collapses cutAt to 0 but
502 // diffText is non-empty, advance by exactly one rune so the
503 // outer for-loop is guaranteed to consume input each iteration.
504 if cutAt == 0 && len(diffText) > 0 {
505 _, size := utf8.DecodeRuneInString(diffText)
506 cutAt = size
507 }
508 diffText = diffText[:cutAt]
509

Callers 4

PatchApplyMethod · 0.95
TestPatchSplitMaxFunction · 0.80
BenchmarkPatchSplitMaxFunction · 0.80

Calls 5

DiffText2Method · 0.95
minFunction · 0.85
alignRuneStartFunction · 0.85
maxFunction · 0.85
diffText1PrefixFunction · 0.85

Tested by 3

TestPatchSplitMaxFunction · 0.64
BenchmarkPatchSplitMaxFunction · 0.64