[Haskell-cafe] Re: ANN: A triple of new packages for talking tothe
outside world
Dominic Steinitz
dominic.steinitz at blueyonder.co.uk
Thu Jan 17 14:05:55 EST 2008
Adam Langley <agl <at> imperialviolet.org> writes:
>
> BitGet is just an API RFC at the moment, so I'm just describing it
> here - not trying to justify it.
>
> In BitGet there's getAsWord[8|16|32|64] which take a number of bits ($n$) and
> returns the next $n$ bits in the bottom of a Word$x$. Thus, getAsWord8 is what
> you call getBits and, if you had a 48 bit number, you could use getAsWord64 and
> the bottom 48-bits of the resulting Word64 would be what you want.
>
> Equally, asking for more than $x$ bits when calling getAsWord$x$ is a mistake,
> however I don't check for it in the interest of speed.
>
> There are also get[Left|Right]ByteString which return the next $n$ bits in a
> ByteString of Word8's. The padding is either at the end of the last byte (left
> aligned) or at the beginning of the first byte (right aligned).
>
Ok so I should be doing something like this. I'm not clear what happens if you
are reading from a socket and not all the input has arrived but I'll think
about that over the weekend.
Another thought: could e.g. getRightByteString be in the IO monad and then I
don't have to run the Get(?) monad? Or is that a really stupid question?
Dominic.
import qualified Data.ByteString as B
import Data.Word
import IO
import qualified Data.Binary.Strict.BitGet as BG
test =
do h <- openFile "foobarbaz" ReadMode
b <- B.hGetContents h
let ebms = test2 b
case ebms of
Left s -> return s
Right bms -> return (concat ((map (show . B.unpack) bms)))
test1 =
do bm1 <- BG.getRightByteString 2
bm2 <- BG.getRightByteString 2
return [bm1,bm2]
test2 bs = BG.runBitGet bs test1
More information about the Haskell-Cafe
mailing list