diff --git a/src/byteb4rb1e_utils/string.py b/src/byteb4rb1e_utils/string.py index 3261a03..f59babf 100644 --- a/src/byteb4rb1e_utils/string.py +++ b/src/byteb4rb1e_utils/string.py @@ -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