findSubArray function

dynamic findSubArray(
  1. dynamic needle,
  2. dynamic haystack
)

Implementation

findSubArray(needle, haystack) {
    var needleLength = needle.length;
    var limit = haystack.length - needleLength + 1;

    loop:
    for (var pos = 0; pos < limit; pos++) {
        for (; pos < limit; pos++) {
            for (var k = 0; k < needleLength; k++) {
                if (haystack[pos + k] != needle[k]) {
                    continue loop;
                }
            }

            return pos;
        }
    }

    return -1;
}