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

Method DiffCommonOverlap

diffmatchpatch/diff.go:477–514  ·  view source on GitHub ↗

DiffCommonOverlap determines if the suffix of one string is the prefix of another.

(text1 string, text2 string)

Source from the content-addressed store, hash-verified

475
476// DiffCommonOverlap determines if the suffix of one string is the prefix of another.
477func (dmp *DiffMatchPatch) DiffCommonOverlap(text1 string, text2 string) int {
478 // Cache the text lengths to prevent multiple calls.
479 text1Length := len(text1)
480 text2Length := len(text2)
481 // Eliminate the null case.
482 if text1Length == 0 || text2Length == 0 {
483 return 0
484 }
485 // Truncate the longer string.
486 if text1Length > text2Length {
487 text1 = text1[text1Length-text2Length:]
488 } else if text1Length < text2Length {
489 text2 = text2[0:text1Length]
490 }
491 textLength := int(math.Min(float64(text1Length), float64(text2Length)))
492 // Quick check for the worst case.
493 if text1 == text2 {
494 return textLength
495 }
496
497 // Start by looking for a single character match and increase length until no match is found. Performance analysis: http://neil.fraser.name/news/2010/11/04/
498 best := 0
499 length := 1
500 for {
501 pattern := text1[textLength-length:]
502 found := strings.Index(text2, pattern)
503 if found == -1 {
504 break
505 }
506 length += found
507 if found == 0 || text1[textLength-length:] == text2[0:length] {
508 best = length
509 length++
510 }
511 }
512
513 return best
514}
515
516// DiffHalfMatch checks whether the two texts share a substring which is at least half the length of the longer text. This speedup can produce non-minimal diffs.
517func (dmp *DiffMatchPatch) DiffHalfMatch(text1, text2 string) []string {

Callers 2

DiffCleanupSemanticMethod · 0.95
TestDiffCommonOverlapFunction · 0.80

Calls

no outgoing calls

Tested by 1

TestDiffCommonOverlapFunction · 0.64