I've created a large (2000) bytearray with a memoryview to read a UART into. The commands that the UART is receiving can sometimes take a while to arrive, so I'd like to check for a specific finish character sequence within the bytearray in a loop.
I've noticed that Micropython doesn't support the .find() method. What is the most efficient way of doing this without creating a copy of the bytearray? Ideally I could specify a start/finish index too like the find method. I have something rough below that i'm using but doesn't seem very efficient.
Code: Select all
def has_substring(input_string, pattern, start=0, end=-1): j=0 i=start k=0 if end == -1: l = len(input_string) else: l = end m = len(pattern) while((i<l) and (j<m)): if(input_string[i] == pattern[j]): i+=1 j+=1 else: j=0 k+=1 i=k if(j==m): return i else: return -1