Commits

Aleksey Khudyakov committed 11818db

* Add functions to encode unsigned values
* Add documentation to functions
* Remove junk

  • Participants
  • Parent commits 5727846

Comments (0)

Files changed (1)

File Data/Binary/Combinators.hs

-{-# LANGUAGE MagicHash #-}
 module Data.Binary.Combinators (
-                               -- * Integers
-                                 putAsInt8, putAsInt16, putAsInt32, putAsInt64
-                               , getAsInt8, getAsInt16, getAsInt32, getAsInt64
+                                 -- * Integers
+                                 -- ** Signed integers
+                                 putAsInt8
+                               , putAsInt16
+                               , putAsInt32
+                               , putAsInt64
+                               , getAsInt8
+                               , getAsInt16
+                               , getAsInt32
+                               , getAsInt64
+                                 -- ** Unsigned integers
+                               , putAsWord8
+                               , putAsWord16
+                               , putAsWord32
+                               , putAsWord64
+                               , getAsWord8
+                               , getAsWord16
+                               , getAsWord32
+                               , getAsWord64
                                -- * Floating point numbers
-                               , putAsFloat, putAsDouble, getAsFloat, getAsDouble
+                               , putAsFloat
+                               , putAsDouble
+                               , getAsFloat
+                               , getAsDouble
                                -- * Lists
-                               , putShortList , getShortList
+                               , putShortList 
+                               , getShortList
                                -- * Tuples
-                               , putTuple2, getTuple2
+                               , putTuple2
+                               , getTuple2
                                ) where
 ----------------------------------------------------------------
 import Control.Monad
 import Data.Binary
 import Data.Binary.Get
 import Data.Binary.Put
-import qualified Data.ByteString as B
-import qualified Data.ByteString.Internal as B
 import Data.Int
-import Data.Word
-import Foreign
 
+
+----------------------------------------------------------------
+
+-- Functions which add signs to integers. (Conversion from words)
 signify8 :: Word8 -> Int8
 signify8 = fromIntegral
 
 signify64 = fromIntegral
 
 ----------------------------------------------------------------
+-- Signed integers
+----------------------------------------------------------------
 
+-- | Put integral value as 8-bit integer ([-128..127] range)
 putAsInt8  :: Integral a => a -> Put
 putAsInt8  = putWord8    . fromIntegral
+-- | Put integral value as 16-bit integer [-32768..32767]
 putAsInt16 :: Integral a => a -> Put
 putAsInt16 = putWord16le . fromIntegral
+-- | Put integral value as 32 bit integer
 putAsInt32 :: Integral a => a -> Put
 putAsInt32 = putWord32le . fromIntegral
+-- | Put integral value as 64 bit integer
 putAsInt64 :: Integral a => a -> Put
 putAsInt64 = putWord64le . fromIntegral
 
+-- | Retrieve value encoded as 8-bit integer
 getAsInt8  :: Num a => Get a
 getAsInt8  = fromIntegral . signify8  <$> getWord8
+-- | Retrieve value encoded as 16-bit integer
 getAsInt16 :: Num a => Get a
 getAsInt16 = fromIntegral . signify16 <$> getWord16le
+-- | Retrieve value encoded as 32-bit integer
 getAsInt32 :: Num a => Get a
 getAsInt32 = fromIntegral . signify32 <$> getWord32le
+-- | Retrieve value encoded as 64-bit integer
 getAsInt64 :: Num a => Get a
 getAsInt64 = fromIntegral . signify64 <$> getWord64le
 
+
+----------------------------------------------------------------
+-- Unsigned integers
+----------------------------------------------------------------
+
+-- | Put integral value as 8-bit unsigned integer ([-128..127] range)
+putAsWord8  :: Integral a => a -> Put
+putAsWord8  = putWord8    . fromIntegral
+-- | Put integral value as 16-bit unsigned integer [-32768..32767]
+putAsWord16 :: Integral a => a -> Put
+putAsWord16 = putWord16le . fromIntegral
+-- | Put integral value as 32 bit unsigned integer
+putAsWord32 :: Integral a => a -> Put
+putAsWord32 = putWord32le . fromIntegral
+-- | Put integral value as 64 bit unsigned integer
+putAsWord64 :: Integral a => a -> Put
+putAsWord64 = putWord64le . fromIntegral
+
+-- | Retrieve value encoded as 8-bit unsigned integer
+getAsWord8  :: Num a => Get a
+getAsWord8  = fromIntegral  <$> getWord8
+-- | Retrieve value encoded as 16-bit unsigned integer
+getAsWord16 :: Num a => Get a
+getAsWord16 = fromIntegral <$> getWord16le
+-- | Retrieve value encoded as 32-bit unsigned integer
+getAsWord32 :: Num a => Get a
+getAsWord32 = fromIntegral <$> getWord32le
+-- | Retrieve value encoded as 64-bit unsigned integer
+getAsWord64 :: Num a => Get a
+getAsWord64 = fromIntegral <$> getWord64le
+
+
+----------------------------------------------------------------
+-- Floating point values
+----------------------------------------------------------------
+
 -- | Put number as double precition (64bit) IEEE floating point number
 putAsFloat  :: Real a => a -> Put
 putAsFloat  = putFloatIEEEhost . realToFrac