|
@ -58,12 +58,14 @@ class Bm(object): |
|
|
Return the index of the *first* occurrence of the pattern in the |
|
|
Return the index of the *first* occurrence of the pattern in the |
|
|
text, or '-1' if the pattern is not in the text. |
|
|
text, or '-1' if the pattern is not in the text. |
|
|
""" |
|
|
""" |
|
|
|
|
|
PATTERN_LENGTH = len(self.pattern) |
|
|
|
|
|
if not self.pattern or not self.text or len(self.text) < PATTERN_LENGTH: |
|
|
|
|
|
return -1 |
|
|
|
|
|
|
|
|
# The search works on one character at a time in the text. The current |
|
|
# The search works on one character at a time in the text. The current |
|
|
# location is the "head" (like a read-head on a disk) |
|
|
# location is the "head" (like a read-head on a disk) |
|
|
# It starts at the last character in the pattern, and gets moved in the |
|
|
# It starts at the last character in the pattern, and gets moved in the |
|
|
# loop until we reach the end of the text |
|
|
# loop until we reach the end of the text |
|
|
PATTERN_LENGTH = len(self.pattern) |
|
|
|
|
|
head = PATTERN_LENGTH - 1 |
|
|
head = PATTERN_LENGTH - 1 |
|
|
while head < len(self.text): |
|
|
while head < len(self.text): |
|
|
# When the last character of the pattern matches the current head |
|
|
# When the last character of the pattern matches the current head |
|
|