ocaml-protoc icon indicating copy to clipboard operation
ocaml-protoc copied to clipboard

Decoding a bytes at a particular offset

Open saroupille opened this issue 2 years ago • 1 comments

Assume that I have a buffer of type Bytes.t for which the data I would like to decode are at some offset o. Is there a way to decode the bytes without doing an extra allocation (via Bytes.sub for example)?

The interface seems to get a decoder only from an entire buffer of type Bytes.t or String.t. Did I miss something?

I have the impression that a similar remark could also be made when encoding values.

Looking at the code source quickly, I have the feeling it would be quite easy to implement since the internal representation of a buffer already has this notion of offset.

saroupille avatar Sep 07 '23 21:09 saroupille

On paper we could have an option to represent the value as a bytes * int * int (just like there's an option to use, say, int instead of int32). Then encoding is still trivial (a Bytes.blit) and decoding offers a slice of the decoded string (so maybe you get a slice of string, not bytes, and it's on the user to use Bytes.unsafe_to_string where appropriate).

I think it makes sense! PR welcome if you have the time.

c-cube avatar Sep 07 '23 21:09 c-cube