deserialization - Haskell: find out how many bytes a Get expression would consume -
i writing tool includes deserialization mechanism bachelor thesis, use get
monad (data.binary.get
). ran following problem:
during deserialization, there part have getter of type get a
, need read bytestring
of length n
, n
amount of bytes consumed if ran getter
@ position. in other words, need know how bytes getter
consume without consuming them.
there way this:
readbytes :: -> bytestring readbytes getter = safe <- lookahead getremaininglazybytestring let info = rungetorfail getter safe -- n_cb = number of consumed bytes case info of right (_, n_cb, _) -> getlazybytestring n_cb
but hideous beyond description. every time method called, copies entire remainder of file.
even though doesn't seem hard problem in theory, , far monad has been capable of doing needed, cannot find better solution.
i need know how bytes getter consume without consuming them.
perhaps perform 2 calls bytesread :: int64
function, second call inside lookahead
, after having parsed a
value. like
bytesread1 <- bytesread bytesread2 <- lookahead (getter *> bytesread) return (bytesread2 - bytesread1)
i'm not sure how bytesread
behaves inside lookahead
, however.
Comments
Post a Comment