feat: Extracting word types
This commit is contained in:
parent
a577bddd7f
commit
5723a92308
4 changed files with 58 additions and 3 deletions
|
@ -1,8 +1,9 @@
|
|||
{-# LANGUAGE DeriveFunctor #-}
|
||||
{-# LANGUAGE InstanceSigs #-}
|
||||
module Language.Java.Classfile.Extract (Extract()) where
|
||||
module Language.Java.Classfile.Extract (Extract(), bytes) where
|
||||
|
||||
import Data.ByteString.Lazy (ByteString)
|
||||
import qualified Data.ByteString.Lazy as ByteString
|
||||
|
||||
data Extract a = Extract (Continuation a)
|
||||
|
||||
|
@ -28,6 +29,16 @@ instance Applicative Extract where
|
|||
Fail -> Fail
|
||||
|
||||
|
||||
-- | Get a specified count of bytes. Fail if there are not enough bytes available.
|
||||
|
||||
bytes :: Word -> Extract ByteString
|
||||
bytes count = Extract $ \ input -> let
|
||||
count' = fromIntegral count
|
||||
(bs, rest) = ByteString.splitAt count' input
|
||||
in if ByteString.length bs /= count'
|
||||
then Fail
|
||||
else Done rest bs
|
||||
|
||||
|
||||
{- It seems I cannot define a lawful monad instance
|
||||
instance Monad Extract where
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue