Pocket Gems game clients may need to locate a command or token inside a larger string. Implement a function that returns the starting index of the first occurrence of pattern in text without using built-in substring search functions such as find, index, or regular expressions.
Use the Knuth-Morris-Pratt (KMP) pattern-matching algorithm so repeated characters in the pattern do not cause unnecessary comparisons.
Given two strings, text and pattern, return the smallest zero-based index i such that text[i:i + len(pattern)] == pattern. Return -1 if the pattern does not occur. An empty pattern is considered to occur at index 0.
def find_substring(text, pattern):