D4434: cborutil: add a buffering decoder

Yuya Nishihara yuya at tcha.org
Mon Sep 3 13:37:48 UTC 2018


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.


More information about the Mercurial-devel mailing list