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

Method diffHalfMatchI

diffmatchpatch/diff.go:582–620  ·  view source on GitHub ↗

diffHalfMatchI checks if a substring of shorttext exist within longtext such that the substring is at least half the length of longtext? Returns a slice containing the prefix of longtext, the suffix of longtext, the prefix of shorttext, the suffix of shorttext and the common middle, or null if there

(l, s []rune, i int)

Source from the content-addressed store, hash-verified

580// diffHalfMatchI checks if a substring of shorttext exist within longtext such that the substring is at least half the length of longtext?
581// Returns a slice containing the prefix of longtext, the suffix of longtext, the prefix of shorttext, the suffix of shorttext and the common middle, or null if there was no match.
582func (dmp *DiffMatchPatch) diffHalfMatchI(l, s []rune, i int) [][]rune {
583 var bestCommonA []rune
584 var bestCommonB []rune
585 var bestCommonLen int
586 var bestLongtextA []rune
587 var bestLongtextB []rune
588 var bestShorttextA []rune
589 var bestShorttextB []rune
590
591 // Start with a 1/4 length substring at position i as a seed.
592 seed := l[i : i+len(l)/4]
593
594 for j := runesIndexOf(s, seed, 0); j != -1; j = runesIndexOf(s, seed, j+1) {
595 prefixLength := commonPrefixLength(l[i:], s[j:])
596 suffixLength := commonSuffixLength(l[:i], s[:j])
597
598 if bestCommonLen < suffixLength+prefixLength {
599 bestCommonA = s[j-suffixLength : j]
600 bestCommonB = s[j : j+prefixLength]
601 bestCommonLen = len(bestCommonA) + len(bestCommonB)
602 bestLongtextA = l[:i-suffixLength]
603 bestLongtextB = l[i+prefixLength:]
604 bestShorttextA = s[:j-suffixLength]
605 bestShorttextB = s[j+prefixLength:]
606 }
607 }
608
609 if bestCommonLen*2 < len(l) {
610 return nil
611 }
612
613 return [][]rune{
614 bestLongtextA,
615 bestLongtextB,
616 bestShorttextA,
617 bestShorttextB,
618 append(append([]rune(nil), bestCommonA...), bestCommonB...),
619 }
620}
621
622// DiffCleanupSemantic reduces the number of edits by eliminating semantically trivial equalities.
623func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {

Callers 1

diffHalfMatchMethod · 0.95

Calls 3

runesIndexOfFunction · 0.85
commonPrefixLengthFunction · 0.85
commonSuffixLengthFunction · 0.85

Tested by

no test coverage detected