match_main method

int match_main(
  1. String? text,
  2. String? pattern,
  3. int loc
)

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. Returns the best match index or -1.

Implementation

int match_main(String? text, String? pattern, int loc) {
  // Check for null inputs.
  if (text == null || pattern == null) {
    throw ArgumentError('Null inputs. (match_main)');
  }

  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 _match_bitap(text, pattern, loc);
  }
}