Skip to content

Commit

Permalink
read_bytes_term(): additional minor performance improvements
Browse files Browse the repository at this point in the history
See the `new_v2` version at
https://gist.github.com/generalmimon/41dcb2bba48cb47c2f30ce9f086cbbf2 -
it's about 10% faster than the existing `new` version.
  • Loading branch information
generalmimon committed Jul 22, 2024
1 parent 3bfbbe6 commit 812ae7e
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions kaitaistruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,11 @@ def read_bytes_full(self):

def read_bytes_term(self, term, include_term, consume_term, eos_error):
self.align_to_byte()
term_byte = KaitaiStream.byte_from_int(term)
r = bytearray()
while True:
c = self._io.read(1)
if c == b'':
if not c:
if eos_error:
raise Exception(
"end of stream reached, but no terminator %d found" %
Expand All @@ -421,7 +422,7 @@ def read_bytes_term(self, term, include_term, consume_term, eos_error):

return bytes(r)

if ord(c) == term:
if c == term_byte:
if include_term:
r += c
if not consume_term:
Expand Down

0 comments on commit 812ae7e

Please sign in to comment.