@@ -17,79 +17,70 @@ import qualified Data.ByteString.Internal as B
17
17
newtype Decode a = Decode (Peek a )
18
18
deriving (Functor , Applicative , Monad )
19
19
20
+ {-# INLINE runDecode #-}
20
21
runDecode :: Decode a -> B. ByteString -> (B. ByteString , a )
21
22
runDecode (Decode dec) bs =
22
23
let (offset,v ) = decodeExPortionWith dec bs
23
24
in (B. drop offset bs, v)
24
- {-# INLINE runDecode #-}
25
25
26
- fixed :: Int -> (Ptr Word8 -> IO a ) -> Decode a
27
- fixed len f = Decode $ Peek $ \ ps ptr -> do
26
+ {-# INLINE prim #-}
27
+ prim :: Int -> (Ptr Word8 -> IO a ) -> Decode a
28
+ prim len f = Decode $ Peek $ \ ps ptr -> do
28
29
! v <- f ptr
29
30
let ! newPtr = ptr `plusPtr` len
30
31
return (newPtr, v)
31
32
-- return $ PeekResult newPtr v
32
- {-# INLINE fixed #-}
33
-
34
- getByte :: Decode Word8
35
- getByte = fixed 1 peek
36
- {-# INLINE getByte #-}
37
-
38
- getTwoBytes :: Decode (Word8 , Word8 )
39
- getTwoBytes = fixed 2 $ \ ptr -> do
40
- b1 <- peek ptr
41
- b2 <- peekByteOff ptr 1
42
- return (b1, b2)
43
- {-# INLINE getTwoBytes #-}
44
-
B41A
45
- getFourBytes :: Decode (Word8 , Word8 , Word8 , Word8 )
46
- getFourBytes = fixed 4 $ \ ptr -> do
47
- b1 <- peek ptr
48
- b2 <- peekByteOff ptr 1
49
- b3 <- peekByteOff ptr 2
50
- b4 <- peekByteOff ptr 3
51
- return (b1, b2, b3, b4)
52
- {-# INLINE getFourBytes #-}
53
33
54
- skipBytes :: Int -> Decode ()
55
- skipBytes n = fixed n $ const $ pure ()
56
-
57
- -----------
58
34
-- Public
59
35
36
+ {-# INLINE skipBytes #-}
37
+ skipBytes :: Int -> Decode ()
38
+ skipBytes n = prim n $ const $ pure ()
39
+
40
+ {-# INLINE getByteString #-}
60
41
getByteString :: Int -> Decode B. ByteString
61
42
getByteString len = Decode $ Peek $ \ ps ptr -> do
62
43
bs <- B. packCStringLen (castPtr ptr, len)
63
44
let ! newPtr = ptr `plusPtr` len
64
45
-- return $ PeekResult newPtr bs
65
46
return (newPtr, bs)
66
- {-# INLINE getByteString #-}
67
47
48
+ {-# INLINE getByteStringNull #-}
68
49
getByteStringNull :: Decode B. ByteString
69
50
getByteStringNull = Decode $ Peek $ \ ps ptr -> do
70
51
bs <- B. packCString (castPtr ptr)
71
52
let ! newPtr = ptr `plusPtr` (B. length bs + 1 )
72
53
-- return $ PeekResult newPtr bs
73
54
return (newPtr, bs)
74
- {-# INLINE getByteStringNull #-}
75
55
76
- getWord8 :: Decode Word8
77
- getWord8 = getByte
78
56
{-# INLINE getWord8 #-}
57
+ getWord8 :: Decode Word8
58
+ getWord8 = prim 1 peek
79
59
80
- getWord16BE :: Decode Word16
81
- getWord16BE = fixed 2 $ \ ptr -> byteSwap16 <$> peek (castPtr ptr)
82
- {-# INLINE getWord16BE #-}
83
-
84
- getWord32BE :: Decode Word32
85
- getWord32BE = fixed 4 $ \ ptr -> byteSwap32 <$> peek (castPtr ptr)
86
- {-# INLINE getWord32BE #-}
87
-
88
- getInt16BE :: Decode Int16
89
- getInt16BE = fromIntegral <$> getWord16BE
90
60
{-# INLINE getInt16BE #-}
61
+ getInt16BE :: Decode Int16
62
+ getInt16BE = prim 2 $ \ ptr -> fromIntegral . byteSwap16 <$> peek (castPtr ptr)
91
63
92
- getInt32BE :: Decode Int32
93
- getInt32BE = fromIntegral <$> getWord32BE
94
64
{-# INLINE getInt32BE #-}
95
-
65
+ getInt32BE :: Decode Int32
66
+ getInt32BE = prim 4 $ \ ptr -> fromIntegral . byteSwap32 <$> peek (castPtr ptr)
67
+
68
+ {-# INLINE getInt64BE #-}
69
+ getInt64BE :: Decode Int64
70
+ getInt64BE = prim 8 $ \ ptr -> fromIntegral . byteSwap64 <$> peek (castPtr ptr)
71
+
72
+ {-# INLINE getFloat32BE #-}
73
+ getFloat32BE :: Decode Float
74
+ getFloat32BE = prim 4 $ \ ptr -> byteSwap32 <$> peek (castPtr ptr)
75
+ >>= wordToFloat
76
+
77
+ {-# INLINE getFloat64BE #-}
78
+ getFloat64BE :: Decode Double
79
+ getFloat64BE = prim 8 $ \ ptr -> byteSwap64 <$> peek (castPtr ptr)
80
+ >>= wordToFloat
81
+
82
+ {-# INLINE wordToFloat #-}
83
+ wordToFloat :: (Storable word , Storable float ) => word -> IO float
84
+ wordToFloat word = alloca $ \ buf -> do
85
+ poke (castPtr buf) word
86
+ peek buf
0 commit comments