Movatterモバイル変換


[0]ホーム

URL:


{-# LANGUAGE CPP #-}moduleBinaryParser(BinaryParser,run,failure,byte,matchingByte,bytesOfSize,bytesWhile,unitOfSize,unitOfBytes,unitWhile,remainders,fold,endOfInput,sized,-- * ExtrasstorableOfSize,beWord16,leWord16,beWord32,leWord32,beWord64,leWord64,asciiIntegral,)whereimportBinaryParser.Preludehiding(fold)importqualifiedData.ByteStringasByteStringimportqualifiedData.ByteString.InternalasByteStringimportqualifiedData.ByteString.UnsafeasByteString-- |-- A highly-efficient parser specialised for strict 'ByteString's.---- Supports the roll-back and alternative branching-- on the basis of the 'Alternative' interface.---- Does not generate fancy error-messages,-- which contributes to its efficiency.newtypeBinaryParsera=BinaryParser(ByteString->EitherText(a,ByteString))deriving((forall a b. (a -> b) -> BinaryParser a -> BinaryParser b)-> (forall a b. a -> BinaryParser b -> BinaryParser a)-> Functor BinaryParserforall a b. a -> BinaryParser b -> BinaryParser aforall a b. (a -> b) -> BinaryParser a -> BinaryParser bforall (f :: * -> *).(forall a b. (a -> b) -> f a -> f b)-> (forall a b. a -> f b -> f a) -> Functor f$cfmap :: forall a b. (a -> b) -> BinaryParser a -> BinaryParser bfmap :: forall a b. (a -> b) -> BinaryParser a -> BinaryParser b$c<$ :: forall a b. a -> BinaryParser b -> BinaryParser a<$ :: forall a b. a -> BinaryParser b -> BinaryParser aFunctor,Functor BinaryParserFunctor BinaryParser =>(forall a. a -> BinaryParser a)-> (forall a b.    BinaryParser (a -> b) -> BinaryParser a -> BinaryParser b)-> (forall a b c.    (a -> b -> c)    -> BinaryParser a -> BinaryParser b -> BinaryParser c)-> (forall a b. BinaryParser a -> BinaryParser b -> BinaryParser b)-> (forall a b. BinaryParser a -> BinaryParser b -> BinaryParser a)-> Applicative BinaryParserforall a. a -> BinaryParser aforall a b. BinaryParser a -> BinaryParser b -> BinaryParser aforall a b. BinaryParser a -> BinaryParser b -> BinaryParser bforall a b.BinaryParser (a -> b) -> BinaryParser a -> BinaryParser bforall a b c.(a -> b -> c) -> BinaryParser a -> BinaryParser b -> BinaryParser cforall (f :: * -> *).Functor f =>(forall a. a -> f a)-> (forall a b. f (a -> b) -> f a -> f b)-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)-> (forall a b. f a -> f b -> f b)-> (forall a b. f a -> f b -> f a)-> Applicative f$cpure :: forall a. a -> BinaryParser apure :: forall a. a -> BinaryParser a$c<*> :: forall a b.BinaryParser (a -> b) -> BinaryParser a -> BinaryParser b<*> :: forall a b.BinaryParser (a -> b) -> BinaryParser a -> BinaryParser b$cliftA2 :: forall a b c.(a -> b -> c) -> BinaryParser a -> BinaryParser b -> BinaryParser cliftA2 :: forall a b c.(a -> b -> c) -> BinaryParser a -> BinaryParser b -> BinaryParser c$c*> :: forall a b. BinaryParser a -> BinaryParser b -> BinaryParser b*> :: forall a b. BinaryParser a -> BinaryParser b -> BinaryParser b$c<* :: forall a b. BinaryParser a -> BinaryParser b -> BinaryParser a<* :: forall a b. BinaryParser a -> BinaryParser b -> BinaryParser aApplicative,Applicative BinaryParserApplicative BinaryParser =>(forall a. BinaryParser a)-> (forall a. BinaryParser a -> BinaryParser a -> BinaryParser a)-> (forall a. BinaryParser a -> BinaryParser [a])-> (forall a. BinaryParser a -> BinaryParser [a])-> Alternative BinaryParserforall a. BinaryParser aforall a. BinaryParser a -> BinaryParser [a]forall a. BinaryParser a -> BinaryParser a -> BinaryParser aforall (f :: * -> *).Applicative f =>(forall a. f a)-> (forall a. f a -> f a -> f a)-> (forall a. f a -> f [a])-> (forall a. f a -> f [a])-> Alternative f$cempty :: forall a. BinaryParser aempty :: forall a. BinaryParser a$c<|> :: forall a. BinaryParser a -> BinaryParser a -> BinaryParser a<|> :: forall a. BinaryParser a -> BinaryParser a -> BinaryParser a$csome :: forall a. BinaryParser a -> BinaryParser [a]some :: forall a. BinaryParser a -> BinaryParser [a]$cmany :: forall a. BinaryParser a -> BinaryParser [a]many :: forall a. BinaryParser a -> BinaryParser [a]Alternative,Applicative BinaryParserApplicative BinaryParser =>(forall a b. BinaryParser a -> (a -> BinaryParser b) -> BinaryParser b)-> (forall a b. BinaryParser a -> BinaryParser b -> BinaryParser b)-> (forall a. a -> BinaryParser a)-> Monad BinaryParserforall a. a -> BinaryParser aforall a b. BinaryParser a -> BinaryParser b -> BinaryParser bforall a b.BinaryParser a -> (a -> BinaryParser b) -> BinaryParser bforall (m :: * -> *).Applicative m =>(forall a b. m a -> (a -> m b) -> m b)-> (forall a b. m a -> m b -> m b)-> (forall a. a -> m a)-> Monad m$c>>= :: forall a b.BinaryParser a -> (a -> BinaryParser b) -> BinaryParser b>>= :: forall a b.BinaryParser a -> (a -> BinaryParser b) -> BinaryParser b$c>> :: forall a b. BinaryParser a -> BinaryParser b -> BinaryParser b>> :: forall a b. BinaryParser a -> BinaryParser b -> BinaryParser b$creturn :: forall a. a -> BinaryParser areturn :: forall a. a -> BinaryParser aMonad,Monad BinaryParserAlternative BinaryParser(Alternative BinaryParser, Monad BinaryParser) =>(forall a. BinaryParser a)-> (forall a. BinaryParser a -> BinaryParser a -> BinaryParser a)-> MonadPlus BinaryParserforall a. BinaryParser aforall a. BinaryParser a -> BinaryParser a -> BinaryParser aforall (m :: * -> *).(Alternative m, Monad m) =>(forall a. m a) -> (forall a. m a -> m a -> m a) -> MonadPlus m$cmzero :: forall a. BinaryParser amzero :: forall a. BinaryParser a$cmplus :: forall a. BinaryParser a -> BinaryParser a -> BinaryParser amplus :: forall a. BinaryParser a -> BinaryParser a -> BinaryParser aMonadPlus,MonadErrorText)via(StateTByteString(ExceptText))typeroleBinaryParserrepresentationalinstanceMonadFailBinaryParserwherefail :: forall a. String -> BinaryParser afail=Text -> BinaryParser aforall a. Text -> BinaryParser afailure(Text -> BinaryParser a)-> (String -> Text) -> String -> BinaryParser aforall b c a. (b -> c) -> (a -> b) -> a -> cforall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).Category cat =>cat b c -> cat a b -> cat a c.String -> Textforall a. IsString a => String -> afromString-- |-- Apply a parser to bytes.{-# INLINErun#-}run::BinaryParsera->ByteString->EitherTextarun :: forall a. BinaryParser a -> ByteString -> Either Text arun(BinaryParserByteString -> Either Text (a, ByteString)parser)ByteStringinput=((a, ByteString) -> a)-> Either Text (a, ByteString) -> Either Text aforall a b. (a -> b) -> Either Text a -> Either Text bforall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f bfmap(a, ByteString) -> aforall a b. (a, b) -> afst(Either Text (a, ByteString) -> Either Text a)-> Either Text (a, ByteString) -> Either Text aforall a b. (a -> b) -> a -> b$ByteString -> Either Text (a, ByteString)parserByteStringinput-- |-- Fail with a message.{-# INLINEfailure#-}failure::Text->BinaryParserafailure :: forall a. Text -> BinaryParser afailureTexttext=(ByteString -> Either Text (a, ByteString)) -> BinaryParser aforall a.(ByteString -> Either Text (a, ByteString)) -> BinaryParser aBinaryParser(Either Text (a, ByteString)-> ByteString -> Either Text (a, ByteString)forall a b. a -> b -> aconst(Text -> Either Text (a, ByteString)forall a b. a -> Either a bLeftTexttext))-- |-- Consume a single byte.{-# INLINEbyte#-}byte::BinaryParserWord8byte :: BinaryParser Word8byte=(ByteString -> Either Text (Word8, ByteString))-> BinaryParser Word8forall a.(ByteString -> Either Text (a, ByteString)) -> BinaryParser aBinaryParser((ByteString -> Either Text (Word8, ByteString)) -> BinaryParser Word8)-> (ByteString -> Either Text (Word8, ByteString))-> BinaryParser Word8forall a b. (a -> b) -> a -> b$\ByteStringremainders->ifByteString -> BoolByteString.nullByteStringremaindersthenText -> Either Text (Word8, ByteString)forall a b. a -> Either a bLeftText"End of input"else(Word8, ByteString) -> Either Text (Word8, ByteString)forall a b. b -> Either a bRight(ByteString -> Word8ByteString.unsafeHeadByteStringremainders,Int -> ByteString -> ByteStringByteString.unsafeDropInt1ByteStringremainders)-- |-- Consume a single byte, which satisfies the predicate.{-# INLINEmatchingByte#-}matchingByte::(Word8->EitherTexta)->BinaryParseramatchingByte :: forall a. (Word8 -> Either Text a) -> BinaryParser amatchingByteWord8 -> Either Text amatcher=(ByteString -> Either Text (a, ByteString)) -> BinaryParser aforall a.(ByteString -> Either Text (a, ByteString)) -> BinaryParser aBinaryParser((ByteString -> Either Text (a, ByteString)) -> BinaryParser a)-> (ByteString -> Either Text (a, ByteString)) -> BinaryParser aforall a b. (a -> b) -> a -> b$\ByteStringremainders->caseByteString -> Maybe (Word8, ByteString)ByteString.unconsByteStringremaindersofMaybe (Word8, ByteString)Nothing->Text -> Either Text (a, ByteString)forall a b. a -> Either a bLeftText"End of input"Just(Word8head,ByteStringtail)->caseWord8 -> Either Text amatcherWord8headofRightaresult->(a, ByteString) -> Either Text (a, ByteString)forall a b. b -> Either a bRight(aresult,ByteStringtail)LeftTexterror->Text -> Either Text (a, ByteString)forall a b. a -> Either a bLeftTexterror-- |-- Consume an amount of bytes.{-# INLINEbytesOfSize#-}bytesOfSize::Int->BinaryParserByteStringbytesOfSize :: Int -> BinaryParser ByteStringbytesOfSizeIntsize=(ByteString -> Either Text (ByteString, ByteString))-> BinaryParser ByteStringforall a.(ByteString -> Either Text (a, ByteString)) -> BinaryParser aBinaryParser((ByteString -> Either Text (ByteString, ByteString)) -> BinaryParser ByteString)-> (ByteString -> Either Text (ByteString, ByteString))-> BinaryParser ByteStringforall a b. (a -> b) -> a -> b$\ByteStringremainders->ifByteString -> IntByteString.lengthByteStringremaindersInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intsizethen(ByteString, ByteString) -> Either Text (ByteString, ByteString)forall a b. b -> Either a bRight(Int -> ByteString -> ByteStringByteString.unsafeTakeIntsizeByteStringremainders,Int -> ByteString -> ByteStringByteString.unsafeDropIntsizeByteStringremainders)elseText -> Either Text (ByteString, ByteString)forall a b. a -> Either a bLeftText"End of input"-- |-- Consume multiple bytes, which satisfy the predicate.{-# INLINEbytesWhile#-}bytesWhile::(Word8->Bool)->BinaryParserByteStringbytesWhile :: (Word8 -> Bool) -> BinaryParser ByteStringbytesWhileWord8 -> Boolpredicate=(ByteString -> Either Text (ByteString, ByteString))-> BinaryParser ByteStringforall a.(ByteString -> Either Text (a, ByteString)) -> BinaryParser aBinaryParser((ByteString -> Either Text (ByteString, ByteString)) -> BinaryParser ByteString)-> (ByteString -> Either Text (ByteString, ByteString))-> BinaryParser ByteStringforall a b. (a -> b) -> a -> b$\ByteStringremainders->(ByteString, ByteString) -> Either Text (ByteString, ByteString)forall a b. b -> Either a bRight((Word8 -> Bool) -> ByteString -> (ByteString, ByteString)ByteString.spanWord8 -> BoolpredicateByteStringremainders)-- |-- Skip an amount of bytes.{-# INLINEunitOfSize#-}unitOfSize::Int->BinaryParser()unitOfSize :: Int -> BinaryParser ()unitOfSizeIntsize=(ByteString -> Either Text ((), ByteString)) -> BinaryParser ()forall a.(ByteString -> Either Text (a, ByteString)) -> BinaryParser aBinaryParser((ByteString -> Either Text ((), ByteString)) -> BinaryParser ())-> (ByteString -> Either Text ((), ByteString)) -> BinaryParser ()forall a b. (a -> b) -> a -> b$\ByteStringremainders->ifByteString -> IntByteString.lengthByteStringremaindersInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intsizethen((), ByteString) -> Either Text ((), ByteString)forall a b. b -> Either a bRight((),Int -> ByteString -> ByteStringByteString.unsafeDropIntsizeByteStringremainders)elseText -> Either Text ((), ByteString)forall a b. a -> Either a bLeftText"End of input"-- |-- Skip specific bytes, while failing if they don't match.{-# INLINEunitOfBytes#-}unitOfBytes::ByteString->BinaryParser()unitOfBytes :: ByteString -> BinaryParser ()unitOfBytesByteStringbytes=(ByteString -> Either Text ((), ByteString)) -> BinaryParser ()forall a.(ByteString -> Either Text (a, ByteString)) -> BinaryParser aBinaryParser((ByteString -> Either Text ((), ByteString)) -> BinaryParser ())-> (ByteString -> Either Text ((), ByteString)) -> BinaryParser ()forall a b. (a -> b) -> a -> b$\ByteStringremainders->ifByteString -> ByteString -> BoolByteString.isPrefixOfByteStringbytesByteStringremaindersthen((), ByteString) -> Either Text ((), ByteString)forall a b. b -> Either a bRight((),Int -> ByteString -> ByteStringByteString.unsafeDrop(ByteString -> IntByteString.lengthByteStringbytes)ByteStringremainders)elseText -> Either Text ((), ByteString)forall a b. a -> Either a bLeftText"Bytes don't match"-- |-- Skip bytes, which satisfy the predicate.{-# INLINEunitWhile#-}unitWhile::(Word8->Bool)->BinaryParser()unitWhile :: (Word8 -> Bool) -> BinaryParser ()unitWhileWord8 -> Boolpredicate=(ByteString -> Either Text ((), ByteString)) -> BinaryParser ()forall a.(ByteString -> Either Text (a, ByteString)) -> BinaryParser aBinaryParser((ByteString -> Either Text ((), ByteString)) -> BinaryParser ())-> (ByteString -> Either Text ((), ByteString)) -> BinaryParser ()forall a b. (a -> b) -> a -> b$\ByteStringremainders->((), ByteString) -> Either Text ((), ByteString)forall a b. b -> Either a bRight((),(Word8 -> Bool) -> ByteString -> ByteStringByteString.dropWhileWord8 -> BoolpredicateByteStringremainders)-- |-- Consume all the remaining bytes.{-# INLINEremainders#-}remainders::BinaryParserByteStringremainders :: BinaryParser ByteStringremainders=(ByteString -> Either Text (ByteString, ByteString))-> BinaryParser ByteStringforall a.(ByteString -> Either Text (a, ByteString)) -> BinaryParser aBinaryParser((ByteString -> Either Text (ByteString, ByteString)) -> BinaryParser ByteString)-> (ByteString -> Either Text (ByteString, ByteString))-> BinaryParser ByteStringforall a b. (a -> b) -> a -> b$\ByteStringremainders->(ByteString, ByteString) -> Either Text (ByteString, ByteString)forall a b. b -> Either a bRight(ByteStringremainders,ByteStringByteString.empty)-- |-- Fail if the input hasn't ended.{-# INLINEendOfInput#-}endOfInput::BinaryParser()endOfInput :: BinaryParser ()endOfInput=(ByteString -> Either Text ((), ByteString)) -> BinaryParser ()forall a.(ByteString -> Either Text (a, ByteString)) -> BinaryParser aBinaryParser((ByteString -> Either Text ((), ByteString)) -> BinaryParser ())-> (ByteString -> Either Text ((), ByteString)) -> BinaryParser ()forall a b. (a -> b) -> a -> b$\caseByteString""->((), ByteString) -> Either Text ((), ByteString)forall a b. b -> Either a bRight((),ByteStringByteString.empty)ByteString_->Text -> Either Text ((), ByteString)forall a b. a -> Either a bLeftText"Not the end of input"-- |-- Left-fold the bytes, terminating before the byte,-- on which the step function returns Nothing.{-# INLINEfold#-}fold::(a->Word8->Maybea)->a->BinaryParserafold :: forall a. (a -> Word8 -> Maybe a) -> a -> BinaryParser afolda -> Word8 -> Maybe astepainit=(ByteString -> Either Text (a, ByteString)) -> BinaryParser aforall a.(ByteString -> Either Text (a, ByteString)) -> BinaryParser aBinaryParser((ByteString -> Either Text (a, ByteString)) -> BinaryParser a)-> (ByteString -> Either Text (a, ByteString)) -> BinaryParser aforall a b. (a -> b) -> a -> b$(a, ByteString) -> Either Text (a, ByteString)forall a b. b -> Either a bRight((a, ByteString) -> Either Text (a, ByteString))-> (ByteString -> (a, ByteString))-> ByteString-> Either Text (a, ByteString)forall b c a. (b -> c) -> (a -> b) -> a -> cforall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).Category cat =>cat b c -> cat a b -> cat a c.a -> ByteString -> (a, ByteString)loopainitwhereloop :: a -> ByteString -> (a, ByteString)loop!aaccumulatorByteStringremainders=caseByteString -> Maybe (Word8, ByteString)ByteString.unconsByteStringremaindersofMaybe (Word8, ByteString)Nothing->(aaccumulator,ByteStringremainders)Just(Word8head,ByteStringtail)->casea -> Word8 -> Maybe astepaaccumulatorWord8headofJustanewAccumulator->a -> ByteString -> (a, ByteString)loopanewAccumulatorByteStringtailMaybe aNothing->(aaccumulator,ByteStringremainders)-- |-- Run a subparser passing it a chunk of the current input of the specified size.{-# INLINEsized#-}sized::Int->BinaryParsera->BinaryParserasized :: forall a. Int -> BinaryParser a -> BinaryParser asizedIntsize(BinaryParserByteString -> Either Text (a, ByteString)parser)=(ByteString -> Either Text (a, ByteString)) -> BinaryParser aforall a.(ByteString -> Either Text (a, ByteString)) -> BinaryParser aBinaryParser((ByteString -> Either Text (a, ByteString)) -> BinaryParser a)-> (ByteString -> Either Text (a, ByteString)) -> BinaryParser aforall a b. (a -> b) -> a -> b$\ByteStringremainders->ifByteString -> IntByteString.lengthByteStringremaindersInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=IntsizethenByteString -> Either Text (a, ByteString)parser(Int -> ByteString -> ByteStringByteString.unsafeTakeIntsizeByteStringremainders)Either Text (a, ByteString)-> (Either Text (a, ByteString) -> Either Text (a, ByteString))-> Either Text (a, ByteString)forall a b. a -> (a -> b) -> b&((a, ByteString) -> (a, ByteString))-> Either Text (a, ByteString) -> Either Text (a, ByteString)forall a b. (a -> b) -> Either Text a -> Either Text bforall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f bfmap(\(a, ByteString)result->((a, ByteString) -> aforall a b. (a, b) -> afst(a, ByteString)result,Int -> ByteString -> ByteStringByteString.unsafeDropIntsizeByteStringremainders))elseText -> Either Text (a, ByteString)forall a b. a -> Either a bLeftText"End of input"-- |-- Storable value of the given amount of bytes.{-# INLINEstorableOfSize#-}storableOfSize::(Storablea)=>Int->BinaryParserastorableOfSize :: forall a. Storable a => Int -> BinaryParser astorableOfSizeIntsize=(ByteString -> Either Text (a, ByteString)) -> BinaryParser aforall a.(ByteString -> Either Text (a, ByteString)) -> BinaryParser aBinaryParser((ByteString -> Either Text (a, ByteString)) -> BinaryParser a)-> (ByteString -> Either Text (a, ByteString)) -> BinaryParser aforall a b. (a -> b) -> a -> b$\(ByteString.PSForeignPtr Word8payloadFPIntoffsetIntlength)->ifIntlengthInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intsizethenletresult :: aresult=IO a -> aforall a. IO a -> aunsafeDupablePerformIO(IO a -> a) -> IO a -> aforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO a) -> IO aforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bwithForeignPtrForeignPtr Word8payloadFP((Ptr Word8 -> IO a) -> IO a) -> (Ptr Word8 -> IO a) -> IO aforall a b. (a -> b) -> a -> b$\Ptr Word8ptr->Ptr Any -> Int -> IO aforall b. Ptr b -> Int -> IO aforall a b. Storable a => Ptr b -> Int -> IO apeekByteOff(Ptr Word8 -> Ptr Anyforall a b. Ptr a -> Ptr bcastPtrPtr Word8ptr)IntoffsetnewRemainder :: ByteStringnewRemainder=ForeignPtr Word8 -> Int -> Int -> ByteStringByteString.PSForeignPtr Word8payloadFP(IntoffsetInt -> Int -> Intforall a. Num a => a -> a -> a+Intsize)(IntlengthInt -> Int -> Intforall a. Num a => a -> a -> a-Intsize)in(a, ByteString) -> Either Text (a, ByteString)forall a b. b -> Either a bRight(aresult,ByteStringnewRemainder)elseText -> Either Text (a, ByteString)forall a b. a -> Either a bLeftText"End of input"-- | Big-endian word of 2 bytes.{-# INLINEbeWord16#-}beWord16::BinaryParserWord16#ifdef WORDS_BIGENDIANbeWord16=storableOfSize2#elsebeWord16 :: BinaryParser Word16beWord16=Word16 -> Word16byteSwap16(Word16 -> Word16) -> BinaryParser Word16 -> BinaryParser Word16forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b<$>Int -> BinaryParser Word16forall a. Storable a => Int -> BinaryParser astorableOfSizeInt2#endif-- | Little-endian word of 2 bytes.{-# INLINEleWord16#-}leWord16::BinaryParserWord16#ifdef WORDS_BIGENDIANleWord16=byteSwap16<$>storableOfSize2#elseleWord16 :: BinaryParser Word16leWord16=Int -> BinaryParser Word16forall a. Storable a => Int -> BinaryParser astorableOfSizeInt2#endif-- | Big-endian word of 4 bytes.{-# INLINEbeWord32#-}beWord32::BinaryParserWord32#ifdef WORDS_BIGENDIANbeWord32=storableOfSize4#elsebeWord32 :: BinaryParser Word32beWord32=Word32 -> Word32byteSwap32(Word32 -> Word32) -> BinaryParser Word32 -> BinaryParser Word32forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b<$>Int -> BinaryParser Word32forall a. Storable a => Int -> BinaryParser astorableOfSizeInt4#endif-- | Little-endian word of 4 bytes.{-# INLINEleWord32#-}leWord32::BinaryParserWord32#ifdef WORDS_BIGENDIANleWord32=byteSwap32<$>storableOfSize4#elseleWord32 :: BinaryParser Word32leWord32=Int -> BinaryParser Word32forall a. Storable a => Int -> BinaryParser astorableOfSizeInt4#endif-- | Big-endian word of 8 bytes.{-# INLINEbeWord64#-}beWord64::BinaryParserWord64#ifdef WORDS_BIGENDIANbeWord64=storableOfSize8#elsebeWord64 :: BinaryParser Word64beWord64=Word64 -> Word64byteSwap64(Word64 -> Word64) -> BinaryParser Word64 -> BinaryParser Word64forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b<$>Int -> BinaryParser Word64forall a. Storable a => Int -> BinaryParser astorableOfSizeInt8#endif-- | Little-endian word of 8 bytes.{-# INLINEleWord64#-}leWord64::BinaryParserWord64#ifdef WORDS_BIGENDIANleWord64=byteSwap64<$>storableOfSize8#elseleWord64 :: BinaryParser Word64leWord64=Int -> BinaryParser Word64forall a. Storable a => Int -> BinaryParser astorableOfSizeInt8#endif-- |-- Integral number encoded in ASCII.{-# INLINEasciiIntegral#-}asciiIntegral::(Integrala)=>BinaryParseraasciiIntegral :: forall a. Integral a => BinaryParser aasciiIntegral=doafirstDigit<-(Word8 -> Either Text a) -> BinaryParser aforall a. (Word8 -> Either Text a) -> BinaryParser amatchingByteWord8 -> Either Text aforall {a} {b} {a}.(Integral a, Num b, IsString a) =>a -> Either a bbyteDigit(a -> Word8 -> Maybe a) -> a -> BinaryParser aforall a. (a -> Word8 -> Maybe a) -> a -> BinaryParser afolda -> Word8 -> Maybe aforall {a} {a}. (Integral a, Num a) => a -> a -> Maybe astepafirstDigitwherebyteDigit :: a -> Either a bbyteDigitabyte=caseabytea -> a -> aforall a. Num a => a -> a -> a-a48ofasubtracted->ifasubtracteda -> a -> Boolforall a. Ord a => a -> a -> Bool<=a9thenb -> Either a bforall a b. b -> Either a bRight(a -> bforall a b. (Integral a, Num b) => a -> bfromIntegralasubtracted)elsea -> Either a bforall a b. a -> Either a bLefta"Not an ASCII decimal byte"step :: a -> a -> Maybe astepastateabyte=casea -> Either String aforall {a} {b} {a}.(Integral a, Num b, IsString a) =>a -> Either a bbyteDigitabyteofRightadigit->a -> Maybe aforall a. a -> Maybe aJust(astatea -> a -> aforall a. Num a => a -> a -> a*a10a -> a -> aforall a. Num a => a -> a -> a+adigit)Either String a_->Maybe aforall a. Maybe aNothing

[8]ページ先頭

©2009-2025 Movatter.jp