A log viewer highlights search matches as you type, and it must stay fast on huge files where naive scanning stalls, such as a line of a million a characters.
A border of a string is a prefix that is also a suffix, other than the whole string. For example, "aba" and "a" are borders of "abacaba", and "" is a border of every string.
Write three functions. Characters are compared as JavaScript string units (s[i]), so no Unicode handling is needed.
prefixFunction(s) returns an array pi of length s.length, where pi[i] is the length of the longest border of s.slice(0, i + 1).
longestBorder(s) returns the longest border of s itself, or "" if it has none.
findAll(text, pattern) returns every index where pattern starts in text, ascending. Overlapping matches count.
Rules:
If an argument is not a string, throw a TypeError. If pattern is empty, throw a RangeError.
An empty s gives [] and "".
Strings can have 1,000,000 characters. Comparing prefixes and suffixes directly, or checking the pattern at every position of the text, is quadratic on inputs like "aaaa…" and will not finish.