@@ -10,51 +10,62 @@ import Control.Monad
10
10
import Control.Applicative
11
11
12
12
-- Change to Ptr-based parser later
13
- data Decode a = Decode
13
+ newtype Decode a = Decode
14
14
{ runDecode :: B. ByteString -> Either String (B. ByteString , a )}
15
15
16
16
instance Functor Decode where
17
17
fmap f p = Decode $ fmap (fmap f) . runDecode p
18
+ {-# INLINE fmap #-}
18
19
19
20
instance Applicative Decode where
20
21
pure x = Decode $ \ bs -> Right (bs, x)
22
+ {-# INLINE pure #-}
21
23
22
24
p1 <*> p2 = Decode $ \ bs -> do
23
25
(bs2, f) <- runDecode p1 bs
24
26
(bs3, x) <- runDecode p2 bs2
25
27
pure (bs3, f x)
28
+ {-# INLINE (<*>) #-}
26
29
27
30
instance Monad Decode where
28
31
return = pure
32
+ {-# INLINE return #-}
29
33
30
34
p >>= f = Decode $ \ bs -> do
31
35
(bs2, x) <- runDecode p bs
32
36
runDecode (f x) bs2
37
+ {-# INLINE (>>=) #-}
33
38
34
39
fail = Decode . const . Left
40
+ {-# INLINE fail #-}
35
41
36
42
checkLen :: B. ByteString -> Int -> Either String ()
37
43
checkLen bs len | len > B. length bs = Left " too many bytes to read"
38
44
| otherwise = Right ()
45
+ {-# INLINE checkLen #-}
39
46
40
47
41
48
takeWhile :: (Word8 -> Bool ) -> Decode B. ByteString
42
49
takeWhile f = Decode $ \ bs -> Right . swap $ B. span f bs
50
+ {-# INLINE takeWhile #-}
43
51
44
52
getByte :: Decode Word8
45
53
getByte = Decode $ \ bs -> do
46
54
checkLen bs 1
47
55
Right (B. drop 1 bs, B. index bs 0 )
56
+ {-# INLINE getByte #-}
48
57
49
58
getTwoBytes :: Decode (Word8 , Word8 )
50
59
getTwoBytes = Decode $ \ bs -> do
51
60
checkLen bs 2
52
61
Right (B. drop 2 bs, (B. index bs 0 , B. index bs 1 ))
62
+ {-# INLINE getTwoBytes #-}
53
63
54
64
getFourBytes :: Decode (Word8 , Word8 , Word8 , Word8 )
55
65
getFourBytes = Decode $ \ bs -> do
56
66
checkLen bs 4
57
67
Right (B. drop 4 bs, (B. index bs 0 , B. index bs 1 , B. index bs 2 , B. index bs 3 ))
68
+ {-# INLINE getFourBytes #-}
58
69
59
70
-----------
60
71
-- Public
@@ -63,18 +74,22 @@ getByteString :: Int -> Decode B.ByteString
63
74
getByteString len = Decode $ \ bs -> do
64
75
checkLen bs len
65
76
Right . swap $ B. splitAt len bs
77
+ {-# INLINE getByteString #-}
66
78
67
79
getByteStringNull :: Decode B. ByteString
68
80
getByteStringNull = takeWhile (/= 0 ) <* getWord8
81
+ {-# INLINE getByteStringNull #-}
69
82
70
83
getWord8 :: Decode Word8
71
84
getWord8 = getByte
85
+ {-# INLINE getWord8 #-}
72
86
73
87
getWord16BE :: Decode Word16
74
88
getWord16BE =
8000
do
75
89
(w1, w2) <- getTwoBytes
76
90
pure $ fromIntegral w1 * 256 +
77
91
fromIntegral w2
92
+ {-# INLINE getWord16BE #-}
78
93
79
94
getWord32BE :: Decode Word32
80
95
getWord32BE = do
@@ -83,10 +98,13 @@ getWord32BE = do
83
98
fromIntegral w2 * 256 * 256 +
84
99
fromIntegral w3 * 256 +
85
100
fromIntegral w4
101
+ {-# INLINE getWord32BE #-}
86
102
87
103
getInt16BE :: Decode Int16
88
104
getInt16BE = fromIntegral <$> getWord16BE
105
+ {-# INLINE getInt16BE #-}
89
106
90
107
getInt32BE :: Decode Int32
91
108
getInt32BE = fromIntegral <$> getWord32BE
109
+ {-# INLINE getInt32BE #-}
92
110
0 commit comments