Decoder.try_next

This commit is contained in:
Tony Garnock-Jones 2018-11-18 16:59:13 +00:00
parent 10d8eb1b0a
commit ecdf123358
2 changed files with 14 additions and 2 deletions

View File

@ -179,11 +179,15 @@ class SymbolStream(BinaryStream):
minor = 3
class Decoder(Codec):
def __init__(self, packet):
def __init__(self, packet=b''):
super(Decoder, self).__init__()
self.packet = packet
self.index = 0
def extend(self, data):
self.packet = self.packet[self.index:] + data
self.index = 0
def nextbyte(self):
if self.index >= len(self.packet):
raise ShortPacket('Short packet')
@ -299,6 +303,14 @@ class Decoder(Codec):
else: # major == 3
return self.decodecollection(minor, self.nextvalues(self.wirelength(arg)))
def try_next(self):
start = self.index
try:
return self.next()
except ShortPacket:
self.index = start
return None
class Encoder(Codec):
def __init__(self):
super(Encoder, self).__init__()

View File

@ -5,7 +5,7 @@ except ImportError:
setup(
name="preserves",
version="0.0.1",
version="0.0.2",
author="Tony Garnock-Jones",
author_email="tonyg@leastfixedpoint.com",
license="GNU General Public License v3 or later (GPLv3+)",