DiffCommonOverlap determines if the suffix of one string is the prefix of another.
(text1 string, text2 string)
| 458 | |
| 459 | // DiffCommonOverlap determines if the suffix of one string is the prefix of another. |
| 460 | func (dmp *DiffMatchPatch) DiffCommonOverlap(text1 string, text2 string) int { |
| 461 | // Cache the text lengths to prevent multiple calls. |
| 462 | text1Length := len(text1) |
| 463 | text2Length := len(text2) |
| 464 | // Eliminate the null case. |
| 465 | if text1Length == 0 || text2Length == 0 { |
| 466 | return 0 |
| 467 | } |
| 468 | // Truncate the longer string. |
| 469 | if text1Length > text2Length { |
| 470 | text1 = text1[text1Length-text2Length:] |
| 471 | } else if text1Length < text2Length { |
| 472 | text2 = text2[0:text1Length] |
| 473 | } |
| 474 | textLength := int(math.Min(float64(text1Length), float64(text2Length))) |
| 475 | // Quick check for the worst case. |
| 476 | if text1 == text2 { |
| 477 | return textLength |
| 478 | } |
| 479 | |
| 480 | // 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/ |
| 481 | best := 0 |
| 482 | length := 1 |
| 483 | for { |
| 484 | pattern := text1[textLength-length:] |
| 485 | found := strings.Index(text2, pattern) |
| 486 | if found == -1 { |
| 487 | break |
| 488 | } |
| 489 | length += found |
| 490 | if found == 0 || text1[textLength-length:] == text2[0:length] { |
| 491 | best = length |
| 492 | length++ |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return best |
| 497 | } |
| 498 | |
| 499 | // 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. |
| 500 | func (dmp *DiffMatchPatch) DiffHalfMatch(text1, text2 string) []string { |
no outgoing calls