packages feed

bytable 0.0.0.10 → 0.1.0.0

raw patch · 3 files changed

+86/−103 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- Codec.Bytable: BytableM :: (ByteString -> Either String (a, ByteString)) -> BytableM a
- Codec.Bytable: addLen :: (Bytable n, Num n) => n -> ByteString -> ByteString
- Codec.Bytable: class Bytable b
- Codec.Bytable: class Parsable p
- Codec.Bytable: data BytableM a
- Codec.Bytable: decode :: Bytable b => ByteString -> Either String b
- Codec.Bytable: encode :: Bytable b => b -> ByteString
- Codec.Bytable: evalBytableM :: BytableM a -> ByteString -> Either String a
- Codec.Bytable: execBytableM :: BytableM a -> ByteString -> Either String ByteString
- Codec.Bytable: fromByteString :: Bytable b => ByteString -> Either String b
- Codec.Bytable: head :: BytableM Word8
- Codec.Bytable: instance Applicative BytableM
- Codec.Bytable: instance Bytable ByteString
- Codec.Bytable: instance Bytable Word8
- Codec.Bytable: instance Functor BytableM
- Codec.Bytable: instance Monad BytableM
- Codec.Bytable: list :: Int -> BytableM b -> BytableM [b]
- Codec.Bytable: null :: BytableM Bool
- Codec.Bytable: parse :: Parsable p => BytableM p
- Codec.Bytable: runBytableM :: BytableM a -> ByteString -> Either String (a, ByteString)
- Codec.Bytable: take :: Bytable b => Int -> BytableM b
- Codec.Bytable: toByteString :: Bytable b => b -> ByteString
+ Codec.Bytable.BigEndian: BytableM :: (ByteString -> Either String (a, ByteString)) -> BytableM a
+ Codec.Bytable.BigEndian: addLen :: (Bytable n, Num n) => n -> ByteString -> ByteString
+ Codec.Bytable.BigEndian: class Bytable b
+ Codec.Bytable.BigEndian: class Parsable p
+ Codec.Bytable.BigEndian: data BytableM a
+ Codec.Bytable.BigEndian: decode :: Bytable b => ByteString -> Either String b
+ Codec.Bytable.BigEndian: encode :: Bytable b => b -> ByteString
+ Codec.Bytable.BigEndian: evalBytableM :: BytableM a -> ByteString -> Either String a
+ Codec.Bytable.BigEndian: execBytableM :: BytableM a -> ByteString -> Either String ByteString
+ Codec.Bytable.BigEndian: head :: BytableM Word8
+ Codec.Bytable.BigEndian: instance Applicative BytableM
+ Codec.Bytable.BigEndian: instance Bytable ByteString
+ Codec.Bytable.BigEndian: instance Bytable Word8
+ Codec.Bytable.BigEndian: instance Functor BytableM
+ Codec.Bytable.BigEndian: instance Monad BytableM
+ Codec.Bytable.BigEndian: list :: Int -> BytableM b -> BytableM [b]
+ Codec.Bytable.BigEndian: null :: BytableM Bool
+ Codec.Bytable.BigEndian: parse :: Parsable p => BytableM p
+ Codec.Bytable.BigEndian: runBytableM :: BytableM a -> ByteString -> Either String (a, ByteString)
+ Codec.Bytable.BigEndian: take :: Bytable b => Int -> BytableM b

Files

bytable.cabal view
@@ -2,7 +2,7 @@ build-type:	Simple  name:		bytable-version:	0.0.0.10+version:	0.1.0.0 stability:	Experimental author:		Yoshikuni Jujo <PAF01143@nifty.ne.jp> maintainer:	Yoshikuni Jujo <PAF01143@nifty.ne.jp>@@ -28,10 +28,10 @@ source-repository	this     type:	git     location:	git://github.com/YoshikuniJujo/forest-    tag:	bytable-0.0.0.10+    tag:	bytable-0.1.0.0  library     hs-source-dirs:	src-    exposed-modules:	Codec.Bytable, Codec.Bytable.BigEndian+    exposed-modules:	Codec.Bytable.BigEndian     build-depends:	base == 4.*, bytestring == 0.10.*, word24 == 1.0.*     ghc-options:	-Wall
− src/Codec/Bytable.hs
@@ -1,94 +0,0 @@-{-# LANGUAGE PatternGuards, OverloadedStrings, TupleSections #-}--module Codec.Bytable (-	Bytable(..), fromByteString, toByteString,-	Parsable(..),-	BytableM(..), evalBytableM, execBytableM,-	head, take, null, list, addLen,-) where--import Prelude hiding (take, head, null)-import Control.Applicative(Applicative(..), (<$>))-import Control.Monad (unless, liftM, ap)-import Data.Word (Word8)-import qualified Data.ByteString as BS--data BytableM a = BytableM {-	runBytableM :: BS.ByteString -> Either String (a, BS.ByteString) }--evalBytableM :: BytableM a -> BS.ByteString -> Either String a-evalBytableM m bs = fst <$> runBytableM m bs--execBytableM :: BytableM a -> BS.ByteString -> Either String BS.ByteString-execBytableM m bs = snd <$> runBytableM m bs--instance Monad BytableM where-	return x = BytableM $ \bs -> Right (x, bs)-	BytableM m1 >>= f = BytableM $ \bs -> do-		(x, bs') <- m1 bs-		runBytableM (f x) bs'-	fail = BytableM . const . Left--instance Functor BytableM where-	fmap = liftM--instance Applicative BytableM where-	pure = return-	(<*>) = ap--class Bytable b where-	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-	decode "" = Right 0-	decode bs-		| [w] <- BS.unpack bs = Right w-	decode _ = Left "Codec.Bytable.BigEndian: Bytable Word8: too large"-	encode = BS.pack . (: [])--instance Bytable BS.ByteString where-	decode = Right-	encode = id--class Parsable p where-	parse :: BytableM p--head :: BytableM Word8-head = BytableM $ \bs -> case BS.uncons bs of-	Just (h, t) -> Right (h, t)-	_ -> Left "Bytable.head: null"--take :: Bytable b => Int -> BytableM b-take n = BytableM $ \bs -> do-	unless (BS.length bs >= n) .  Left $-		"Bytable.take: length shorter than " ++ show n-	let (x, bs') = BS.splitAt n bs-	(, bs') <$> decode x--null :: BytableM Bool-null  = BytableM $ \bs -> Right (BS.null bs, bs)--list :: Int -> BytableM b -> BytableM [b]-list n m = do-	bs <- take n-	case evalBytableM lst bs of-		Right xs -> return xs-		Left msg -> fail msg-	where-	lst = do-		e <- null-		if e then return [] else (:) <$> m <*> lst--addLen :: (Bytable n, Num n) => n -> BS.ByteString -> BS.ByteString-addLen t bs =-	encode (fromIntegral (BS.length bs) `asTypeOf` t) `BS.append` bs
src/Codec/Bytable/BigEndian.hs view
@@ -1,12 +1,89 @@-{-# OPTIONS_GHC -fno-warn-orphans #-}+{-# LANGUAGE PatternGuards, OverloadedStrings, TupleSections #-} -module Codec.Bytable.BigEndian () where+module Codec.Bytable.BigEndian (+	Bytable(..), Parsable(..),+	BytableM(..), evalBytableM, execBytableM,+	head, take, null, list, addLen,+) where -import Data.Bits-import Data.Word-import Data.Word.Word24-import Codec.Bytable+import Prelude hiding (take, head, null)+import Control.Applicative(Applicative(..), (<$>))+import Control.Monad (unless, liftM, ap)+import Data.Bits (Bits, shiftL, shiftR, (.|.))+import Data.Word (Word8, Word16, Word32, Word64)+import Data.Word.Word24 (Word24) import qualified Data.ByteString as BS++data BytableM a = BytableM {+	runBytableM :: BS.ByteString -> Either String (a, BS.ByteString) }++evalBytableM :: BytableM a -> BS.ByteString -> Either String a+evalBytableM m bs = fst <$> runBytableM m bs++execBytableM :: BytableM a -> BS.ByteString -> Either String BS.ByteString+execBytableM m bs = snd <$> runBytableM m bs++instance Monad BytableM where+	return x = BytableM $ \bs -> Right (x, bs)+	BytableM m1 >>= f = BytableM $ \bs -> do+		(x, bs') <- m1 bs+		runBytableM (f x) bs'+	fail = BytableM . const . Left++instance Functor BytableM where+	fmap = liftM++instance Applicative BytableM where+	pure = return+	(<*>) = ap++class Bytable b where+	decode :: BS.ByteString -> Either String b+	encode :: b -> BS.ByteString++instance Bytable Word8 where+	decode "" = Right 0+	decode bs+		| [w] <- BS.unpack bs = Right w+	decode _ = Left "Codec.Bytable.BigEndian: Bytable Word8: too large"+	encode = BS.pack . (: [])++instance Bytable BS.ByteString where+	decode = Right+	encode = id++class Parsable p where+	parse :: BytableM p++head :: BytableM Word8+head = BytableM $ \bs -> case BS.uncons bs of+	Just (h, t) -> Right (h, t)+	_ -> Left "Bytable.head: null"++take :: Bytable b => Int -> BytableM b+take n = BytableM $ \bs -> do+	unless (BS.length bs >= n) .  Left $+		"Bytable.take: length shorter than " ++ show n+	let (x, bs') = BS.splitAt n bs+	(, bs') <$> decode x++null :: BytableM Bool+null  = BytableM $ \bs -> Right (BS.null bs, bs)++list :: Int -> BytableM b -> BytableM [b]+list n m = do+	bs <- take n+	case evalBytableM lst bs of+		Right xs -> return xs+		Left msg -> fail msg+	where+	lst = do+		e <- null+		if e then return [] else (:) <$> m <*> lst++addLen :: (Bytable n, Num n) => n -> BS.ByteString -> BS.ByteString+addLen t bs =+	encode (fromIntegral (BS.length bs) `asTypeOf` t) `BS.append` bs  instance Bytable Int where 	decode bs