From b9a67eb0a596c85c29621fa18316b07eaf231dea Mon Sep 17 00:00:00 2001 From: "Rodney, Tiara" Date: Sun, 4 May 2025 02:34:05 +0200 Subject: [PATCH] feat(string): add circular buffer support for KMP search --- src/byteb4rb1e_utils/string.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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