This is an archive of the discontinued Mercurial Phabricator instance.

cborutil: add a buffering decoder
ClosedPublic

Authored by indygreg on Aug 31 2018, 6:57 PM.

Details

Summary

The sansiodecoder leaves it up to the callers to feed in data that
wasn't fully consumed last time.

This commit implements a decoder that performs buffering of
leftover chunks from the previous invocation. It otherwise
behaves identically to sansiodecoder.

Diff Detail

Repository
rHG Mercurial
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

indygreg created this revision.Aug 31 2018, 6:57 PM
yuja added a subscriber: yuja.Sep 3 2018, 9:38 AM

Queued up to this patch, thanks. The decoder looks promising.

+ def decode(self, b):
+ """Attempt to decode bytes to CBOR values.
+
+ Returns a tuple with the following fields:
+
+ * Bool indicating whether new values are available for retrieval.
+ * Integer number of bytes decoded from the new input.
+ * Integer number of bytes wanted to decode the next value.
+ """
+
+ if self._leftover:
+ oldlen = len(self._leftover)
+ b = self._leftover + b
+ self._leftover = None
+ else:
+ b = b
+ oldlen = 0
+
+ available, readcount, wanted = self._decoder.decode(b)
+
+ if readcount < len(b):
+ self._leftover = b[readcount:]
+
+ return available, readcount - oldlen, wanted

I think wanted should be wanted - len(leftover) since leftover is
internally buffered by the decoder. I have no idea about the readcount.

+ def getavailable(self):
+ return self._decoder.getavailable()

Nit: pop might be a better word to denote that this function consumes
the available values.

This revision was automatically updated to reflect the committed changes.