UNPKG

@thi.ng/arrays

Version:

Array / Arraylike utilities

23 lines (22 loc) 462 B
function findSequence(buf, needle, start = 0) { const m = buf.length; const n = needle.length; const max = m - n; if (max < 0) return -1; while (start < m) { const idx = buf.indexOf(needle[0], start); if (idx < 0 || idx > max) return -1; let j = n; while (j-- > 1) { if (buf[idx + j] !== needle[j]) { start = idx + 1; break; } } if (j === 0) return idx; } return -1; } export { findSequence };