Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store needle length #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pyscreeze/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,11 @@ def _kmp(needle, haystack, _dummy): # Knuth-Morris-Pratt search algorithm implem
"""
TODO
"""
needleLen = len(needle)
# build table of shift amounts
shifts = [1] * (len(needle) + 1)
shifts = [1] * (needleLen + 1)
shift = 1
for pos in range(len(needle)):
for pos in range(needleLen):
while shift <= pos and needle[pos] != needle[pos-shift]:
shift += shifts[pos-shift]
shifts[pos+1] = shift
Expand All @@ -507,12 +508,12 @@ def _kmp(needle, haystack, _dummy): # Knuth-Morris-Pratt search algorithm implem
startPos = 0
matchLen = 0
for c in haystack:
while matchLen == len(needle) or \
while matchLen == needleLen or \
matchLen >= 0 and needle[matchLen] != c:
startPos += shifts[matchLen]
matchLen -= shifts[matchLen]
matchLen += 1
if matchLen == len(needle):
if matchLen == needleLen:
yield startPos


Expand Down