pack package:streaming-bytestring

O(n) Convert a monadic stream of individual Word8s into a packed byte stream.
O(n) Convert a stream of separate characters into a packed byte stream.
Packing and unpacking from lists packBytes' :: Monad m => [Word8] -> ByteString m () packBytes' cs0 = packChunks 32 cs0 where packChunks n cs = case B.packUptoLenBytes n cs of (bs, []) -> Chunk bs (Empty ()) (bs, cs') -> Chunk bs (packChunks (min (n * 2) BI.smallChunkSize) cs') -- packUptoLenBytes :: Int -> [Word8] -> (ByteString, [Word8]) packUptoLenBytes len xs0 = accursedUnutterablePerformIO (createUptoN' len $ p -> go p len xs0) where go !_ !n [] = return (len-n, []) go !_ !0 xs = return (len, xs) go !p !n (x:xs) = poke p x >> go (p plusPtr 1) (n-1) xs createUptoN' :: Int -> (Ptr Word8 -> IO (Int, a)) -> IO (B.ByteString, a) createUptoN' l f = do fp <- B.mallocByteString l (l', res) withForeignPtr fp $ p - f p assert (l' <= l) $ return (B.PS fp 0 l', res) {-# INLINABLE packBytes' #-} Convert a Stream of pure Word8 into a chunked ByteStream.
Convert a vanilla Stream of characters into a stream of bytes. Note: Each Char value is truncated to 8 bits.
O(n) Converts a packed byte stream into a stream of individual bytes.
Given a stream of bytes, produce a vanilla Stream of characters.
The reverse of packChars. Given a stream of bytes, produce a Stream individual bytes.