bytable 0.0.0.9 → 0.0.0.10
raw patch · 3 files changed
+34/−25 lines, 3 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ Codec.Bytable: decode :: Bytable b => ByteString -> Either String b
+ Codec.Bytable: encode :: Bytable b => b -> ByteString
Files
- bytable.cabal +2/−2
- src/Codec/Bytable.hs +20/−11
- src/Codec/Bytable/BigEndian.hs +12/−12
bytable.cabal view
@@ -2,7 +2,7 @@ build-type: Simple name: bytable-version: 0.0.0.9+version: 0.0.0.10 stability: Experimental author: Yoshikuni Jujo <PAF01143@nifty.ne.jp> maintainer: Yoshikuni Jujo <PAF01143@nifty.ne.jp>@@ -28,7 +28,7 @@ source-repository this type: git location: git://github.com/YoshikuniJujo/forest- tag: bytable-0.0.0.9+ tag: bytable-0.0.0.10 library hs-source-dirs: src
src/Codec/Bytable.hs view
@@ -1,7 +1,7 @@ {-# LANGUAGE PatternGuards, OverloadedStrings, TupleSections #-} module Codec.Bytable (- Bytable(..),+ Bytable(..), fromByteString, toByteString, Parsable(..), BytableM(..), evalBytableM, execBytableM, head, take, null, list, addLen,@@ -37,19 +37,28 @@ (<*>) = ap class Bytable b where- fromByteString :: BS.ByteString -> Either String b- toByteString :: b -> BS.ByteString+ decode :: BS.ByteString -> Either String b+ encode :: b -> BS.ByteString +fromByteString :: Bytable b => BS.ByteString -> Either String b+fromByteString = decode++toByteString :: Bytable b => b -> BS.ByteString+toByteString = encode++{-# DEPRECATED fromByteString "Use decode instead" #-}+{-# DEPRECATED toByteString "Use encode instead" #-}+ instance Bytable Word8 where- fromByteString "" = Right 0- fromByteString bs+ decode "" = Right 0+ decode bs | [w] <- BS.unpack bs = Right w- fromByteString _ = Left "Codec.Bytable.BigEndian: Bytable Word8: too large"- toByteString = BS.pack . (: [])+ decode _ = Left "Codec.Bytable.BigEndian: Bytable Word8: too large"+ encode = BS.pack . (: []) instance Bytable BS.ByteString where- fromByteString = Right- toByteString = id+ decode = Right+ encode = id class Parsable p where parse :: BytableM p@@ -64,7 +73,7 @@ unless (BS.length bs >= n) . Left $ "Bytable.take: length shorter than " ++ show n let (x, bs') = BS.splitAt n bs- (, bs') <$> fromByteString x+ (, bs') <$> decode x null :: BytableM Bool null = BytableM $ \bs -> Right (BS.null bs, bs)@@ -82,4 +91,4 @@ addLen :: (Bytable n, Num n) => n -> BS.ByteString -> BS.ByteString addLen t bs =- toByteString (fromIntegral (BS.length bs) `asTypeOf` t) `BS.append` bs+ encode (fromIntegral (BS.length bs) `asTypeOf` t) `BS.append` bs
src/Codec/Bytable/BigEndian.hs view
@@ -9,43 +9,43 @@ import qualified Data.ByteString as BS instance Bytable Int where- fromByteString bs+ decode bs | BS.length bs <= 4 = Right $ byteStringToNum bs | otherwise = Left "Codec.Bytable.BigEndian: Bytable Int: too large"- toByteString = integralToByteStringN 4+ encode = integralToByteStringN 4 instance Bytable Integer where- fromByteString bs = Right $ byteStringToNum bs- toByteString = integralToByteString+ decode bs = Right $ byteStringToNum bs+ encode = integralToByteString instance Bytable Word16 where- fromByteString bs+ decode bs | BS.length bs <= 2 = Right $ byteStringToNum bs | otherwise = Left "Codec.Bytable.BigEndian: Bytable Word16: too large"- toByteString = integralToByteStringN 2+ encode = integralToByteStringN 2 instance Bytable Word24 where- fromByteString bs+ decode bs | BS.length bs <= 3 = Right $ byteStringToNum bs | otherwise = Left "Codec.Bytable.BigEndian: Bytable Word24: too large"- toByteString = integralToByteStringN 3+ encode = integralToByteStringN 3 instance Bytable Word32 where- fromByteString bs+ decode bs | BS.length bs <= 4 = Right $ byteStringToNum bs | otherwise = Left "Codec.Bytable.BigEndian: Bytable Word32: too large"- toByteString = integralToByteStringN 4+ encode = integralToByteStringN 4 instance Bytable Word64 where- fromByteString bs+ decode bs | BS.length bs <= 8 = Right $ byteStringToNum bs | otherwise = Left "Codec.Bytable.BigEndian: Bytable Word32: too large"- toByteString = integralToByteStringN 8+ encode = integralToByteStringN 8 byteStringToNum :: (Num n, Bits n) => BS.ByteString -> n byteStringToNum = wordsToNum . reverse . BS.unpack