{-# language BangPatterns #-}
{-# language DataKinds #-}
{-# language MagicHash #-}
{-# language TypeFamilies #-}
{-# language DuplicateRecordFields #-}

-- This needs to be in its own module to prevent a cyclic dependency
-- between UnliftedBytes and Data.Bytes.Types
module Data.Bytes.Internal
  ( Bytes(..)
  ) where

import Control.Monad.ST (runST)
import Control.Monad.ST.Run (runByteArrayST)
import Data.Primitive (ByteArray(..))
import Data.Word (Word8)
import GHC.Exts (Int(I#),unsafeCoerce#,sameMutableByteArray#)
import GHC.Exts (isTrue#,compareByteArrays#,IsList(..))
import Data.Bytes.Internal.Show (showsSlice)

import qualified Data.List as L
import qualified Data.Foldable as F
import qualified Data.Primitive as PM

-- | A slice of a 'ByteArray'.
data Bytes = Bytes
  { Bytes -> ByteArray
array :: {-# UNPACK #-} !ByteArray
  , Bytes -> Int
offset :: {-# UNPACK #-} !Int
  , Bytes -> Int
length :: {-# UNPACK #-} !Int
  }

instance IsList Bytes where
  type Item Bytes = Word8
  fromListN :: Int -> [Item Bytes] -> Bytes
fromListN Int
n [Item Bytes]
xs = ByteArray -> Int -> Int -> Bytes
Bytes (forall l. IsList l => Int -> [Item l] -> l
fromListN Int
n [Item Bytes]
xs) Int
0 Int
n
  fromList :: [Item Bytes] -> Bytes
fromList [Item Bytes]
xs = forall l. IsList l => Int -> [Item l] -> l
fromListN (forall (t :: * -> *) a. Foldable t => t a -> Int
L.length [Item Bytes]
xs) [Item Bytes]
xs
  toList :: Bytes -> [Item Bytes]
toList (Bytes ByteArray
arr Int
off Int
len) = Int -> Int -> ByteArray -> [Word8]
toListLoop Int
off Int
len ByteArray
arr

toListLoop :: Int -> Int -> ByteArray -> [Word8]
toListLoop :: Int -> Int -> ByteArray -> [Word8]
toListLoop !Int
off !Int
len !ByteArray
arr = if Int
len forall a. Ord a => a -> a -> Bool
> Int
0
  then forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
arr Int
off forall a. a -> [a] -> [a]
: Int -> Int -> ByteArray -> [Word8]
toListLoop (Int
off forall a. Num a => a -> a -> a
+ Int
1) (Int
len forall a. Num a => a -> a -> a
- Int
1) ByteArray
arr
  else []

instance Show Bytes where
  showsPrec :: Int -> Bytes -> ShowS
showsPrec Int
_ (Bytes ByteArray
arr Int
off Int
len) String
s = ByteArray -> Int -> Int -> ShowS
showsSlice ByteArray
arr Int
off Int
len String
s

instance Eq Bytes where
  Bytes ByteArray
arr1 Int
off1 Int
len1 == :: Bytes -> Bytes -> Bool
== Bytes ByteArray
arr2 Int
off2 Int
len2
    | Int
len1 forall a. Eq a => a -> a -> Bool
/= Int
len2 = Bool
False
    | ByteArray -> ByteArray -> Bool
sameByteArray ByteArray
arr1 ByteArray
arr2 Bool -> Bool -> Bool
&& Int
off1 forall a. Eq a => a -> a -> Bool
== Int
off2 = Bool
True
    | Bool
otherwise = ByteArray -> Int -> ByteArray -> Int -> Int -> Ordering
compareByteArrays ByteArray
arr1 Int
off1 ByteArray
arr2 Int
off2 Int
len1 forall a. Eq a => a -> a -> Bool
== Ordering
EQ

instance Ord Bytes where
  compare :: Bytes -> Bytes -> Ordering
compare (Bytes ByteArray
arr1 Int
off1 Int
len1) (Bytes ByteArray
arr2 Int
off2 Int
len2)
    | ByteArray -> ByteArray -> Bool
sameByteArray ByteArray
arr1 ByteArray
arr2 Bool -> Bool -> Bool
&& Int
off1 forall a. Eq a => a -> a -> Bool
== Int
off2 Bool -> Bool -> Bool
&& Int
len1 forall a. Eq a => a -> a -> Bool
== Int
len2 = Ordering
EQ
    | Bool
otherwise = ByteArray -> Int -> ByteArray -> Int -> Int -> Ordering
compareByteArrays ByteArray
arr1 Int
off1 ByteArray
arr2 Int
off2 (forall a. Ord a => a -> a -> a
min Int
len1 Int
len2) forall a. Semigroup a => a -> a -> a
<> forall a. Ord a => a -> a -> Ordering
compare Int
len1 Int
len2

instance Semigroup Bytes where
  -- TODO: Do the trick to move the data constructor to the outside
  -- of runST.
  Bytes ByteArray
arrA Int
offA Int
lenA <> :: Bytes -> Bytes -> Bytes
<> Bytes ByteArray
arrB Int
offB Int
lenB = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
    MutableByteArray s
marr <- forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
PM.newByteArray (Int
lenA forall a. Num a => a -> a -> a
+ Int
lenB)
    forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m)
-> Int -> ByteArray -> Int -> Int -> m ()
PM.copyByteArray MutableByteArray s
marr Int
0 ByteArray
arrA Int
offA Int
lenA
    forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m)
-> Int -> ByteArray -> Int -> Int -> m ()
PM.copyByteArray MutableByteArray s
marr Int
lenA ByteArray
arrB Int
offB Int
lenB
    ByteArray
r <- forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m ByteArray
PM.unsafeFreezeByteArray MutableByteArray s
marr
    forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteArray -> Int -> Int -> Bytes
Bytes ByteArray
r Int
0 (Int
lenA forall a. Num a => a -> a -> a
+ Int
lenB))

instance Monoid Bytes where
  mempty :: Bytes
mempty = ByteArray -> Int -> Int -> Bytes
Bytes forall a. Monoid a => a
mempty Int
0 Int
0
  mconcat :: [Bytes] -> Bytes
mconcat [] = forall a. Monoid a => a
mempty
  mconcat [Bytes
x] = Bytes
x
  mconcat [Bytes]
bs = ByteArray -> Int -> Int -> Bytes
Bytes ByteArray
r Int
0 Int
fullLen
    where
    !fullLen :: Int
fullLen = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
L.foldl' (\Int
acc (Bytes ByteArray
_ Int
_ Int
len) -> Int
acc forall a. Num a => a -> a -> a
+ Int
len) Int
0 [Bytes]
bs
    r :: ByteArray
r = (forall s. ST s ByteArray) -> ByteArray
runByteArrayST forall a b. (a -> b) -> a -> b
$ do
      MutableByteArray s
marr <- forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
PM.newByteArray Int
fullLen
      !Int
_ <- forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
F.foldlM
        (\ !Int
currLen (Bytes ByteArray
arr Int
off Int
len) -> do
          forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m)
-> Int -> ByteArray -> Int -> Int -> m ()
PM.copyByteArray MutableByteArray s
marr Int
currLen ByteArray
arr Int
off Int
len
          forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
currLen forall a. Num a => a -> a -> a
+ Int
len)
        ) Int
0 [Bytes]
bs
      forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m ByteArray
PM.unsafeFreezeByteArray MutableByteArray s
marr

compareByteArrays :: ByteArray -> Int -> ByteArray -> Int -> Int -> Ordering
{-# INLINE compareByteArrays #-}
compareByteArrays :: ByteArray -> Int -> ByteArray -> Int -> Int -> Ordering
compareByteArrays (ByteArray ByteArray#
ba1#) (I# Int#
off1#) (ByteArray ByteArray#
ba2#) (I# Int#
off2#) (I# Int#
n#) =
  forall a. Ord a => a -> a -> Ordering
compare (Int# -> Int
I# (ByteArray# -> Int# -> ByteArray# -> Int# -> Int# -> Int#
compareByteArrays# ByteArray#
ba1# Int#
off1# ByteArray#
ba2# Int#
off2# Int#
n#)) Int
0

sameByteArray :: ByteArray -> ByteArray -> Bool
{-# INLINE sameByteArray #-}
sameByteArray :: ByteArray -> ByteArray -> Bool
sameByteArray (ByteArray ByteArray#
ba1#) (ByteArray ByteArray#
ba2#) =
  Int# -> Bool
isTrue# (forall d. MutableByteArray# d -> MutableByteArray# d -> Int#
sameMutableByteArray# (unsafeCoerce# :: forall a b. a -> b
unsafeCoerce# ByteArray#
ba1#) (unsafeCoerce# :: forall a b. a -> b
unsafeCoerce# ByteArray#
ba2#))