wildcardFind function

int wildcardFind(
  1. dynamic needle,
  2. dynamic haystack
)

wildcardFind searches the haystack for a copy of needle. both needle and haystack must be Lists. Needle may contain null's, that position is then treated as a wildcard.

Implementation

int wildcardFind(dynamic needle, dynamic haystack) {
  final int? hl = haystack.length;
  final int? nl = needle.length;

  if (nl == 0) {
    return 0;
  }

  if (hl! < nl!) {
    return -1;
  }

  for (int i = 0; i <= (hl - nl); i++) {
    bool found = true;
    for (int j = 0; j < nl; j++) {
      if (needle[j] != null && (haystack[i + j] != needle[j])) {
        found = false;
        break;
      }
    }
    if (found) {
      return i;
    }
  }
  return -1;
}