D4434: cborutil: add a buffering decoder

yuja (Yuya Nishihara) phabricator at mercurial-scm.org
Mon Sep 3 09:38:04 EDT 2018


yuja added a comment.


  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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D4434

To: indygreg, #hg-reviewers
Cc: yuja, mercurial-devel


More information about the Mercurial-devel mailing list