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

Method MatchMain

diffmatchpatch/match.go:17–33  ·  view source on GitHub ↗

MatchMain locates the best instance of 'pattern' in 'text' near 'loc'. Returns -1 if no match found.

(text, pattern string, loc int)

Source from the content-addressed store, hash-verified

15// MatchMain locates the best instance of 'pattern' in 'text' near 'loc'.
16// Returns -1 if no match found.
17func (dmp *DiffMatchPatch) MatchMain(text, pattern string, loc int) int {
18 // Check for null inputs not needed since null can't be passed in C#.
19
20 loc = int(math.Max(0, math.Min(float64(loc), float64(len(text)))))
21 if text == pattern {
22 // Shortcut (potentially not guaranteed by the algorithm)
23 return 0
24 } else if len(text) == 0 {
25 // Nothing to match.
26 return -1
27 } else if loc+len(pattern) <= len(text) && text[loc:loc+len(pattern)] == pattern {
28 // Perfect match at the perfect spot! (Includes case of null pattern)
29 return loc
30 }
31 // Do a fuzzy compare.
32 return dmp.MatchBitap(text, pattern, loc)
33}
34
35// MatchBitap locates the best instance of 'pattern' in 'text' near 'loc' using the Bitap algorithm.
36// Returns -1 if no match was found.

Callers 2

PatchApplyMethod · 0.95
TestMatchMainFunction · 0.80

Calls 1

MatchBitapMethod · 0.95

Tested by 1

TestMatchMainFunction · 0.64