memory-0.7: memory and related abtraction stuff

LicenseBSD-Style
MaintainerVincent Hanquez <vincent@snarc.org>
Stabilityexperimental
Portabilityunknown
Safe HaskellNone
LanguageHaskell2010

Data.ByteArray.Pack

Contents

Description

Simple Byte Array packer

Simple example:

> flip pack 20 $ putWord8 0x41 >> putByteString "BCD" >> putWord8 0x20 >> putStorable (42 :: Word32)
Right (ABCD *\NUL\NUL\NUL")

Original code from https://hackage.haskell.org/package/bspack generalized and adapted to run on memory, and spellchecked / tweaked. (2015-05) Copyright (c) 2014 Nicolas DI PRIMA

Synopsis

Documentation

data Packer a

Simple ByteArray Packer

data Result a

Packing result:

  • PackerMore: the next state of Packing with an arbitrary value
  • PackerFail: an error happened

Instances

Show a => Show (Result a) 

fill :: ByteArray byteArray => Int -> Packer a -> Either String byteArray

fill a given sized buffer with the result of the Packer action

pack :: ByteArray byteArray => Packer a -> Int -> Either String byteArray

Deprecated: use fill instead

pack the given packer into the given bytestring

Operations

put

putWord8 :: Word8 -> Packer ()

put Word8 in the current position in the stream

putWord16 :: Word16 -> Packer ()

put Word16 in the current position in the stream /! use Host Endianness

putWord32 :: Word32 -> Packer ()

put Word32 in the current position in the stream /! use Host Endianness

putStorable :: Storable storable => storable -> Packer ()

put a storable from the current position in the stream

putBytes :: ByteArrayAccess ba => ba -> Packer ()

put a Byte Array from the current position in the stream

If the ByteArray is null, then do nothing

fillList :: Storable storable => [storable] -> Packer ()

Will put the given storable list from the current position in the stream to the end.

This function will fail with not enough storage if the given storable can't be written (not enough space)

example: > pack (fillList $ [1..] :: Word8) 9 ==> "123456789" > pack (fillList $ [1..] :: Word32) 4 ==> "1000" > pack (fillList $ [1..] :: Word32) 64 -- will work > pack (fillList $ [1..] :: Word32) 1 -- will fail (not enough space) > pack (fillList $ [1..] :: Word32) 131 -- will fail (not enough space)

fillUpWith :: Storable storable => storable -> Packer ()

fill up from the current position in the stream to the end

it is basically: > fillUpWith s == fillList (repeat s)

skip

skip :: Int -> Packer ()

skip some bytes from the current position in the stream

skipStorable :: Storable storable => storable -> Packer ()

skip the size of a storable from the current position in the stream