Last active
August 14, 2017 14:53
-
-
Save alistair-broomhead/f10edcbade1465ac5aeb39e6ce03aa67 to your computer and use it in GitHub Desktop.
Python3 Byte Utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def until_bytes(input_bytes, ending): | |
""" | |
Yields bytes from input up to and including a given ending. | |
This can be used to seek to the end of a pattern if your | |
input_bytes object is an iterator. | |
""" | |
position = 0 | |
length = len(ending) | |
for byte in input_bytes: | |
yield byte | |
if byte != ending[position]: | |
position = 0 | |
continue | |
else: | |
position += 1 | |
if position == length: | |
break | |
else: | |
raise ValueError('{!r} not found'.format(ending)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment