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

Method diffHalfMatch

diffmatchpatch/diff.go:531–578  ·  view source on GitHub ↗
(text1, text2 []rune)

Source from the content-addressed store, hash-verified

529}
530
531func (dmp *DiffMatchPatch) diffHalfMatch(text1, text2 []rune) [][]rune {
532 if dmp.DiffTimeout <= 0 {
533 // Don't risk returning a non-optimal diff if we have unlimited time.
534 return nil
535 }
536
537 var longtext, shorttext []rune
538 if len(text1) > len(text2) {
539 longtext = text1
540 shorttext = text2
541 } else {
542 longtext = text2
543 shorttext = text1
544 }
545
546 if len(longtext) < 4 || len(shorttext)*2 < len(longtext) {
547 return nil // Pointless.
548 }
549
550 // First check if the second quarter is the seed for a half-match.
551 hm1 := dmp.diffHalfMatchI(longtext, shorttext, int(float64(len(longtext)+3)/4))
552
553 // Check again based on the third quarter.
554 hm2 := dmp.diffHalfMatchI(longtext, shorttext, int(float64(len(longtext)+1)/2))
555
556 hm := [][]rune{}
557 if hm1 == nil && hm2 == nil {
558 return nil
559 } else if hm2 == nil {
560 hm = hm1
561 } else if hm1 == nil {
562 hm = hm2
563 } else {
564 // Both matched. Select the longest.
565 if len(hm1[4]) > len(hm2[4]) {
566 hm = hm1
567 } else {
568 hm = hm2
569 }
570 }
571
572 // A half-match was found, sort out the return data.
573 if len(text1) > len(text2) {
574 return hm
575 }
576
577 return [][]rune{hm[2], hm[3], hm[0], hm[1], hm[4]}
578}
579
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.

Callers 2

diffComputeMethod · 0.95
DiffHalfMatchMethod · 0.95

Calls 1

diffHalfMatchIMethod · 0.95

Tested by

no test coverage detected