match function
Locate the best instance of pattern
in text
near loc
.
Returns -1 if no match found.
text
is the text to search.pattern
is the pattern to search for.loc
is the location to search around.threshold
At what point is no match declared (0.0 = perfection, 1.0 = very loose).distance
How far to search for a match (0 = exact location, 1000+ = broad match). A match this many characters away from the expected location will add 1.0 to the score (0.0 is a perfect match).
Returns the best match index or -1.
Implementation
int match(String text, String pattern, int loc,
{double threshold: 0.5, int distance: 1000}) {
loc = max(0, min(loc, text.length));
if (text == pattern) {
// Shortcut (potentially not guaranteed by the algorithm)
return 0;
} else if (text.length == 0) {
// Nothing to match.
return -1;
} else if (loc + pattern.length <= text.length
&& text.substring(loc, loc + pattern.length) == pattern) {
// Perfect match at the perfect spot! (Includes case of null pattern)
return loc;
} else {
// Do a fuzzy compare.
return matchBitap(text, pattern, loc, threshold, distance);
}
}