module Codec.Borsh.Internal.Util.ByteString (
peekByteString
, splitAtEnd
, lengthStrict
, lengthLazy
) where
import Foreign
import System.IO.Unsafe (unsafePerformIO)
import qualified Data.ByteString as S
import qualified Data.ByteString.Internal as S.Internal
import qualified Data.ByteString.Lazy as L
import Codec.Borsh.Internal.Util.ByteSwap
peekByteString :: forall a.
ByteSwap a
=> S.ByteString
-> Maybe (a, Word32, S.ByteString)
peekByteString :: forall a. ByteSwap a => ByteString -> Maybe (a, Word32, ByteString)
peekByteString ByteString
bs
| Int
sizeA forall a. Ord a => a -> a -> Bool
> Int
len = forall a. Maybe a
Nothing
| Bool
otherwise = forall a. a -> Maybe a
Just (
forall a. ByteSwap a => LE a -> a
fromLE forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> LE a
LE forall a b. (a -> b) -> a -> b
$ forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr (forall a b. ForeignPtr a -> Int -> ForeignPtr b
plusForeignPtr ForeignPtr Word8
fPtr Int
offset) (forall a. Storable a => Ptr a -> IO a
peek forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr Word8 -> Ptr a
cast)
, forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
sizeA
, Int -> ByteString -> ByteString
S.drop Int
sizeA ByteString
bs
)
where
sizeA :: Int
sizeA :: Int
sizeA = forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: a)
fPtr :: ForeignPtr Word8
offset, len :: Int
(ForeignPtr Word8
fPtr, Int
offset, Int
len) = ByteString -> (ForeignPtr Word8, Int, Int)
S.Internal.toForeignPtr ByteString
bs
cast :: Ptr Word8 -> Ptr a
cast :: Ptr Word8 -> Ptr a
cast = forall a b. Ptr a -> Ptr b
castPtr
splitAtEnd ::
Int
-> S.ByteString
-> (S.ByteString, S.ByteString)
splitAtEnd :: Int -> ByteString -> (ByteString, ByteString)
splitAtEnd Int
n ByteString
bs = Int -> ByteString -> (ByteString, ByteString)
S.splitAt Int
n' ByteString
bs
where
n' :: Int
n' :: Int
n' = ByteString -> Int
S.length ByteString
bs forall a. Num a => a -> a -> a
- Int
n
lengthStrict :: S.ByteString -> Word32
lengthStrict :: ByteString -> Word32
lengthStrict = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
S.length
lengthLazy :: L.ByteString -> Word32
lengthLazy :: ByteString -> Word32
lengthLazy = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int64
L.length