{-# language BangPatterns #-}
{-# language BinaryLiterals #-}
{-# language DataKinds #-}
{-# language DeriveFunctor #-}
{-# language DerivingStrategies #-}
{-# language DuplicateRecordFields #-}
{-# 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.Unsafe
(
Parser(..)
, cursor
, cursor#
, expose
, unconsume
, jump
) where
import Prelude hiding (length)
import Data.Primitive (ByteArray)
import Data.Bytes.Types (Bytes(..))
import Data.Bytes.Parser.Internal (Parser(..),uneffectful,uneffectfulInt#)
import Data.Bytes.Parser.Internal (InternalResult(..))
import GHC.Exts (Int#,Int(I#))
cursor :: Parser e s Int
cursor = uneffectful $ \Bytes{offset,length} ->
InternalSuccess offset offset length
cursor# :: Parser e s Int#
cursor# = uneffectfulInt# $ \Bytes{offset=I# off,length=I# len} -> (# | (# off, off, len #) #)
expose :: Parser e s ByteArray
expose = uneffectful $ \Bytes{length,offset,array} ->
InternalSuccess array offset length
unconsume :: Int -> Parser e s ()
unconsume n = uneffectful $ \Bytes{length,offset} ->
InternalSuccess () (offset - n) (length + n)
jump :: Int -> Parser e s ()
jump ix = uneffectful $ \(Bytes{length,offset}) ->
InternalSuccess () ix (length + (offset - ix))