{-# language BangPatterns #-}
{-# language LambdaCase #-}
module Data.Bytes.Mutable
(
MutableBytes
, takeWhile
, dropWhile
, unsafeTake
, unsafeDrop
, fromMutableByteArray
) where
import Prelude hiding (takeWhile,dropWhile)
import Data.Bytes.Types (MutableBytes(MutableBytes))
import Data.Primitive (MutableByteArray)
import Data.Word (Word8)
import Control.Monad.Primitive (PrimMonad,PrimState)
import qualified Data.Primitive as PM
takeWhile :: PrimMonad m
=> (Word8 -> m Bool)
-> MutableBytes (PrimState m)
-> m (MutableBytes (PrimState m))
{-# inline takeWhile #-}
takeWhile :: forall (m :: * -> *).
PrimMonad m =>
(Word8 -> m Bool)
-> MutableBytes (PrimState m) -> m (MutableBytes (PrimState m))
takeWhile Word8 -> m Bool
k MutableBytes (PrimState m)
b = do
Int
n <- forall (m :: * -> *).
PrimMonad m =>
(Word8 -> m Bool) -> MutableBytes (PrimState m) -> m Int
countWhile Word8 -> m Bool
k MutableBytes (PrimState m)
b
forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall s. Int -> MutableBytes s -> MutableBytes s
unsafeTake Int
n MutableBytes (PrimState m)
b)
dropWhile :: PrimMonad m
=> (Word8 -> m Bool)
-> MutableBytes (PrimState m)
-> m (MutableBytes (PrimState m))
{-# inline dropWhile #-}
dropWhile :: forall (m :: * -> *).
PrimMonad m =>
(Word8 -> m Bool)
-> MutableBytes (PrimState m) -> m (MutableBytes (PrimState m))
dropWhile Word8 -> m Bool
k MutableBytes (PrimState m)
b = do
Int
n <- forall (m :: * -> *).
PrimMonad m =>
(Word8 -> m Bool) -> MutableBytes (PrimState m) -> m Int
countWhile Word8 -> m Bool
k MutableBytes (PrimState m)
b
forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall s. Int -> MutableBytes s -> MutableBytes s
unsafeDrop Int
n MutableBytes (PrimState m)
b)
unsafeTake :: Int -> MutableBytes s -> MutableBytes s
{-# inline unsafeTake #-}
unsafeTake :: forall s. Int -> MutableBytes s -> MutableBytes s
unsafeTake Int
n (MutableBytes MutableByteArray s
arr Int
off Int
_) =
forall s. MutableByteArray s -> Int -> Int -> MutableBytes s
MutableBytes MutableByteArray s
arr Int
off Int
n
unsafeDrop :: Int -> MutableBytes s -> MutableBytes s
{-# inline unsafeDrop #-}
unsafeDrop :: forall s. Int -> MutableBytes s -> MutableBytes s
unsafeDrop Int
n (MutableBytes MutableByteArray s
arr Int
off Int
len) =
forall s. MutableByteArray s -> Int -> Int -> MutableBytes s
MutableBytes MutableByteArray s
arr (Int
off forall a. Num a => a -> a -> a
+ Int
n) (Int
len forall a. Num a => a -> a -> a
- Int
n)
fromMutableByteArray :: PrimMonad m
=> MutableByteArray (PrimState m)
-> m (MutableBytes (PrimState m))
{-# inline fromMutableByteArray #-}
fromMutableByteArray :: forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m (MutableBytes (PrimState m))
fromMutableByteArray MutableByteArray (PrimState m)
mba = do
Int
sz <- forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m Int
PM.getSizeofMutableByteArray MutableByteArray (PrimState m)
mba
forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall s. MutableByteArray s -> Int -> Int -> MutableBytes s
MutableBytes MutableByteArray (PrimState m)
mba Int
0 Int
sz)
countWhile :: PrimMonad m
=> (Word8 -> m Bool)
-> MutableBytes (PrimState m)
-> m Int
{-# inline countWhile #-}
countWhile :: forall (m :: * -> *).
PrimMonad m =>
(Word8 -> m Bool) -> MutableBytes (PrimState m) -> m Int
countWhile Word8 -> m Bool
k (MutableBytes MutableByteArray (PrimState m)
arr Int
off0 Int
len0) = forall {t} {b}. (Ord t, Num t, Num b) => Int -> t -> b -> m b
go Int
off0 Int
len0 Int
0 where
go :: Int -> t -> b -> m b
go !Int
off !t
len !b
n = if t
len forall a. Ord a => a -> a -> Bool
> t
0
then (Word8 -> m Bool
k forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> m a
PM.readByteArray MutableByteArray (PrimState m)
arr Int
off) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Bool
True -> Int -> t -> b -> m b
go (Int
off forall a. Num a => a -> a -> a
+ Int
1) (t
len forall a. Num a => a -> a -> a
- t
1) (b
n forall a. Num a => a -> a -> a
+ b
1)
Bool
False -> forall (f :: * -> *) a. Applicative f => a -> f a
pure b
n
else forall (f :: * -> *) a. Applicative f => a -> f a
pure b
n