{-# LANGUAGE FlexibleContexts #-}
module Raaz.Core.Util.ByteString
( length, replicate
, create, createFrom
, withByteString
, unsafeCopyToPointer
, unsafeNCopyToPointer
) where
import Prelude hiding (length, replicate)
import qualified Data.ByteString as B
import Data.ByteString (ByteString)
import qualified Data.ByteString.Internal as BI
import Data.Word
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Ptr (castPtr, plusPtr)
import Raaz.Core.Types.Pointer
import Raaz.Core.Types.Copying
length :: ByteString -> BYTES Int
length :: ByteString -> BYTES Int
length = Int -> BYTES Int
forall a. a -> BYTES a
BYTES (Int -> BYTES Int)
-> (ByteString -> Int) -> ByteString -> BYTES Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
B.length
replicate :: LengthUnit l => l -> Word8 -> ByteString
replicate :: l -> Word8 -> ByteString
replicate l
l = Int -> Word8 -> ByteString
B.replicate Int
sz
where BYTES Int
sz = l -> BYTES Int
forall u. LengthUnit u => u -> BYTES Int
inBytes l
l
unsafeCopyToPointer :: Pointer ptr
=> ByteString
-> ptr a
-> IO ()
unsafeCopyToPointer :: ByteString -> ptr a -> IO ()
unsafeCopyToPointer ByteString
bs ptr a
cptr = ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fptr ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$
\ Ptr Word8
p -> Dest (ptr a) -> Src (Ptr Any) -> BYTES Int -> IO ()
forall l (ptrS :: * -> *) (ptrD :: * -> *) dest src.
(LengthUnit l, Pointer ptrS, Pointer ptrD) =>
Dest (ptrD dest) -> Src (ptrS src) -> l -> IO ()
memcpy (ptr a -> Dest (ptr a)
forall a. a -> Dest a
destination ptr a
cptr) (Ptr Any -> Src (Ptr Any)
forall a. a -> Src a
source (Ptr Any -> Src (Ptr Any)) -> Ptr Any -> Src (Ptr Any)
forall a b. (a -> b) -> a -> b
$ Ptr Word8
p Ptr Word8 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
offset) (Int -> BYTES Int
forall a. a -> BYTES a
BYTES Int
n)
where (ForeignPtr Word8
fptr, Int
offset,Int
n) = ByteString -> (ForeignPtr Word8, Int, Int)
BI.toForeignPtr ByteString
bs
unsafeNCopyToPointer :: LengthUnit n
=> n
-> ByteString
-> Ptr a
-> IO ()
unsafeNCopyToPointer :: n -> ByteString -> Ptr a -> IO ()
unsafeNCopyToPointer n
n ByteString
bs Ptr a
cptr = ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fptr ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$
\ Ptr Word8
p -> Dest (Ptr a) -> Src (Ptr Any) -> n -> IO ()
forall l (ptrS :: * -> *) (ptrD :: * -> *) dest src.
(LengthUnit l, Pointer ptrS, Pointer ptrD) =>
Dest (ptrD dest) -> Src (ptrS src) -> l -> IO ()
memcpy (Ptr a -> Dest (Ptr a)
forall a. a -> Dest a
destination Ptr a
cptr) (Ptr Any -> Src (Ptr Any)
forall a. a -> Src a
source (Ptr Any -> Src (Ptr Any)) -> Ptr Any -> Src (Ptr Any)
forall a b. (a -> b) -> a -> b
$ Ptr Word8
p Ptr Word8 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
offset) n
n
where (ForeignPtr Word8
fptr, Int
offset,Int
_) = ByteString -> (ForeignPtr Word8, Int, Int)
BI.toForeignPtr ByteString
bs
withByteString :: ByteString -> (Ptr something -> IO a) -> IO a
withByteString :: ByteString -> (Ptr something -> IO a) -> IO a
withByteString ByteString
bs Ptr something -> IO a
f = ForeignPtr Word8 -> (Ptr Word8 -> IO a) -> IO a
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fptr (Ptr something -> IO a
f (Ptr something -> IO a)
-> (Ptr Word8 -> Ptr something) -> Ptr Word8 -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ptr Word8 -> Int -> Ptr something)
-> Int -> Ptr Word8 -> Ptr something
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr Word8 -> Int -> Ptr something
forall a b. Ptr a -> Int -> Ptr b
plusPtr Int
off)
where (ForeignPtr Word8
fptr, Int
off, Int
_) = ByteString -> (ForeignPtr Word8, Int, Int)
BI.toForeignPtr ByteString
bs
create :: LengthUnit l => l -> (Ptr a -> IO ()) -> IO ByteString
create :: l -> (Ptr a -> IO ()) -> IO ByteString
create l
l Ptr a -> IO ()
act = (Ptr Word8 -> IO ()) -> IO ByteString
myCreate (Ptr a -> IO ()
act (Ptr a -> IO ()) -> (Ptr Word8 -> Ptr a) -> Ptr Word8 -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr Word8 -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr)
where myCreate :: (Ptr Word8 -> IO ()) -> IO ByteString
myCreate = Int -> (Ptr Word8 -> IO ()) -> IO ByteString
BI.create (Int -> (Ptr Word8 -> IO ()) -> IO ByteString)
-> Int -> (Ptr Word8 -> IO ()) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ BYTES Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (BYTES Int -> Int) -> BYTES Int -> Int
forall a b. (a -> b) -> a -> b
$ l -> BYTES Int
forall u. LengthUnit u => u -> BYTES Int
inBytes l
l
createFrom :: LengthUnit l => l -> Ptr a -> IO ByteString
createFrom :: l -> Ptr a -> IO ByteString
createFrom l
l Ptr a
cptr = l -> (Ptr Any -> IO ()) -> IO ByteString
forall l a. LengthUnit l => l -> (Ptr a -> IO ()) -> IO ByteString
create l
l Ptr Any -> IO ()
forall (ptrD :: * -> *) dest. Pointer ptrD => ptrD dest -> IO ()
filler
where filler :: ptrD dest -> IO ()
filler ptrD dest
dptr = Dest (ptrD dest) -> Src (Ptr a) -> l -> IO ()
forall l (ptrS :: * -> *) (ptrD :: * -> *) dest src.
(LengthUnit l, Pointer ptrS, Pointer ptrD) =>
Dest (ptrD dest) -> Src (ptrS src) -> l -> IO ()
memcpy (ptrD dest -> Dest (ptrD dest)
forall a. a -> Dest a
destination ptrD dest
dptr) (Ptr a -> Src (Ptr a)
forall a. a -> Src a
source Ptr a
cptr) l
l