feat(string): add circular buffer support for KMP search

This commit is contained in:
Rodney, Tiara 2025-05-04 02:34:05 +02:00
parent 6f267c29e6
commit b9a67eb0a5
No known key found for this signature in database
GPG key ID: 5CD8EC1D46106723

View file

@ -49,3 +49,20 @@ class KnuthMorrisPratt:
if j == m: if j == m:
return i - m + 1 return i - m + 1
return -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