feat(string): add circular buffer support for KMP search
This commit is contained in:
parent
6f267c29e6
commit
b9a67eb0a5
1 changed files with 17 additions and 0 deletions
|
|
@ -49,3 +49,20 @@ class KnuthMorrisPratt:
|
|||
if j == m:
|
||||
return i - m + 1
|
||||
return -1
|
||||
|
||||
def match_circular(self, data: CircularBuffer):
|
||||
"""Finds the boundary using KMP, handling circular wraparound."""
|
||||
i, j = data.start, 0 # Start checking from the oldest data in the buffer
|
||||
|
||||
while j < len(boundary) and (data.filled or i != data.end):
|
||||
if data.buf[i] == self.pattern[j]:
|
||||
i = (i + 1) % data.size
|
||||
j += 1
|
||||
if j == len(self.pattern): # Full match found
|
||||
return True
|
||||
elif j > 0:
|
||||
j = table[j - 1]
|
||||
else:
|
||||
i = (i + 1) % data.size
|
||||
|
||||
return False
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue