MatchMain locates the best instance of 'pattern' in 'text' near 'loc'. Returns -1 if no match found.
(text, pattern string, loc int)
| 15 | // MatchMain locates the best instance of 'pattern' in 'text' near 'loc'. |
| 16 | // Returns -1 if no match found. |
| 17 | func (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. |