cborg-0.2.9.0: Concise Binary Object Representation (CBOR)

Copyright | (c) Duncan Coutts 2015-2017
License | BSD3-style (see LICENSE.txt)
Maintainer | duncan@community.haskell.org
Stability | experimental
Portability | non-portable (GHC extensions)
Safe Haskell | Safe-Inferred
Language | Haskell2010

Codec.CBOR.Magic

Contents

- Word utilities
- `ByteString` utilities
- Half-floats
- Float/Word conversion
- Int and Word explicit conversions
- `Integer` utilities
- Simple mutable counters
- Array support

Description

An internal module for doing magical, low-level, and unholy things in the name of efficiency.

Synopsis

- grabWord8 :: Ptr () -> Word8
- grabWord16 :: Ptr () -> Word16
- grabWord32 :: Ptr () -> Word32
- grabWord64 :: Ptr () -> Word64
- eatTailWord8 :: ByteString -> Word8
- eatTailWord16 :: ByteString -> Word16
- eatTailWord32 :: ByteString -> Word32
- eatTailWord64 :: ByteString -> Word64
- wordToFloat16 :: Word16 -> Float
- floatToWord16 :: Float -> Word16
- wordToFloat32 :: Word32 -> Float
- wordToFloat64 :: Word64 -> Double
- word8ToWord :: Word8 -> Word
- word16ToWord :: Word16 -> Word
- word32ToWord :: Word32 -> Word
- word64ToWord :: Word64 -> Word
- word8ToInt :: Word8 -> Int
- word16ToInt :: Word16 -> Int
- word32ToInt :: Word32 -> Int
- word64ToInt :: Word64 -> Maybe Int
- intToWord :: Int -> Word
- intToInt64 :: Int -> Int64
- intToWord64 :: Int -> Word64
- int64ToWord64 :: Int64 -> Word64
- nintegerFromBytes :: ByteString -> Integer
- uintegerFromBytes :: ByteString -> Integer
- data Counter s
- newCounter :: Int -> ST s (Counter s)
- readCounter :: Counter s -> ST s Int
- writeCounter :: Counter s -> Int -> ST s ()
- incCounter :: Counter s -> ST s ()
- decCounter :: Counter s -> ST s ()
- copyByteStringToByteArray :: ByteString -> ByteArray
- copyByteArrayToByteString :: ByteArray -> Int -> Int -> ByteString

# Word utilities

grabWord8 :: Ptr () -> Word8 Source #

Grab a 8-bit `Word` given a `Ptr` to some address.

grabWord16 :: Ptr () -> Word16 Source #

Grab a 16-bit `Word` given a `Ptr` to some address.

grabWord32 :: Ptr () -> Word32 Source #

Grab a 32-bit `Word` given a `Ptr` to some address.

grabWord64 :: Ptr () -> Word64 Source #

Grab a 64-bit `Word64` given a `Ptr` to some address.

# `ByteString` utilities

eatTailWord8 :: ByteString -> Word8 Source #

Take the tail of a `ByteString` (i.e. drop the first byte) and read the resulting byte(s) as an 8-bit word value. The input `ByteString` MUST be at least 2 bytes long: one byte to drop from the front, and one to read as a `Word` value. This is not checked, and failure to ensure this will result in undefined behavior.

eatTailWord16 :: ByteString -> Word16 Source #

Take the tail of a `ByteString` (i.e. drop the first byte) and read the resulting byte(s) as a 16-bit word value. The input `ByteString` MUST be at least 3 bytes long: one byte to drop from the front, and two to read as a 16-bit `Word` value. This is not checked, and failure to ensure this will result in undefined behavior.

eatTailWord32 :: ByteString -> Word32 Source #

Take the tail of a `ByteString` (i.e. drop the first byte) and read the resulting byte(s) as a 32-bit word value. The input `ByteString` MUST be at least 5 bytes long: one byte to drop from the front, and four to read as a 32-bit `Word` value. This is not checked, and failure to ensure this will result in undefined behavior.

eatTailWord64 :: ByteString -> Word64 Source #

Take the tail of a `ByteString` (i.e. drop the first byte) and read the resulting byte(s) as a 64-bit word value. The input `ByteString` MUST be at least 9 bytes long: one byte to drop from the front, and eight to read as a 64-bit `Word64` value. This is not checked, and failure to ensure this will result in undefined behavior.

# Half-floats

wordToFloat16 :: Word16 -> Float Source #

Convert a `Word16` to a half-sized `Float`.

floatToWord16 :: Float -> Word16 Source #

Convert a half-sized `Float` to a `Word`.

# Float/Word conversion

wordToFloat32 :: Word32 -> Float Source #

Cast a `Word32` to a `Float`.

wordToFloat64 :: Word64 -> Double Source #

Cast a `Word64` to a `Float`.

# Int and Word explicit conversions

word8ToWord :: Word8 -> Word Source #

word16ToWord :: Word16 -> Word Source #

word32ToWord :: Word32 -> Word Source #

word64ToWord :: Word64 -> Word Source #

word8ToInt :: Word8 -> Int Source #

word16ToInt :: Word16 -> Int Source #

word32ToInt :: Word32 -> Int Source #

word64ToInt :: Word64 -> Maybe Int Source #

intToWord :: Int -> Word Source #

intToInt64 :: Int -> Int64 Source #

intToWord64 :: Int -> Word64 Source #

int64ToWord64 :: Int64 -> Word64 Source #

# `Integer` utilities

nintegerFromBytes :: ByteString -> Integer Source #

Create a negative `Integer` out of a raw `ByteString`.

uintegerFromBytes :: ByteString -> Integer Source #

Create an `Integer` out of a raw `ByteString`.

# Simple mutable counters

data Counter s Source #

An efficient, mutable counter. Designed to be used inside `ST` or other primitive monads, hence it carries an abstract rank-2 `s` type parameter.

newCounter :: Int -> ST s (Counter s) Source #

Create a new counter with a starting `Int` value.

readCounter :: Counter s -> ST s Int Source #

Read the current value of a `Counter`.

writeCounter :: Counter s -> Int -> ST s () Source #

Write a new value into the `Counter`.

incCounter :: Counter s -> ST s () Source #

Increment a `Counter` by one.

decCounter :: Counter s -> ST s () Source #

Decrement a `Counter` by one.

# Array support

copyByteStringToByteArray :: ByteString -> ByteArray Source #

Copy a `ByteString` and create a primitive `ByteArray` from it.

copyByteArrayToByteString Source #

Arguments

:: ByteArray | `ByteArray` to copy from.
-> Int | Offset into the `ByteArray` to start with.
-> Int | Length of the data to copy.
-> ByteString |

Copy a `ByteArray` at a certain offset and length into a `ByteString`.