{-# language BangPatterns #-}
{-# language BinaryLiterals #-}
{-# language DataKinds #-}
{-# language DeriveFunctor #-}
{-# language DerivingStrategies #-}
{-# language GADTSyntax #-}
{-# language KindSignatures #-}
{-# language LambdaCase #-}
{-# language MagicHash #-}
{-# language MultiWayIf #-}
{-# language NamedFieldPuns #-}
{-# language PolyKinds #-}
{-# language RankNTypes #-}
{-# language ScopedTypeVariables #-}
{-# language StandaloneDeriving #-}
{-# language TypeApplications #-}
{-# language UnboxedSums #-}
{-# language UnboxedTuples #-}

module Data.Bytes.Parser.Internal
  ( Parser(..)
  , Result(..)
  , InternalStep(..)
  , Bytes#
  , ST#
  , Result#
  , unfailing
  , uneffectful
  , uneffectful#
  , uneffectfulInt#
  , boxBytes
  , unboxBytes
  , unboxResult
  , fail
  , indexLatinCharArray
  , upcastUnitSuccess
    -- Swapping
  , swapArray16
  , swapArray32
  , swapArray64
  , swapArray128
  , swapArray256
  ) where

import Prelude hiding (length,any,fail,takeWhile)

import Control.Applicative (Alternative)
import Control.Monad.ST.Run (runByteArrayST)
import Data.Primitive (ByteArray(ByteArray))
import Data.Bytes.Types (Bytes(..))
import Data.Kind (Type)
import Data.Word (Word8)
import GHC.Exts (TYPE,RuntimeRep,Int(I#),Int#,State#,ByteArray#,Char(C#))

import qualified Control.Applicative
import qualified Control.Monad
import qualified Data.Primitive as PM
import qualified GHC.Exts as Exts

-- | A non-resumable parser.
newtype Parser :: forall (r :: RuntimeRep). Type -> Type -> TYPE r -> Type where
  Parser :: forall (r :: RuntimeRep) (e :: Type) (s :: Type) (a :: TYPE r).
    { Parser e s a -> (# ByteArray#, Int#, Int# #) -> ST# s (Result# e a)
runParser :: (# ByteArray#, Int#, Int# #) -> ST# s (Result# e a) } -> Parser e s a

-- The result of running a parser. Used internally.
data Result e a
  = Failure e
    -- An error message indicating what went wrong.
  | Success !a !Int !Int
    -- The parsed value, the offset after the last consumed byte, and the
    -- number of bytes remaining in parsed slice.

data InternalStep a = InternalStep !a !Int !Int

uneffectful :: (Bytes -> Result e a) -> Parser e s a
{-# inline uneffectful #-}
uneffectful :: (Bytes -> Result e a) -> Parser e s a
uneffectful Bytes -> Result e a
f = ((# ByteArray#, Int#, Int# #) -> ST# s (Result# e a))
-> Parser e s a
forall e s a.
((# ByteArray#, Int#, Int# #) -> ST# s (Result# e a))
-> Parser e s a
Parser
  ( \(# ByteArray#, Int#, Int# #)
b State# s
s0 -> (# State# s
s0, Result e a -> Result# e a
forall e a. Result e a -> Result# e a
unboxResult (Bytes -> Result e a
f ((# ByteArray#, Int#, Int# #) -> Bytes
boxBytes (# ByteArray#, Int#, Int# #)
b)) #) )

-- This is like uneffectful but for parsers that always succeed.
-- These combinators typically have names that begin with @try@.
unfailing :: (Bytes -> InternalStep a) -> Parser e s a
{-# inline unfailing #-}
unfailing :: (Bytes -> InternalStep a) -> Parser e s a
unfailing Bytes -> InternalStep a
f = ((# ByteArray#, Int#, Int# #) -> ST# s (Result# e a))
-> Parser e s a
forall e s a.
((# ByteArray#, Int#, Int# #) -> ST# s (Result# e a))
-> Parser e s a
Parser
  ( \(# ByteArray#, Int#, Int# #)
b State# s
s0 -> (# State# s
s0, case Bytes -> InternalStep a
f ((# ByteArray#, Int#, Int# #) -> Bytes
boxBytes (# ByteArray#, Int#, Int# #)
b) of { InternalStep a
a (I# Int#
off) (I# Int#
len) -> (# | (# a
a, Int#
off, Int#
len #) #) } #) )

boxBytes :: Bytes# -> Bytes
{-# inline boxBytes #-}
boxBytes :: (# ByteArray#, Int#, Int# #) -> Bytes
boxBytes (# ByteArray#
a, Int#
b, Int#
c #) = ByteArray -> Int -> Int -> Bytes
Bytes (ByteArray# -> ByteArray
ByteArray ByteArray#
a) (Int# -> Int
I# Int#
b) (Int# -> Int
I# Int#
c)

unboxBytes :: Bytes -> Bytes#
{-# inline unboxBytes #-}
unboxBytes :: Bytes -> (# ByteArray#, Int#, Int# #)
unboxBytes (Bytes (ByteArray ByteArray#
a) (I# Int#
b) (I# Int#
c)) = (# ByteArray#
a,Int#
b,Int#
c #)

type Bytes# = (# ByteArray#, Int#, Int# #)
type ST# s (a :: TYPE r) = State# s -> (# State# s, a #)
type Result# e (a :: TYPE r) =
  (# e
  | (# a, Int#, Int# #) #) -- ints are offset and length

unboxResult :: Result e a -> Result# e a
{-# inline unboxResult #-}
unboxResult :: Result e a -> Result# e a
unboxResult (Success a
a (I# Int#
b) (I# Int#
c)) = (# | (# a
a, Int#
b, Int#
c #) #)
unboxResult (Failure e
e) = (# e
e | #)

-- | Combines the error messages using '<>' when both
-- parsers fail.
instance Monoid e => Alternative (Parser e s) where
  {-# inline empty #-}
  {-# inline (<|>) #-}
  empty :: Parser e s a
empty = e -> Parser e s a
forall e s a. e -> Parser e s a
fail e
forall a. Monoid a => a
mempty
  Parser (# ByteArray#, Int#, Int# #) -> ST# s (Result# e a)
f <|> :: Parser e s a -> Parser e s a -> Parser e s a
<|> Parser (# ByteArray#, Int#, Int# #) -> ST# s (Result# e a)
g = ((# ByteArray#, Int#, Int# #) -> ST# s (Result# e a))
-> Parser e s a
forall e s a.
((# ByteArray#, Int#, Int# #) -> ST# s (Result# e a))
-> Parser e s a
Parser
    (\(# ByteArray#, Int#, Int# #)
x State# s
s0 -> case (# ByteArray#, Int#, Int# #) -> ST# s (Result# e a)
f (# ByteArray#, Int#, Int# #)
x State# s
s0 of
      (# State# s
s1, Result# e a
r0 #) -> case Result# e a
r0 of
        (# e
eRight | #) -> case (# ByteArray#, Int#, Int# #) -> ST# s (Result# e a)
g (# ByteArray#, Int#, Int# #)
x State# s
s1 of
          (# State# s
s2, Result# e a
r1 #) -> case Result# e a
r1 of
            (# e
eLeft | #) -> (# State# s
s2, (# e
eRight e -> e -> e
forall a. Semigroup a => a -> a -> a
<> e
eLeft | #) #)
            (# | (# a, Int#, Int# #)
r #) -> (# State# s
s2, (# | (# a, Int#, Int# #)
r #) #)
        (# | (# a, Int#, Int# #)
r #) -> (# State# s
s1, (# | (# a, Int#, Int# #)
r #) #)
    )

-- | Fail with the provided error message.
fail ::
     e -- ^ Error message
  -> Parser e s a
{-# inline fail #-}
fail :: e -> Parser e s a
fail e
e = (Bytes -> Result e a) -> Parser e s a
forall e a s. (Bytes -> Result e a) -> Parser e s a
uneffectful ((Bytes -> Result e a) -> Parser e s a)
-> (Bytes -> Result e a) -> Parser e s a
forall a b. (a -> b) -> a -> b
$ \Bytes
_ -> e -> Result e a
forall e a. e -> Result e a
Failure e
e

instance Applicative (Parser e s) where
  pure :: a -> Parser e s a
pure = a -> Parser e s a
forall a e s. a -> Parser e s a
pureParser
  <*> :: Parser e s (a -> b) -> Parser e s a -> Parser e s b
(<*>) = Parser e s (a -> b) -> Parser e s a -> Parser e s b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
Control.Monad.ap

instance Monad (Parser e s) where
  {-# inline (>>=) #-}
  >>= :: Parser e s a -> (a -> Parser e s b) -> Parser e s b
(>>=) = Parser e s a -> (a -> Parser e s b) -> Parser e s b
forall e s a b. Parser e s a -> (a -> Parser e s b) -> Parser e s b
bindParser

instance Functor (Parser e s) where
  {-# inline fmap #-}
  fmap :: (a -> b) -> Parser e s a -> Parser e s b
fmap a -> b
f (Parser (# ByteArray#, Int#, Int# #) -> ST# s (Result# e a)
g) = ((# ByteArray#, Int#, Int# #) -> ST# s (Result# e b))
-> Parser e s b
forall e s a.
((# ByteArray#, Int#, Int# #) -> ST# s (Result# e a))
-> Parser e s a
Parser
    (\(# ByteArray#, Int#, Int# #)
x State# s
s0 -> case (# ByteArray#, Int#, Int# #) -> ST# s (Result# e a)
g (# ByteArray#, Int#, Int# #)
x State# s
s0 of
      (# State# s
s1, Result# e a
r #) -> case Result# e a
r of
        (# e
e | #) -> (# State# s
s1, (# e
e | #) #)
        (# | (# a
a, Int#
b, Int#
c #) #) -> (# State# s
s1, (# | (# a -> b
f a
a, Int#
b, Int#
c #) #) #)
    )

indexLatinCharArray :: ByteArray -> Int -> Char
{-# inline indexLatinCharArray #-}
indexLatinCharArray :: ByteArray -> Int -> Char
indexLatinCharArray (ByteArray ByteArray#
arr) (I# Int#
off) =
  Char# -> Char
C# (ByteArray# -> Int# -> Char#
Exts.indexCharArray# ByteArray#
arr Int#
off)

uneffectful# :: (Bytes -> Result# e a) -> Parser e s a
{-# inline uneffectful# #-}
uneffectful# :: (Bytes -> Result# e a) -> Parser e s a
uneffectful# Bytes -> Result# e a
f = ((# ByteArray#, Int#, Int# #) -> ST# s (Result# e a))
-> Parser e s a
forall e s a.
((# ByteArray#, Int#, Int# #) -> ST# s (Result# e a))
-> Parser e s a
Parser
  ( \(# ByteArray#, Int#, Int# #)
b State# s
s0 -> (# State# s
s0, (Bytes -> Result# e a
f ((# ByteArray#, Int#, Int# #) -> Bytes
boxBytes (# ByteArray#, Int#, Int# #)
b)) #) )

uneffectfulInt# :: (Bytes -> Result# e Int# ) -> Parser e s Int#
{-# inline uneffectfulInt# #-}
uneffectfulInt# :: (Bytes -> Result# e Int#) -> Parser e s Int#
uneffectfulInt# Bytes -> Result# e Int#
f = ((# ByteArray#, Int#, Int# #) -> ST# s (Result# e Int#))
-> Parser e s Int#
forall e s a.
((# ByteArray#, Int#, Int# #) -> ST# s (Result# e a))
-> Parser e s a
Parser
  ( \(# ByteArray#, Int#, Int# #)
b State# s
s0 -> (# State# s
s0, (Bytes -> Result# e Int#
f ((# ByteArray#, Int#, Int# #) -> Bytes
boxBytes (# ByteArray#, Int#, Int# #)
b)) #) )

upcastUnitSuccess :: (# Int#, Int# #) -> Result# e ()
{-# inline upcastUnitSuccess #-}
upcastUnitSuccess :: (# Int#, Int# #) -> Result# e ()
upcastUnitSuccess (# Int#
b, Int#
c #) = (# | (# (), Int#
b, Int#
c #) #)

swapArray16 :: Bytes -> ByteArray
swapArray16 :: Bytes -> ByteArray
swapArray16 (Bytes{ByteArray
$sel:array:Bytes :: Bytes -> ByteArray
array :: ByteArray
array,Int
$sel:offset:Bytes :: Bytes -> Int
offset :: Int
offset,Int
$sel:length:Bytes :: Bytes -> Int
length :: Int
length}) = (forall s. ST s ByteArray) -> ByteArray
runByteArrayST ((forall s. ST s ByteArray) -> ByteArray)
-> (forall s. ST s ByteArray) -> ByteArray
forall a b. (a -> b) -> a -> b
$ do
  MutableByteArray s
dst <- Int -> ST s (MutableByteArray (PrimState (ST s)))
forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
PM.newByteArray Int
length
  let go :: Int -> Int -> t -> ST s ()
go !Int
ixSrc !Int
ixDst !t
len = if t
len t -> t -> Bool
forall a. Ord a => a -> a -> Bool
> t
0
        then do
          let v0 :: Word8
v0 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array Int
ixSrc :: Word8
              v1 :: Word8
v1 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) :: Word8
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst Int
ixDst Word8
v1
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Word8
v0
          Int -> Int -> t -> ST s ()
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) (t
len t -> t -> t
forall a. Num a => a -> a -> a
- t
2)
        else () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  Int -> Int -> Int -> ST s ()
forall t. (Ord t, Num t) => Int -> Int -> t -> ST s ()
go Int
offset Int
0 Int
length
  MutableByteArray (PrimState (ST s)) -> ST s ByteArray
forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m ByteArray
PM.unsafeFreezeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst

swapArray32 :: Bytes -> ByteArray
swapArray32 :: Bytes -> ByteArray
swapArray32 (Bytes{ByteArray
array :: ByteArray
$sel:array:Bytes :: Bytes -> ByteArray
array,Int
offset :: Int
$sel:offset:Bytes :: Bytes -> Int
offset,Int
length :: Int
$sel:length:Bytes :: Bytes -> Int
length}) = (forall s. ST s ByteArray) -> ByteArray
runByteArrayST ((forall s. ST s ByteArray) -> ByteArray)
-> (forall s. ST s ByteArray) -> ByteArray
forall a b. (a -> b) -> a -> b
$ do
  MutableByteArray s
dst <- Int -> ST s (MutableByteArray (PrimState (ST s)))
forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
PM.newByteArray Int
length
  let go :: Int -> Int -> t -> ST s ()
go !Int
ixSrc !Int
ixDst !t
len = if t
len t -> t -> Bool
forall a. Ord a => a -> a -> Bool
> t
0
        then do
          let v0 :: Word8
v0 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array Int
ixSrc :: Word8
              v1 :: Word8
v1 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) :: Word8
              v2 :: Word8
v2 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) :: Word8
              v3 :: Word8
v3 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3) :: Word8
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst Int
ixDst Word8
v3
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Word8
v2
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) Word8
v1
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3) Word8
v0
          Int -> Int -> t -> ST s ()
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4) (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4) (t
len t -> t -> t
forall a. Num a => a -> a -> a
- t
4)
        else () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  Int -> Int -> Int -> ST s ()
forall t. (Ord t, Num t) => Int -> Int -> t -> ST s ()
go Int
offset Int
0 Int
length
  MutableByteArray (PrimState (ST s)) -> ST s ByteArray
forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m ByteArray
PM.unsafeFreezeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst

swapArray64 :: Bytes -> ByteArray
swapArray64 :: Bytes -> ByteArray
swapArray64 (Bytes{ByteArray
array :: ByteArray
$sel:array:Bytes :: Bytes -> ByteArray
array,Int
offset :: Int
$sel:offset:Bytes :: Bytes -> Int
offset,Int
length :: Int
$sel:length:Bytes :: Bytes -> Int
length}) = (forall s. ST s ByteArray) -> ByteArray
runByteArrayST ((forall s. ST s ByteArray) -> ByteArray)
-> (forall s. ST s ByteArray) -> ByteArray
forall a b. (a -> b) -> a -> b
$ do
  MutableByteArray s
dst <- Int -> ST s (MutableByteArray (PrimState (ST s)))
forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
PM.newByteArray Int
length
  let go :: Int -> Int -> t -> ST s ()
go !Int
ixSrc !Int
ixDst !t
len = if t
len t -> t -> Bool
forall a. Ord a => a -> a -> Bool
> t
0
        then do
          let v0 :: Word8
v0 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array Int
ixSrc :: Word8
              v1 :: Word8
v1 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) :: Word8
              v2 :: Word8
v2 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) :: Word8
              v3 :: Word8
v3 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3) :: Word8
              v4 :: Word8
v4 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4) :: Word8
              v5 :: Word8
v5 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
5) :: Word8
              v6 :: Word8
v6 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
6) :: Word8
              v7 :: Word8
v7 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
7) :: Word8
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst Int
ixDst Word8
v7
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Word8
v6
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) Word8
v5
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3) Word8
v4
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4) Word8
v3
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
5) Word8
v2
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
6) Word8
v1
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
7) Word8
v0
          Int -> Int -> t -> ST s ()
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
8) (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
8) (t
len t -> t -> t
forall a. Num a => a -> a -> a
- t
8)
        else () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  Int -> Int -> Int -> ST s ()
forall t. (Ord t, Num t) => Int -> Int -> t -> ST s ()
go Int
offset Int
0 Int
length
  MutableByteArray (PrimState (ST s)) -> ST s ByteArray
forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m ByteArray
PM.unsafeFreezeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst

swapArray128 :: Bytes -> ByteArray
swapArray128 :: Bytes -> ByteArray
swapArray128 (Bytes{ByteArray
array :: ByteArray
$sel:array:Bytes :: Bytes -> ByteArray
array,Int
offset :: Int
$sel:offset:Bytes :: Bytes -> Int
offset,Int
length :: Int
$sel:length:Bytes :: Bytes -> Int
length}) = (forall s. ST s ByteArray) -> ByteArray
runByteArrayST ((forall s. ST s ByteArray) -> ByteArray)
-> (forall s. ST s ByteArray) -> ByteArray
forall a b. (a -> b) -> a -> b
$ do
  MutableByteArray s
dst <- Int -> ST s (MutableByteArray (PrimState (ST s)))
forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
PM.newByteArray Int
length
  let go :: Int -> Int -> t -> ST s ()
go !Int
ixSrc !Int
ixDst !t
len = if t
len t -> t -> Bool
forall a. Ord a => a -> a -> Bool
> t
0
        then do
          let v0 :: Word8
v0 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array Int
ixSrc :: Word8
              v1 :: Word8
v1 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) :: Word8
              v2 :: Word8
v2 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) :: Word8
              v3 :: Word8
v3 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3) :: Word8
              v4 :: Word8
v4 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4) :: Word8
              v5 :: Word8
v5 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
5) :: Word8
              v6 :: Word8
v6 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
6) :: Word8
              v7 :: Word8
v7 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
7) :: Word8
              v8 :: Word8
v8 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
8) :: Word8
              v9 :: Word8
v9 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
9) :: Word8
              v10 :: Word8
v10 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
10) :: Word8
              v11 :: Word8
v11 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
11) :: Word8
              v12 :: Word8
v12 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
12) :: Word8
              v13 :: Word8
v13 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
13) :: Word8
              v14 :: Word8
v14 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
14) :: Word8
              v15 :: Word8
v15 = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
15) :: Word8
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst Int
ixDst Word8
v15
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Word8
v14
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) Word8
v13
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3) Word8
v12
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4) Word8
v11
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
5) Word8
v10
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
6) Word8
v9
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
7) Word8
v8
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
8) Word8
v7
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
9) Word8
v6
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
10) Word8
v5
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
11) Word8
v4
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
12) Word8
v3
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
13) Word8
v2
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
14) Word8
v1
          MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
15) Word8
v0
          Int -> Int -> t -> ST s ()
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
16) (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
16) (t
len t -> t -> t
forall a. Num a => a -> a -> a
- t
16)
        else () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  Int -> Int -> Int -> ST s ()
forall t. (Ord t, Num t) => Int -> Int -> t -> ST s ()
go Int
offset Int
0 Int
length
  MutableByteArray (PrimState (ST s)) -> ST s ByteArray
forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m ByteArray
PM.unsafeFreezeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst

swapArray256 :: Bytes -> ByteArray
swapArray256 :: Bytes -> ByteArray
swapArray256 (Bytes{ByteArray
array :: ByteArray
$sel:array:Bytes :: Bytes -> ByteArray
array,Int
offset :: Int
$sel:offset:Bytes :: Bytes -> Int
offset,Int
length :: Int
$sel:length:Bytes :: Bytes -> Int
length}) = (forall s. ST s ByteArray) -> ByteArray
runByteArrayST ((forall s. ST s ByteArray) -> ByteArray)
-> (forall s. ST s ByteArray) -> ByteArray
forall a b. (a -> b) -> a -> b
$ do
  MutableByteArray s
dst <- Int -> ST s (MutableByteArray (PrimState (ST s)))
forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
PM.newByteArray Int
length
  let go :: Int -> Int -> t -> ST s ()
go !Int
ixSrc !Int
ixDst !t
len = if t
len t -> t -> Bool
forall a. Ord a => a -> a -> Bool
> t
0
        then do
          let loop :: Int -> ST s ()
loop !Int
i
                | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
32 = do
                    let v :: Word8
v = ByteArray -> Int -> Word8
forall a. Prim a => ByteArray -> Int -> a
PM.indexByteArray ByteArray
array (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
i) :: Word8
                    MutableByteArray (PrimState (ST s)) -> Int -> Word8 -> ST s ()
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
PM.writeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
31 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i)) Word8
v
                    Int -> ST s ()
loop (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
                | Bool
otherwise = () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
          Int -> ST s ()
loop Int
0
          Int -> Int -> t -> ST s ()
go (Int
ixSrc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
32) (Int
ixDst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
32) (t
len t -> t -> t
forall a. Num a => a -> a -> a
- t
32)
        else () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  Int -> Int -> Int -> ST s ()
forall t. (Ord t, Num t) => Int -> Int -> t -> ST s ()
go Int
offset Int
0 Int
length
  MutableByteArray (PrimState (ST s)) -> ST s ByteArray
forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m ByteArray
PM.unsafeFreezeByteArray MutableByteArray s
MutableByteArray (PrimState (ST s))
dst

pureParser :: a -> Parser e s a
{-# inline pureParser #-}
pureParser :: a -> Parser e s a
pureParser a
a = ((# ByteArray#, Int#, Int# #) -> ST# s (Result# e a))
-> Parser e s a
forall e s a.
((# ByteArray#, Int#, Int# #) -> ST# s (Result# e a))
-> Parser e s a
Parser
  (\(# ByteArray#
_, Int#
b, Int#
c #) State# s
s -> (# State# s
s, (# | (# a
a, Int#
b, Int#
c #) #) #))

bindParser :: Parser e s a -> (a -> Parser e s b) -> Parser e s b
{-# inline bindParser #-}
bindParser :: Parser e s a -> (a -> Parser e s b) -> Parser e s b
bindParser (Parser (# ByteArray#, Int#, Int# #) -> ST# s (Result# e a)
f) a -> Parser e s b
g = ((# ByteArray#, Int#, Int# #) -> ST# s (Result# e b))
-> Parser e s b
forall e s a.
((# ByteArray#, Int#, Int# #) -> ST# s (Result# e a))
-> Parser e s a
Parser
  (\x :: (# ByteArray#, Int#, Int# #)
x@(# ByteArray#
arr, Int#
_, Int#
_ #) State# s
s0 -> case (# ByteArray#, Int#, Int# #) -> ST# s (Result# e a)
f (# ByteArray#, Int#, Int# #)
x State# s
s0 of
    (# State# s
s1, Result# e a
r0 #) -> case Result# e a
r0 of
      (# e
e | #) -> (# State# s
s1, (# e
e | #) #)
      (# | (# a
y, Int#
b, Int#
c #) #) ->
        Parser e s b -> (# ByteArray#, Int#, Int# #) -> ST# s (Result# e b)
forall e s a.
Parser e s a -> (# ByteArray#, Int#, Int# #) -> ST# s (Result# e a)
runParser (a -> Parser e s b
g a
y) (# ByteArray#
arr, Int#
b, Int#
c #) State# s
s1
  )