Skip to content

Instantly share code, notes, and snippets.

@alistair-broomhead
Last active August 14, 2017 14:53
Show Gist options
  • Save alistair-broomhead/f10edcbade1465ac5aeb39e6ce03aa67 to your computer and use it in GitHub Desktop.
Save alistair-broomhead/f10edcbade1465ac5aeb39e6ce03aa67 to your computer and use it in GitHub Desktop.
Python3 Byte Utils
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