Browse Source

Bm: Check edge conditions like empty strings for the search text

master
Nathan Bergey 2 years ago
parent
commit
502b64454b
  1. 4
      src/bm.py
  2. 4
      test/test_bm.py

4
src/bm.py

@ -58,12 +58,14 @@ class Bm(object):
Return the index of the *first* occurrence of the pattern in the
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
# 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
# loop until we reach the end of the text
PATTERN_LENGTH = len(self.pattern)
head = PATTERN_LENGTH - 1
while head < len(self.text):
# When the last character of the pattern matches the current head

4
test/test_bm.py

@ -14,6 +14,10 @@ from src.bm import Bm
# Other useful test cases:
("ユニコード", "コード", 2),
("", "", -1),
("a", "", -1),
("", "a", -1),
("t", "too long", -1),
])
def test_bm(text, pattern, expected):
bm = Bm(text, pattern)

Loading…
Cancel
Save