binary-file 0.15.11 → 0.15.13
raw patch · 5 files changed
+41/−10 lines, 5 files
Files
- binary-file.cabal +2/−2
- src/File/Binary/Classes.hs +19/−2
- src/File/Binary/Instances.hs +18/−4
- src/File/Binary/Instances/BigEndian.hs +1/−1
- src/File/Binary/Instances/LittleEndian.hs +1/−1
binary-file.cabal view
@@ -2,7 +2,7 @@ cabal-version: >= 1.8 name: binary-file-version: 0.15.11+version: 0.15.13 stability: experimental author: Yoshikuni Jujo <PAF01143@nifty.ne.jp> maintainer: Yoshikuni Jujo <PAF01143@nifty.ne.jp>@@ -90,7 +90,7 @@ source-repository this type: git location: git://github.com/YoshikuniJujo/binary-file.git- tag: 0.15.11+ tag: 0.15.13 library hs-source-dirs: src
src/File/Binary/Classes.hs view
@@ -1,10 +1,12 @@-{-# LANGUAGE TypeFamilies, TupleSections #-}+{-# LANGUAGE TypeFamilies, TupleSections, OverloadedStrings #-} module File.Binary.Classes (Field(..), Binary(..), pop, push) where -import Data.ByteString.Lazy (ByteString, unpack, singleton)+import Data.ByteString.Lazy (ByteString, unpack, singleton, cons)+import qualified Data.ByteString.Lazy.Char8 as BSLC () import Data.Bits ((.&.), (.|.), shiftL, shiftR) import Data.Monoid (Monoid, mappend, mempty)+import Data.Word import Control.Arrow (first, second) import Control.Applicative @@ -44,4 +46,19 @@ class (Eq b, Monoid b) => Binary b where getBytes :: Int -> b -> (ByteString, b)+ spanBytes :: (Word8 -> Bool) -> b -> (ByteString, b)+ unconsByte :: b -> (Word8, b) makeBinary :: ByteString -> b++ getBytes 0 b = ("", b)+ getBytes n b = let+ (h, t) = unconsByte b+ (r, b') = getBytes (n - 1) t in+ (h `cons` r, b')++ spanBytes p b+ | b == makeBinary "" = ("", b)+ | p h = let (ret, rest) = spanBytes p t in (h `cons` ret, rest)+ | otherwise = ("", b)+ where+ (h, t) = unconsByte b
src/File/Binary/Instances.hs view
@@ -1,19 +1,28 @@-{-# LANGUAGE TypeFamilies, TypeSynonymInstances, FlexibleInstances, PackageImports #-}+{-# LANGUAGE+ TypeFamilies,+ TypeSynonymInstances,+ FlexibleInstances,+ PackageImports,+ OverloadedStrings #-} {-# OPTIONS_GHC -fno-warn-orphans #-} module File.Binary.Instances () where -import Prelude hiding (take, drop)+import Prelude hiding (take, drop, span) import File.Binary.Classes (Field(..), Binary(..)) import Data.Word (Word8)-import Data.ByteString.Lazy (ByteString, take, drop, toChunks, fromChunks, pack, unpack)+import Data.ByteString.Lazy+ (ByteString, take, drop, toChunks, fromChunks, pack, unpack, uncons, span) import qualified Data.ByteString.Lazy.Char8 as BSLC (pack, unpack)-import qualified Data.ByteString as BS (ByteString, take, drop, concat)+import qualified Data.ByteString as BS (ByteString, take, drop, concat, uncons, span)+import qualified Data.ByteString.Char8 () import Control.Monad (replicateM) import "monads-tf" Control.Monad.State (StateT(..), gets) import Control.Applicative ((<$>), (<*>)) import Control.Arrow (first, (&&&)) import Data.Monoid (mempty)+import Data.Char+import Data.Maybe -------------------------------------------------------------------------------- @@ -56,12 +65,17 @@ instance Binary String where getBytes n = first BSLC.pack . splitAt n+ unconsByte = fromIntegral . ord . head &&& tail makeBinary = BSLC.unpack instance Binary ByteString where getBytes n = take (fromIntegral n) &&& drop (fromIntegral n)+ spanBytes = span+ unconsByte = fromMaybe (0, "") . uncons makeBinary = id instance Binary BS.ByteString where getBytes n = fromChunks . (: []) . BS.take n &&& BS.drop n+ spanBytes p = first (fromChunks . (: [])) . BS.span p+ unconsByte = fromMaybe (0, "") . BS.uncons makeBinary = BS.concat . toChunks
src/File/Binary/Instances/BigEndian.hs view
@@ -26,7 +26,7 @@ fromBinary n = return . first (wordsToInt . unpack) . getBytes n toBinary n = makeBinary . pack . intToWords n -wordsToInt :: Bits i => [Word8] -> i+wordsToInt :: (Num i, Bits i) => [Word8] -> i wordsToInt = foldl (\i w -> i `shiftL` 8 .|. fromIntegral w) 0 intToWords :: (Bits i, Integral i) => Int -> i -> [Word8]
src/File/Binary/Instances/LittleEndian.hs view
@@ -25,7 +25,7 @@ fromBinary n = return . first (wordsToInt . unpack) . getBytes n toBinary n = makeBinary . pack . intToWords n -wordsToInt :: Bits i => [Word8] -> i+wordsToInt :: (Num i, Bits i) => [Word8] -> i wordsToInt = foldr (\w i -> fromIntegral w .|. i `shiftL` 8) 0 intToWords :: (Bits i, Integral i) => Int -> i -> [Word8]