{-# LANGUAGE BangPatterns           #-}
{-# LANGUAGE CPP                    #-}
{-# LANGUAGE DeriveDataTypeable     #-}
{-# LANGUAGE FlexibleContexts       #-}
{-# LANGUAGE FlexibleInstances      #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MagicHash              #-}
{-# LANGUAGE MultiParamTypeClasses  #-}
{-# LANGUAGE OverloadedStrings      #-}
{-# LANGUAGE RankNTypes             #-}

-- Bump up from the default 1.5, otherwise our decoder fast path is no good.
-- We went over the threshold when we switched to using ST.
{-# OPTIONS_GHC -funfolding-keeness-factor=2.0 #-}

-- |
-- Module      : Codec.CBOR.Read
-- Copyright   : (c) Duncan Coutts 2015-2017
-- License     : BSD3-style (see LICENSE.txt)
--
-- Maintainer  : duncan@community.haskell.org
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)
--
-- Tools for reading values in a CBOR-encoded format
-- back into ordinary values.
--
module Codec.CBOR.Read
  ( deserialiseFromBytes         -- :: Decoder a -> ByteString -> Either String (ByteString, a)
  , deserialiseFromBytesWithSize -- :: Decoder a -> ByteString -> Either String (ByteString, ByteOffset, a)
  , deserialiseIncremental       -- :: Decoder a -> ST s (IDecode s a)
  , DeserialiseFailure(..)
  , IDecode(..)
  , ByteOffset
  ) where

#include "cbor.h"

#if !MIN_VERSION_base(4,8,0)
import           Control.Applicative
#endif
import           GHC.Int

import           Control.DeepSeq
import           Control.Monad (ap)
import           Control.Monad.ST
import           Data.Array.IArray
import           Data.Array.Unboxed
import qualified Data.Array.Base as A
import           Data.Monoid
import           Data.Bits
import           Data.ByteString                (ByteString)
import qualified Data.ByteString                as BS
import qualified Data.ByteString.Unsafe         as BS
import qualified Data.ByteString.Lazy           as LBS
import qualified Data.ByteString.Lazy.Internal  as LBS
import qualified Data.Text          as T
import qualified Data.Text.Encoding as T
import           Data.Word
import           GHC.Word
#if defined(ARCH_32bit)
import           GHC.IntWord64
#endif
import           GHC.Exts
import           GHC.Float (float2Double)
import           Data.Typeable
import           Control.Exception

-- We do all numeric conversions explicitly to be careful about overflows.
import           Prelude hiding (fromIntegral)

import qualified Codec.CBOR.ByteArray as BA
import           Codec.CBOR.Decoding hiding (DecodeAction(Done, Fail))
import           Codec.CBOR.Decoding (DecodeAction)
import qualified Codec.CBOR.Decoding as D
import           Codec.CBOR.Magic

--------------------------------------------------------------------------------

-- | An exception type that may be returned (by pure functions) or
-- thrown (by IO actions) that fail to deserialise a given input.
--
-- @since 0.2.0.0
data DeserialiseFailure = DeserialiseFailure ByteOffset String
  deriving (DeserialiseFailure -> DeserialiseFailure -> Bool
(DeserialiseFailure -> DeserialiseFailure -> Bool)
-> (DeserialiseFailure -> DeserialiseFailure -> Bool)
-> Eq DeserialiseFailure
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DeserialiseFailure -> DeserialiseFailure -> Bool
$c/= :: DeserialiseFailure -> DeserialiseFailure -> Bool
== :: DeserialiseFailure -> DeserialiseFailure -> Bool
$c== :: DeserialiseFailure -> DeserialiseFailure -> Bool
Eq, Int -> DeserialiseFailure -> ShowS
[DeserialiseFailure] -> ShowS
DeserialiseFailure -> String
(Int -> DeserialiseFailure -> ShowS)
-> (DeserialiseFailure -> String)
-> ([DeserialiseFailure] -> ShowS)
-> Show DeserialiseFailure
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DeserialiseFailure] -> ShowS
$cshowList :: [DeserialiseFailure] -> ShowS
show :: DeserialiseFailure -> String
$cshow :: DeserialiseFailure -> String
showsPrec :: Int -> DeserialiseFailure -> ShowS
$cshowsPrec :: Int -> DeserialiseFailure -> ShowS
Show, Typeable)

instance NFData DeserialiseFailure where
  rnf :: DeserialiseFailure -> ()
rnf (DeserialiseFailure offset :: ByteOffset
offset msg :: String
msg) = ByteOffset -> ()
forall a. NFData a => a -> ()
rnf ByteOffset
offset () -> () -> ()
forall a b. a -> b -> b
`seq` String -> ()
forall a. NFData a => a -> ()
rnf String
msg () -> () -> ()
forall a b. a -> b -> b
`seq` ()

instance Exception DeserialiseFailure where
#if MIN_VERSION_base(4,8,0)
    displayException :: DeserialiseFailure -> String
displayException (DeserialiseFailure off :: ByteOffset
off msg :: String
msg) =
      "Codec.CBOR: deserialising failed at offset "
           String -> ShowS
forall a. [a] -> [a] -> [a]
++ ByteOffset -> String
forall a. Show a => a -> String
show ByteOffset
off String -> ShowS
forall a. [a] -> [a] -> [a]
++ " : " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
msg
#endif

-- | An Incremental decoder, used to represent the result of
-- attempting to run a decoder over a given input, and return a value
-- of type @a@.
data IDecode s a
  = -- | The decoder has consumed the available input and needs more
    -- to continue. Provide 'Just' if more input is available and
    -- 'Nothing' otherwise, and you will get a new 'IDecode'.
    Partial (Maybe BS.ByteString -> ST s (IDecode s a))

    -- | The decoder has successfully finished. Except for the output
    -- value you also get any unused input as well as the number of
    -- bytes consumed.
  | Done !BS.ByteString {-# UNPACK #-} !ByteOffset a

    -- | The decoder ran into an error. The decoder either used
    -- 'fail' or was not provided enough input. Contains any
    -- unconsumed input, the number of bytes consumed, and a
    -- 'DeserialiseFailure' exception describing the reason why the
    -- failure occurred.
  | Fail !BS.ByteString {-# UNPACK #-} !ByteOffset DeserialiseFailure

-- | Given a 'Decoder' and some 'LBS.ByteString' representing
-- an encoded CBOR value, return 'Either' the decoded CBOR value
-- or an error. In addition to the decoded value return any remaining input
-- content.
--
-- @since 0.2.0.0
deserialiseFromBytes :: (forall s. Decoder s a)
                     -> LBS.ByteString
                     -> Either DeserialiseFailure (LBS.ByteString, a)
deserialiseFromBytes :: (forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
deserialiseFromBytes d :: forall s. Decoder s a
d lbs :: ByteString
lbs =
    ((ByteString, ByteOffset, a) -> (ByteString, a))
-> Either DeserialiseFailure (ByteString, ByteOffset, a)
-> Either DeserialiseFailure (ByteString, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ByteString, ByteOffset, a) -> (ByteString, a)
forall a b b. (a, b, b) -> (a, b)
f (Either DeserialiseFailure (ByteString, ByteOffset, a)
 -> Either DeserialiseFailure (ByteString, a))
-> Either DeserialiseFailure (ByteString, ByteOffset, a)
-> Either DeserialiseFailure (ByteString, a)
forall a b. (a -> b) -> a -> b
$ (forall s. ST s (IDecode s a))
-> ByteString
-> Either DeserialiseFailure (ByteString, ByteOffset, a)
forall a.
(forall s. ST s (IDecode s a))
-> ByteString
-> Either DeserialiseFailure (ByteString, ByteOffset, a)
runIDecode (Decoder s a -> ST s (IDecode s a)
forall s a. Decoder s a -> ST s (IDecode s a)
deserialiseIncremental Decoder s a
forall s. Decoder s a
d) ByteString
lbs
  where f :: (a, b, b) -> (a, b)
f (rest :: a
rest, _, x :: b
x) = (a
rest, b
x)

-- | Given a 'Decoder' and some 'LBS.ByteString' representing
-- an encoded CBOR value, return 'Either' the decoded CBOR value
-- or an error. In addition to the decoded value return any remaining input
-- content and the number of bytes consumed.
--
-- @since 0.2.0.0
deserialiseFromBytesWithSize :: (forall s. Decoder s a)
                             -> LBS.ByteString
                             -> Either DeserialiseFailure (LBS.ByteString, ByteOffset, a)
deserialiseFromBytesWithSize :: (forall s. Decoder s a)
-> ByteString
-> Either DeserialiseFailure (ByteString, ByteOffset, a)
deserialiseFromBytesWithSize d :: forall s. Decoder s a
d lbs :: ByteString
lbs =
    (forall s. ST s (IDecode s a))
-> ByteString
-> Either DeserialiseFailure (ByteString, ByteOffset, a)
forall a.
(forall s. ST s (IDecode s a))
-> ByteString
-> Either DeserialiseFailure (ByteString, ByteOffset, a)
runIDecode (Decoder s a -> ST s (IDecode s a)
forall s a. Decoder s a -> ST s (IDecode s a)
deserialiseIncremental Decoder s a
forall s. Decoder s a
d) ByteString
lbs

runIDecode :: (forall s. ST s (IDecode s a))
           -> LBS.ByteString
           -> Either DeserialiseFailure (LBS.ByteString, ByteOffset, a)
runIDecode :: (forall s. ST s (IDecode s a))
-> ByteString
-> Either DeserialiseFailure (ByteString, ByteOffset, a)
runIDecode d :: forall s. ST s (IDecode s a)
d lbs :: ByteString
lbs =
    (forall s.
 ST s (Either DeserialiseFailure (ByteString, ByteOffset, a)))
-> Either DeserialiseFailure (ByteString, ByteOffset, a)
forall a. (forall s. ST s a) -> a
runST (ByteString
-> IDecode s a
-> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a))
forall s a.
ByteString
-> IDecode s a
-> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a))
go ByteString
lbs (IDecode s a
 -> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a)))
-> ST s (IDecode s a)
-> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ST s (IDecode s a)
forall s. ST s (IDecode s a)
d)
  where
    go :: LBS.ByteString
       -> IDecode s a
       -> ST s (Either DeserialiseFailure (LBS.ByteString, ByteOffset, a))
    go :: ByteString
-> IDecode s a
-> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a))
go  _                  (Fail _ _ err :: DeserialiseFailure
err)  = Either DeserialiseFailure (ByteString, ByteOffset, a)
-> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a))
forall (m :: * -> *) a. Monad m => a -> m a
return (DeserialiseFailure
-> Either DeserialiseFailure (ByteString, ByteOffset, a)
forall a b. a -> Either a b
Left DeserialiseFailure
err)
    go  lbs' :: ByteString
lbs'               (Done bs :: ByteString
bs off :: ByteOffset
off x :: a
x) = let rest :: ByteString
rest
                                                   | ByteString -> Bool
BS.null ByteString
bs = ByteString
lbs'
                                                   | Bool
otherwise  = ByteString -> ByteString -> ByteString
LBS.Chunk ByteString
bs ByteString
lbs'
                                             in Either DeserialiseFailure (ByteString, ByteOffset, a)
-> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a))
forall (m :: * -> *) a. Monad m => a -> m a
return ((ByteString, ByteOffset, a)
-> Either DeserialiseFailure (ByteString, ByteOffset, a)
forall a b. b -> Either a b
Right (ByteString
rest, ByteOffset
off, a
x))
    go  LBS.Empty          (Partial  k :: Maybe ByteString -> ST s (IDecode s a)
k)    = Maybe ByteString -> ST s (IDecode s a)
k Maybe ByteString
forall a. Maybe a
Nothing   ST s (IDecode s a)
-> (IDecode s a
    -> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a)))
-> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString
-> IDecode s a
-> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a))
forall s a.
ByteString
-> IDecode s a
-> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a))
go ByteString
LBS.Empty
    go (LBS.Chunk bs :: ByteString
bs lbs' :: ByteString
lbs') (Partial  k :: Maybe ByteString -> ST s (IDecode s a)
k)    = Maybe ByteString -> ST s (IDecode s a)
k (ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
bs) ST s (IDecode s a)
-> (IDecode s a
    -> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a)))
-> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString
-> IDecode s a
-> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a))
forall s a.
ByteString
-> IDecode s a
-> ST s (Either DeserialiseFailure (ByteString, ByteOffset, a))
go ByteString
lbs'

-- | Run a 'Decoder' incrementally, returning a continuation
-- representing the result of the incremental decode.
--
-- @since 0.2.0.0
deserialiseIncremental :: Decoder s a -> ST s (IDecode s a)
deserialiseIncremental :: Decoder s a -> ST s (IDecode s a)
deserialiseIncremental decoder :: Decoder s a
decoder = do
    DecodeAction s a
da <- Decoder s a -> ST s (DecodeAction s a)
forall s a. Decoder s a -> ST s (DecodeAction s a)
getDecodeAction Decoder s a
decoder
    IncrementalDecoder s (ByteString, ByteOffset, a)
-> ST s (IDecode s a)
forall s a.
IncrementalDecoder s (ByteString, ByteOffset, a)
-> ST s (IDecode s a)
runIncrementalDecoder (DecodeAction s a
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> IncrementalDecoder s (ByteString, ByteOffset, a)
runDecodeAction DecodeAction s a
da)

----------------------------------------------
-- A monad for building incremental decoders
--

newtype IncrementalDecoder s a = IncrementalDecoder {
       IncrementalDecoder s a
-> forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r)
unIncrementalDecoder ::
         forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r)
     }

instance Functor (IncrementalDecoder s) where
    fmap :: (a -> b) -> IncrementalDecoder s a -> IncrementalDecoder s b
fmap f :: a -> b
f a :: IncrementalDecoder s a
a = IncrementalDecoder s a
a IncrementalDecoder s a
-> (a -> IncrementalDecoder s b) -> IncrementalDecoder s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> IncrementalDecoder s b
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> IncrementalDecoder s b)
-> (a -> b) -> a -> IncrementalDecoder s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f

instance Applicative (IncrementalDecoder s) where
    pure :: a -> IncrementalDecoder s a
pure x :: a
x = (forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s a
forall s a.
(forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s a
IncrementalDecoder ((forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r))
 -> IncrementalDecoder s a)
-> (forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s a
forall a b. (a -> b) -> a -> b
$ \k :: a -> ST s (IDecode s r)
k -> a -> ST s (IDecode s r)
k a
x
    <*> :: IncrementalDecoder s (a -> b)
-> IncrementalDecoder s a -> IncrementalDecoder s b
(<*>) = IncrementalDecoder s (a -> b)
-> IncrementalDecoder s a -> IncrementalDecoder s b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Monad (IncrementalDecoder s) where
    return :: a -> IncrementalDecoder s a
return = a -> IncrementalDecoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

    {-# INLINE (>>=) #-}
    m :: IncrementalDecoder s a
m >>= :: IncrementalDecoder s a
-> (a -> IncrementalDecoder s b) -> IncrementalDecoder s b
>>= f :: a -> IncrementalDecoder s b
f = (forall r. (b -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s b
forall s a.
(forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s a
IncrementalDecoder ((forall r. (b -> ST s (IDecode s r)) -> ST s (IDecode s r))
 -> IncrementalDecoder s b)
-> (forall r. (b -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s b
forall a b. (a -> b) -> a -> b
$ \k :: b -> ST s (IDecode s r)
k ->
                IncrementalDecoder s a
-> forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r)
forall s a.
IncrementalDecoder s a
-> forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r)
unIncrementalDecoder IncrementalDecoder s a
m ((a -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> (a -> ST s (IDecode s r)) -> ST s (IDecode s r)
forall a b. (a -> b) -> a -> b
$ \x :: a
x ->
                  IncrementalDecoder s b
-> (b -> ST s (IDecode s r)) -> ST s (IDecode s r)
forall s a.
IncrementalDecoder s a
-> forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r)
unIncrementalDecoder (a -> IncrementalDecoder s b
f a
x) b -> ST s (IDecode s r)
k

runIncrementalDecoder :: IncrementalDecoder s (ByteString, ByteOffset, a)
                      -> ST s (IDecode s a)
runIncrementalDecoder :: IncrementalDecoder s (ByteString, ByteOffset, a)
-> ST s (IDecode s a)
runIncrementalDecoder (IncrementalDecoder f :: forall r.
((ByteString, ByteOffset, a) -> ST s (IDecode s r))
-> ST s (IDecode s r)
f) =
  ((ByteString, ByteOffset, a) -> ST s (IDecode s a))
-> ST s (IDecode s a)
forall r.
((ByteString, ByteOffset, a) -> ST s (IDecode s r))
-> ST s (IDecode s r)
f (\(trailing :: ByteString
trailing, off :: ByteOffset
off, x :: a
x) -> IDecode s a -> ST s (IDecode s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (IDecode s a -> ST s (IDecode s a))
-> IDecode s a -> ST s (IDecode s a)
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteOffset -> a -> IDecode s a
forall s a. ByteString -> ByteOffset -> a -> IDecode s a
Done ByteString
trailing ByteOffset
off a
x)

decodeFail :: ByteString -> ByteOffset -> String -> IncrementalDecoder s a
decodeFail :: ByteString -> ByteOffset -> String -> IncrementalDecoder s a
decodeFail trailing :: ByteString
trailing off :: ByteOffset
off msg :: String
msg = (forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s a
forall s a.
(forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s a
IncrementalDecoder ((forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r))
 -> IncrementalDecoder s a)
-> (forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s a
forall a b. (a -> b) -> a -> b
$ \_ -> IDecode s r -> ST s (IDecode s r)
forall (m :: * -> *) a. Monad m => a -> m a
return (IDecode s r -> ST s (IDecode s r))
-> IDecode s r -> ST s (IDecode s r)
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteOffset -> DeserialiseFailure -> IDecode s r
forall s a.
ByteString -> ByteOffset -> DeserialiseFailure -> IDecode s a
Fail ByteString
trailing ByteOffset
off DeserialiseFailure
exn
  where exn :: DeserialiseFailure
exn = ByteOffset -> String -> DeserialiseFailure
DeserialiseFailure ByteOffset
off String
msg

needChunk :: IncrementalDecoder s (Maybe ByteString)
needChunk :: IncrementalDecoder s (Maybe ByteString)
needChunk = (forall r.
 (Maybe ByteString -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s (Maybe ByteString)
forall s a.
(forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s a
IncrementalDecoder ((forall r.
  (Maybe ByteString -> ST s (IDecode s r)) -> ST s (IDecode s r))
 -> IncrementalDecoder s (Maybe ByteString))
-> (forall r.
    (Maybe ByteString -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s (Maybe ByteString)
forall a b. (a -> b) -> a -> b
$ \k :: Maybe ByteString -> ST s (IDecode s r)
k -> IDecode s r -> ST s (IDecode s r)
forall (m :: * -> *) a. Monad m => a -> m a
return (IDecode s r -> ST s (IDecode s r))
-> IDecode s r -> ST s (IDecode s r)
forall a b. (a -> b) -> a -> b
$ (Maybe ByteString -> ST s (IDecode s r)) -> IDecode s r
forall s a. (Maybe ByteString -> ST s (IDecode s a)) -> IDecode s a
Partial ((Maybe ByteString -> ST s (IDecode s r)) -> IDecode s r)
-> (Maybe ByteString -> ST s (IDecode s r)) -> IDecode s r
forall a b. (a -> b) -> a -> b
$ \mbs :: Maybe ByteString
mbs -> Maybe ByteString -> ST s (IDecode s r)
k Maybe ByteString
mbs

lift :: ST s a -> IncrementalDecoder s a
lift :: ST s a -> IncrementalDecoder s a
lift action :: ST s a
action = (forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s a
forall s a.
(forall r. (a -> ST s (IDecode s r)) -> ST s (IDecode s r))
-> IncrementalDecoder s a
IncrementalDecoder (\k :: a -> ST s (IDecode s r)
k -> ST s a
action ST s a -> (a -> ST s (IDecode s r)) -> ST s (IDecode s r)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> ST s (IDecode s r)
k)

--------------------------------------------
-- The main decoder
--

-- The top level entry point
runDecodeAction :: DecodeAction s a
                -> IncrementalDecoder s (ByteString, ByteOffset, a)
runDecodeAction :: DecodeAction s a
-> IncrementalDecoder s (ByteString, ByteOffset, a)
runDecodeAction (D.Fail msg :: String
msg)        = ByteString
-> ByteOffset
-> String
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
ByteString -> ByteOffset -> String -> IncrementalDecoder s a
decodeFail ByteString
BS.empty 0 String
msg
runDecodeAction (D.Done x :: a
x)          = (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
BS.empty, 0, a
x)
runDecodeAction (D.PeekAvailable k :: Int# -> ST s (DecodeAction s a)
k) = ST s (DecodeAction s a) -> IncrementalDecoder s (DecodeAction s a)
forall s a. ST s a -> IncrementalDecoder s a
lift (Int# -> ST s (DecodeAction s a)
k 0#) IncrementalDecoder s (DecodeAction s a)
-> (DecodeAction s a
    -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= DecodeAction s a
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> IncrementalDecoder s (ByteString, ByteOffset, a)
runDecodeAction
runDecodeAction da :: DecodeAction s a
da = do
    Maybe ByteString
mbs <- IncrementalDecoder s (Maybe ByteString)
forall s. IncrementalDecoder s (Maybe ByteString)
needChunk
    case Maybe ByteString
mbs of
      Nothing -> ByteString
-> ByteOffset
-> String
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
ByteString -> ByteOffset -> String -> IncrementalDecoder s a
decodeFail ByteString
BS.empty 0 "end of input"
      Just bs :: ByteString
bs -> DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow DecodeAction s a
da ByteString
bs 0

-- The decoder is split into a fast path and a slow path. The fast path is
-- used for a single input chunk. It decodes as far as it can, reading only
-- whole tokens that fit within the input chunk. When it cannot read any
-- further it returns control to the slow path. The slow path fixes up all the
-- complicated corner cases with tokens that span chunk boundaries, gets more
-- input and then goes back into the fast path.
--
-- The idea is that chunks are usually large, and we can use simpler and
-- faster code if we don't make it deal with the general case of tokens that
-- span chunk boundaries.

-- These are all the ways in which the fast path can finish, and return
-- control to the slow path. In particular there are three different cases
-- of tokens spanning a chunk boundary.
--
data SlowPath s a
   = FastDone                      {-# UNPACK #-} !ByteString a
   | SlowConsumeTokenBytes         {-# UNPACK #-} !ByteString (ByteString   -> ST s (DecodeAction s a)) {-# UNPACK #-} !Int
   | SlowConsumeTokenByteArray     {-# UNPACK #-} !ByteString (BA.ByteArray -> ST s (DecodeAction s a)) {-# UNPACK #-} !Int
   | SlowConsumeTokenString        {-# UNPACK #-} !ByteString (T.Text       -> ST s (DecodeAction s a)) {-# UNPACK #-} !Int
   | SlowConsumeTokenUtf8ByteArray {-# UNPACK #-} !ByteString (BA.ByteArray -> ST s (DecodeAction s a)) {-# UNPACK #-} !Int
#if defined(ARCH_32bit)
   | SlowPeekByteOffset            {-# UNPACK #-} !ByteString (Int64#       -> ST s (DecodeAction s a))
#else
   | SlowPeekByteOffset            {-# UNPACK #-} !ByteString (Int#         -> ST s (DecodeAction s a))
#endif
   | SlowDecodeAction              {-# UNPACK #-} !ByteString (DecodeAction s a)
   | SlowFail                      {-# UNPACK #-} !ByteString String


-- The main fast path. The fast path itself is actually split into two parts
-- the main version 'go_fast' and a version used when we are near the end of
-- the chunk, 'go_fast_end'.
--
-- This version can then do fewer tests when we're not near the end of the
-- chunk, in particular we just check if there's enough input buffer space
-- left for the largest possible fixed-size token (8+1 bytes).
--
go_fast :: ByteString -> DecodeAction s a -> ST s (SlowPath s a)

go_fast :: ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast !ByteString
bs da :: DecodeAction s a
da | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 9 = ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeWord k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeWord8 k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) ->
        case Word# -> Word# -> Int#
gtWord# Word#
w# 0xff## of
          0#                  -> Word# -> ST s (DecodeAction s a)
k Word#
w#  ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                   -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeWord16 k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) ->
        case Word# -> Word# -> Int#
gtWord# Word#
w# 0xffff## of
          0#                  -> Word# -> ST s (DecodeAction s a)
k Word#
w#  ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                   -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeWord32 k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) ->
#if defined(ARCH_32bit)
                                 k w# >>= go_fast (BS.unsafeDrop sz bs)
#else
        case Word# -> Word# -> Int#
gtWord# Word#
w# 0xffffffff## of
          0#                  -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                   -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
#endif

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeNegWord k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeNegWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeInt k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeInt8 k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) ->
        case (Int#
n# Int# -> Int# -> Int#
># 0x7f#) Int# -> Int# -> Int#
`orI#` (Int#
n# Int# -> Int# -> Int#
<# -0x80#) of
          0#                  -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                   -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeInt16 k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) ->
        case (Int#
n# Int# -> Int# -> Int#
># 0x7fff#) Int# -> Int# -> Int#
`orI#` (Int#
n# Int# -> Int# -> Int#
<# -0x8000#) of
          0#                  -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                   -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeInt32 k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) ->
#if defined(ARCH_32bit)
                                 k n# >>= go_fast (BS.unsafeDrop sz bs)
#else
        case (Int#
n# Int# -> Int# -> Int#
># 0x7fffffff#) Int# -> Int# -> Int#
`orI#` (Int#
n# Int# -> Int# -> Int#
<# -0x80000000#) of
          0#                  -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                   -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
#endif

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeListLen k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeListLen (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeMapLen k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeMapLen (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeTag k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeTag (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeWordCanonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#)
        | Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise             -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeWord8Canonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) ->
        case Word# -> Word# -> Int#
gtWord# Word#
w# 0xff## of
          0# | Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                          -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeWord16Canonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) ->
        case Word# -> Word# -> Int#
gtWord# Word#
w# 0xffff## of
          0# | Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                          -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeWord32Canonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) ->
        case Word# -> Int#
w_out_of_range Word#
w# of
          0# | Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                          -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
  where
    w_out_of_range :: Word# -> Int#
    w_out_of_range :: Word# -> Int#
w_out_of_range _w# :: Word#
_w# =
#if defined(ARCH_32bit)
      0#
#else
      Word# -> Word# -> Int#
gtWord# Word#
_w# 0xffffffff##
#endif

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeNegWordCanonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeNegWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#)
        | Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise             -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeIntCanonical k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#)
        | Int -> Int# -> Bool
isIntCanonical Int
sz Int#
n# -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise            -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeInt8Canonical k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) ->
        case (Int#
n# Int# -> Int# -> Int#
># 0x7f#) Int# -> Int# -> Int#
`orI#` (Int#
n# Int# -> Int# -> Int#
<# -0x80#) of
          0# | Int -> Int# -> Bool
isIntCanonical Int
sz Int#
n# -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                         -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeInt16Canonical k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) ->
        case (Int#
n# Int# -> Int# -> Int#
># 0x7fff#) Int# -> Int# -> Int#
`orI#` (Int#
n# Int# -> Int# -> Int#
<# -0x8000#) of
          0# | Int -> Int# -> Bool
isIntCanonical Int
sz Int#
n# -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                         -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeInt32Canonical k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) ->
        case Int# -> Int#
n_out_of_range Int#
n# of
          0# | Int -> Int# -> Bool
isIntCanonical Int
sz Int#
n# -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                         -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
  where
    n_out_of_range :: Int# -> Int#
    n_out_of_range :: Int# -> Int#
n_out_of_range _n# :: Int#
_n# =
#if defined(ARCH_32bit)
      0#
#else
      (Int#
_n# Int# -> Int# -> Int#
># 0x7fffffff#) Int# -> Int# -> Int#
`orI#` (Int#
_n# Int# -> Int# -> Int#
<# -0x80000000#)
#endif

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeListLenCanonical k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeListLen (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#)
          -- List length can't be negative, cast it to Word#.
        | Int -> Word# -> Bool
isWordCanonical Int
sz (Int# -> Word#
int2Word# Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise                         -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeMapLenCanonical k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeMapLen (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#)
          -- Map length can't be negative, cast it to Word#.
        | Int -> Word# -> Bool
isWordCanonical Int
sz (Int# -> Word#
int2Word# Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise                         -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeTagCanonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeTag (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#)
        | Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise             -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

#if defined(ARCH_32bit)
go_fast !bs da@(ConsumeWord64 k) =
  case tryConsumeWord64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> go_fast_end bs da
    DecodedToken sz (W64# w#) -> k w# >>= go_fast (BS.unsafeDrop sz bs)

go_fast !bs da@(ConsumeNegWord64 k) =
  case tryConsumeNegWord64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> go_fast_end bs da
    DecodedToken sz (W64# w#) -> k w# >>= go_fast (BS.unsafeDrop sz bs)

go_fast !bs da@(ConsumeInt64 k) =
  case tryConsumeInt64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> go_fast_end bs da
    DecodedToken sz (I64# i#) -> k i# >>= go_fast (BS.unsafeDrop sz bs)

go_fast !bs da@(ConsumeListLen64 k) =
  case tryConsumeListLen64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> go_fast_end bs da
    DecodedToken sz (I64# i#) -> k i# >>= go_fast (BS.unsafeDrop sz bs)

go_fast !bs da@(ConsumeMapLen64 k) =
  case tryConsumeMapLen64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> go_fast_end bs da
    DecodedToken sz (I64# i#) -> k i# >>= go_fast (BS.unsafeDrop sz bs)

go_fast !bs da@(ConsumeTag64 k) =
  case tryConsumeTag64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> go_fast_end bs da
    DecodedToken sz (W64# w#) -> k w# >>= go_fast (BS.unsafeDrop sz bs)

go_fast !bs da@(ConsumeWord64Canonical k) =
  case tryConsumeWord64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> go_fast_end bs da
    DecodedToken sz (W64# w#)
      | isWord64Canonical sz w# -> k w# >>= go_fast (BS.unsafeDrop sz bs)
      | otherwise               -> go_fast_end bs da

go_fast !bs da@(ConsumeNegWord64Canonical k) =
  case tryConsumeNegWord64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> go_fast_end bs da
    DecodedToken sz (W64# w#)
      | isWord64Canonical sz w# -> k w# >>= go_fast (BS.unsafeDrop sz bs)
      | otherwise               -> go_fast_end bs da

go_fast !bs da@(ConsumeInt64Canonical k) =
  case tryConsumeInt64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> go_fast_end bs da
    DecodedToken sz (I64# i#)
      | isInt64Canonical sz i# -> k i# >>= go_fast (BS.unsafeDrop sz bs)
      | otherwise              -> go_fast_end bs da

go_fast !bs da@(ConsumeListLen64Canonical k) =
  case tryConsumeListLen64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> go_fast_end bs da
    DecodedToken sz (I64# i#)
        -- List length can't be negative, cast it to Word64#.
      | isWord64Canonical sz (int64ToWord64# i#) -> k i# >>= go_fast (BS.unsafeDrop sz bs)
      | otherwise                                -> go_fast_end bs da

go_fast !bs da@(ConsumeMapLen64Canonical k) =
  case tryConsumeMapLen64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> go_fast_end bs da
    DecodedToken sz (I64# i#)
        -- Map length can't be negative, cast it to Word64#.
      | isWord64Canonical sz (int64ToWord64# i#) -> k i# >>= go_fast (BS.unsafeDrop sz bs)
      | otherwise                                -> go_fast_end bs da

go_fast !bs da@(ConsumeTag64Canonical k) =
  case tryConsumeTag64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> go_fast_end bs da
    DecodedToken sz (W64# w#)
      | isWord64Canonical sz w# -> k w# >>= go_fast (BS.unsafeDrop sz bs)
      | otherwise               -> go_fast_end bs da
#endif

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeInteger k :: Integer -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (BigIntToken Integer)
tryConsumeInteger (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodedToken sz :: Int
sz (BigIntToken _ n :: Integer
n) -> Integer -> ST s (DecodeAction s a)
k Integer
n ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
      _                                 -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeFloat k :: Float# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Float
tryConsumeFloat (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure     -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (F# f# :: Float#
f#) -> Float# -> ST s (DecodeAction s a)
k Float#
f# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeDouble k :: Double# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Double
tryConsumeDouble (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure     -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (D# f# :: Double#
f#) -> Double# -> ST s (DecodeAction s a)
k Double#
f# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeBytes k :: ByteString -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeBytes (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure                   -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (Fits _ bstr :: ByteString
bstr)   -> ByteString -> ST s (DecodeAction s a)
k ByteString
bstr ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
      DecodedToken sz :: Int
sz (TooLong _ len :: Int
len) -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenBytes
                                                   (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ByteString -> ST s (DecodeAction s a)
k Int
len

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeByteArray k :: ByteArray -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeBytes (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure                 -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (Fits _ str :: ByteString
str)  -> ByteArray -> ST s (DecodeAction s a)
k (ByteString -> ByteArray
BA.fromByteString ByteString
str) ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
      DecodedToken sz :: Int
sz (TooLong _ len :: Int
len) -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenByteArray
                                                   (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ByteArray -> ST s (DecodeAction s a)
k Int
len

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeString k :: Text -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeString (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure                   -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (Fits _ str :: ByteString
str)    -> case ByteString -> Either UnicodeException Text
T.decodeUtf8' ByteString
str of
        Right t :: Text
t -> Text -> ST s (DecodeAction s a)
k Text
t ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        Left _e :: UnicodeException
_e -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "invalid UTF8"
      DecodedToken sz :: Int
sz (TooLong _ len :: Int
len) -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (Text -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (Text -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenString
                                                   (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) Text -> ST s (DecodeAction s a)
k Int
len

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeUtf8ByteArray k :: ByteArray -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeString (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure                   -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (Fits _ str :: ByteString
str)    -> ByteArray -> ST s (DecodeAction s a)
k (ByteString -> ByteArray
BA.fromByteString ByteString
str)
                                         ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
      DecodedToken sz :: Int
sz (TooLong _ len :: Int
len) -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenUtf8ByteArray
                                                   (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ByteArray -> ST s (DecodeAction s a)
k Int
len

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeBool k :: Bool -> ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken Bool
tryConsumeBool (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz b :: Bool
b -> Bool -> ST s (DecodeAction s a)
k Bool
b ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeSimple k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeSimple (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeIntegerCanonical k :: Integer -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (BigIntToken Integer)
tryConsumeInteger (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodedToken sz :: Int
sz (BigIntToken True n :: Integer
n) -> Integer -> ST s (DecodeAction s a)
k Integer
n ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
      _                                    -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da


go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeFloat16Canonical k :: Float# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Float
tryConsumeFloat (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure     -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz f :: Float
f@(F# f# :: Float#
f#)
        | Int -> ByteString -> Float -> Bool
isFloat16Canonical Int
sz ByteString
bs Float
f -> Float# -> ST s (DecodeAction s a)
k Float#
f# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise                  -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeFloatCanonical k :: Float# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Float
tryConsumeFloat (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure     -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz f :: Float
f@(F# f# :: Float#
f#)
        | Int -> ByteString -> Float -> Bool
isFloatCanonical Int
sz ByteString
bs Float
f -> Float# -> ST s (DecodeAction s a)
k Float#
f# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise                -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeDoubleCanonical k :: Double# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Double
tryConsumeDouble (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure     -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz f :: Double
f@(D# f# :: Double#
f#)
        | Int -> ByteString -> Double -> Bool
isDoubleCanonical Int
sz ByteString
bs Double
f -> Double# -> ST s (DecodeAction s a)
k Double#
f# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise                 -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeBytesCanonical k :: ByteString -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeBytes (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodedToken sz :: Int
sz (Fits    True bstr :: ByteString
bstr) -> ByteString -> ST s (DecodeAction s a)
k ByteString
bstr ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
      DecodedToken sz :: Int
sz (TooLong True len :: Int
len)  ->
        SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenBytes (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ByteString -> ST s (DecodeAction s a)
k Int
len
      _                                   -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeByteArrayCanonical k :: ByteArray -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeBytes (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodedToken sz :: Int
sz (Fits True str :: ByteString
str)    ->
        ByteArray -> ST s (DecodeAction s a)
k (ByteString -> ByteArray
BA.fromByteString ByteString
str) ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
      DecodedToken sz :: Int
sz (TooLong True len :: Int
len) ->
        SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenByteArray (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ByteArray -> ST s (DecodeAction s a)
k Int
len
      _                                  -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeStringCanonical k :: Text -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeString (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodedToken sz :: Int
sz (Fits True str :: ByteString
str)    -> case ByteString -> Either UnicodeException Text
T.decodeUtf8' ByteString
str of
        Right t :: Text
t -> Text -> ST s (DecodeAction s a)
k Text
t ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        Left _e :: UnicodeException
_e -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "invalid UTF8"
      DecodedToken sz :: Int
sz (TooLong True len :: Int
len) ->
        SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (Text -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (Text -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenString (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) Text -> ST s (DecodeAction s a)
k Int
len
      _                                  -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeUtf8ByteArrayCanonical k :: ByteArray -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeString (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodedToken sz :: Int
sz (Fits True str :: ByteString
str)    ->
        ByteArray -> ST s (DecodeAction s a)
k (ByteString -> ByteArray
BA.fromByteString ByteString
str) ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
      DecodedToken sz :: Int
sz (TooLong True len :: Int
len) ->
        SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenUtf8ByteArray (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ByteArray -> ST s (DecodeAction s a)
k Int
len
      _                                  -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeSimpleCanonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeSimple (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#)
        | Int -> Word# -> Bool
isSimpleCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise               -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeBytesIndef k :: ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken ()
tryConsumeBytesIndef (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz _ -> ST s (DecodeAction s a)
k ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeStringIndef k :: ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken ()
tryConsumeStringIndef (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz _ -> ST s (DecodeAction s a)
k ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeListLenIndef k :: ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken ()
tryConsumeListLenIndef (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz _ -> ST s (DecodeAction s a)
k ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeMapLenIndef k :: ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken ()
tryConsumeMapLenIndef (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz _ -> ST s (DecodeAction s a)
k ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeNull k :: ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken ()
tryConsumeNull (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz _ -> ST s (DecodeAction s a)
k ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeListLenOrIndef k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeListLenOrIndef (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs da :: DecodeAction s a
da@(ConsumeMapLenOrIndef k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeMapLenOrIndef (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs (ConsumeBreakOr k :: Bool -> ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken ()
tryConsumeBreakOr (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> Bool -> ST s (DecodeAction s a)
k Bool
False ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast ByteString
bs
      DecodedToken sz :: Int
sz _ -> Bool -> ST s (DecodeAction s a)
k Bool
True ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast !ByteString
bs (PeekTokenType k :: TokenType -> ST s (DecodeAction s a)
k) =
    let !hdr :: Word8
hdr  = ByteString -> Word8
BS.unsafeHead ByteString
bs
        !tkty :: TokenType
tkty = Array Word8 TokenType
decodeTokenTypeTable Array Word8 TokenType -> Int -> TokenType
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> Int -> e
`A.unsafeAt` Word8 -> Int
word8ToInt Word8
hdr
    in TokenType -> ST s (DecodeAction s a)
k TokenType
tkty ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast ByteString
bs

go_fast !ByteString
bs (PeekAvailable k :: Int# -> ST s (DecodeAction s a)
k) = Int# -> ST s (DecodeAction s a)
k (case ByteString -> Int
BS.length ByteString
bs of I# len# :: Int#
len# -> Int#
len#) ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast ByteString
bs

go_fast !ByteString
bs da :: DecodeAction s a
da@PeekByteOffset{} = ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
go_fast !ByteString
bs da :: DecodeAction s a
da@D.Fail{} = ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da
go_fast !ByteString
bs da :: DecodeAction s a
da@D.Done{} = ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs DecodeAction s a
da


-- This variant of the fast path has to do a few more checks because we're
-- near the end of the chunk. The guarantee we provide here is that we will
-- decode any tokens where the whole token fits within the input buffer. So
-- if we return with input buffer space still unconsumed (and we're not done
-- or failed) then there's one remaining token that spans the end of the
-- input chunk (the slow path fixup code relies on this guarantee).
--
go_fast_end :: ByteString -> DecodeAction s a -> ST s (SlowPath s a)

-- these three cases don't need any input

go_fast_end :: ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end !ByteString
bs (D.Fail msg :: String
msg)      = SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs String
msg
go_fast_end !ByteString
bs (D.Done x :: a
x)        = SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> a -> SlowPath s a
forall s a. ByteString -> a -> SlowPath s a
FastDone ByteString
bs a
x
go_fast_end !ByteString
bs (PeekAvailable k :: Int# -> ST s (DecodeAction s a)
k) = Int# -> ST s (DecodeAction s a)
k (case ByteString -> Int
BS.length ByteString
bs of I# len# :: Int#
len# -> Int#
len#) ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs

go_fast_end !ByteString
bs (PeekByteOffset k :: Int# -> ST s (DecodeAction s a)
k) = SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> (Int# -> ST s (DecodeAction s a)) -> SlowPath s a
forall s a.
ByteString -> (Int# -> ST s (DecodeAction s a)) -> SlowPath s a
SlowPeekByteOffset ByteString
bs Int# -> ST s (DecodeAction s a)
k

-- the next two cases only need the 1 byte token header
go_fast_end !ByteString
bs da :: DecodeAction s a
da | ByteString -> Bool
BS.null ByteString
bs = SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> DecodeAction s a -> SlowPath s a
forall s a. ByteString -> DecodeAction s a -> SlowPath s a
SlowDecodeAction ByteString
bs DecodeAction s a
da

go_fast_end !ByteString
bs (ConsumeBreakOr k :: Bool -> ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken ()
tryConsumeBreakOr (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> Bool -> ST s (DecodeAction s a)
k Bool
False ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs
      DecodedToken sz :: Int
sz _ -> Bool -> ST s (DecodeAction s a)
k Bool
True  ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (PeekTokenType k :: TokenType -> ST s (DecodeAction s a)
k) =
    let !hdr :: Word8
hdr  = ByteString -> Word8
BS.unsafeHead ByteString
bs
        !tkty :: TokenType
tkty = Array Word8 TokenType
decodeTokenTypeTable Array Word8 TokenType -> Int -> TokenType
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> Int -> e
`A.unsafeAt` Word8 -> Int
word8ToInt Word8
hdr
    in TokenType -> ST s (DecodeAction s a)
k TokenType
tkty ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs

-- all the remaining cases have to decode the current token

go_fast_end !ByteString
bs da :: DecodeAction s a
da
    | let !hdr :: Word8
hdr = ByteString -> Word8
BS.unsafeHead ByteString
bs
    , ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Word8 -> Int
tokenSize Word8
hdr
    = SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> DecodeAction s a -> SlowPath s a
forall s a. ByteString -> DecodeAction s a -> SlowPath s a
SlowDecodeAction ByteString
bs DecodeAction s a
da

go_fast_end !ByteString
bs (ConsumeWord k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeWord8 k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word8"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) ->
        case Word# -> Word# -> Int#
gtWord# Word#
w# 0xff## of
          0#                  -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                   -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word8"

go_fast_end !ByteString
bs (ConsumeWord16 k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word16"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) ->
        case Word# -> Word# -> Int#
gtWord# Word#
w# 0xffff## of
          0#                  -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                   -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word16"

go_fast_end !ByteString
bs (ConsumeWord32 k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word32"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) ->
#if defined(ARCH_32bit)
                                 k w# >>= go_fast_end (BS.unsafeDrop sz bs)
#else
        case Word# -> Word# -> Int#
gtWord# Word#
w# 0xffffffff## of
          0#                  -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                   -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word32"
#endif

go_fast_end !ByteString
bs (ConsumeNegWord k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeNegWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected negative int"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeInt k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeInt8 k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int8"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) ->
        case (Int#
n# Int# -> Int# -> Int#
># 0x7f#) Int# -> Int# -> Int#
`orI#` (Int#
n# Int# -> Int# -> Int#
<# -0x80#) of
          0#                  -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                   -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int8"

go_fast_end !ByteString
bs (ConsumeInt16 k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int16"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) ->
        case (Int#
n# Int# -> Int# -> Int#
># 0x7fff#) Int# -> Int# -> Int#
`orI#` (Int#
n# Int# -> Int# -> Int#
<# -0x8000#) of
          0#                  -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                   -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int16"

go_fast_end !ByteString
bs (ConsumeInt32 k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int32"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) ->
#if defined(ARCH_32bit)
                                 k n# >>= go_fast_end (BS.unsafeDrop sz bs)
#else
        case (Int#
n# Int# -> Int# -> Int#
># 0x7fffffff#) Int# -> Int# -> Int#
`orI#` (Int#
n# Int# -> Int# -> Int#
<# -0x80000000#) of
          0#                  -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          _                   -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int32"
#endif

go_fast_end !ByteString
bs (ConsumeListLen k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeListLen (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected list len"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeMapLen k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeMapLen (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected map len"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeTag k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeTag (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected tag"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeWordCanonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#)
        | Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise             -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical word"

go_fast_end !ByteString
bs (ConsumeWord8Canonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word8"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) -> case Word# -> Word# -> Int#
gtWord# Word#
w# 0xff## of
          0# | Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
             | Bool
otherwise             -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical word8"
          _                          -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word8"

go_fast_end !ByteString
bs (ConsumeWord16Canonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word16"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) -> case Word# -> Word# -> Int#
gtWord# Word#
w# 0xffff## of
        0# | Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
           | Bool
otherwise             -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical word16"
        _                          -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word16"

go_fast_end !ByteString
bs (ConsumeWord32Canonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word32"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) -> case Word# -> Int#
w_out_of_range Word#
w# of
        0# | Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
           | Bool
otherwise             -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical word32"
        _                          -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected word32"
  where
    w_out_of_range :: Word# -> Int#
    w_out_of_range :: Word# -> Int#
w_out_of_range _w# :: Word#
_w# =
#if defined(ARCH_32bit)
      0#
#else
      Word# -> Word# -> Int#
gtWord# Word#
_w# 0xffffffff##
#endif

go_fast_end !ByteString
bs (ConsumeNegWordCanonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeNegWord (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected negative int"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#)
        | Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise             -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical negative int"

go_fast_end !ByteString
bs (ConsumeIntCanonical k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#)
        | Int -> Int# -> Bool
isIntCanonical Int
sz Int#
n# -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise            -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical int"

go_fast_end !ByteString
bs (ConsumeInt8Canonical k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int8"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) ->
        case (Int#
n# Int# -> Int# -> Int#
># 0x7f#) Int# -> Int# -> Int#
`orI#` (Int#
n# Int# -> Int# -> Int#
<# -0x80#) of
          0# | Int -> Int# -> Bool
isIntCanonical Int
sz Int#
n# -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
             | Bool
otherwise            -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical int8"
          _                         -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int8"

go_fast_end !ByteString
bs (ConsumeInt16Canonical k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int16"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) ->
        case (Int#
n# Int# -> Int# -> Int#
># 0x7fff#) Int# -> Int# -> Int#
`orI#` (Int#
n# Int# -> Int# -> Int#
<# -0x8000#) of
          0# | Int -> Int# -> Bool
isIntCanonical Int
sz Int#
n# -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
             | Bool
otherwise            -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical int16"
          _                         -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int16"

go_fast_end !ByteString
bs (ConsumeInt32Canonical k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeInt (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int32"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) ->
        case Int# -> Int#
n_out_of_range Int#
n# of
          0# | Int -> Int# -> Bool
isIntCanonical Int
sz Int#
n# -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
             | Bool
otherwise            -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical int32"
          _                         -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected int32"
  where
    n_out_of_range :: Int# -> Int#
    n_out_of_range :: Int# -> Int#
n_out_of_range _n# :: Int#
_n# =
#if defined(ARCH_32bit)
      0#
#else
      (Int#
_n# Int# -> Int# -> Int#
># 0x7fffffff#) Int# -> Int# -> Int#
`orI#` (Int#
_n# Int# -> Int# -> Int#
<# -0x80000000#)
#endif

go_fast_end !ByteString
bs (ConsumeListLenCanonical k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeListLen (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected list len"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#)
          -- List length can't be negative, cast it to Word#.
        | Int -> Word# -> Bool
isWordCanonical Int
sz (Int# -> Word#
int2Word# Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise                         -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical list len"

go_fast_end !ByteString
bs (ConsumeMapLenCanonical k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeMapLen (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected map len"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#)
          -- Map length can't be negative, cast it to Word#.
        | Int -> Word# -> Bool
isWordCanonical Int
sz (Int# -> Word#
int2Word# Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise                         -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical map len"

go_fast_end !ByteString
bs (ConsumeTagCanonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeTag (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected tag"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#)
        | Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise             -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical tag"

#if defined(ARCH_32bit)
go_fast_end !bs (ConsumeWord64 k) =
  case tryConsumeWord64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> return $! SlowFail bs "expected word64"
    DecodedToken sz (W64# w#) -> k w# >>= go_fast_end (BS.unsafeDrop sz bs)

go_fast_end !bs (ConsumeNegWord64 k) =
  case tryConsumeNegWord64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> return $! SlowFail bs "expected negative int"
    DecodedToken sz (W64# w#) -> k w# >>= go_fast_end (BS.unsafeDrop sz bs)

go_fast_end !bs (ConsumeInt64 k) =
  case tryConsumeInt64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> return $! SlowFail bs "expected int64"
    DecodedToken sz (I64# i#) -> k i# >>= go_fast_end (BS.unsafeDrop sz bs)

go_fast_end !bs (ConsumeListLen64 k) =
  case tryConsumeListLen64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> return $! SlowFail bs "expected list len 64"
    DecodedToken sz (I64# i#) -> k i# >>= go_fast_end (BS.unsafeDrop sz bs)

go_fast_end !bs (ConsumeMapLen64 k) =
  case tryConsumeMapLen64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> return $! SlowFail bs "expected map len 64"
    DecodedToken sz (I64# i#) -> k i# >>= go_fast_end (BS.unsafeDrop sz bs)

go_fast_end !bs (ConsumeTag64 k) =
  case tryConsumeTag64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> return $! SlowFail bs "expected tag64"
    DecodedToken sz (W64# w#) -> k w# >>= go_fast_end (BS.unsafeDrop sz bs)

go_fast_end !bs (ConsumeWord64Canonical k) =
  case tryConsumeWord64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> return $! SlowFail bs "expected word64"
    DecodedToken sz (W64# w#)
      | isWord64Canonical sz w# -> k w# >>= go_fast_end (BS.unsafeDrop sz bs)
      | otherwise               -> return $! SlowFail bs "non-canonical word64"

go_fast_end !bs (ConsumeNegWord64Canonical k) =
  case tryConsumeNegWord64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> return $! SlowFail bs "expected negative int"
    DecodedToken sz (W64# w#)
      | isWord64Canonical sz w# -> k w# >>= go_fast_end (BS.unsafeDrop sz bs)
      | otherwise               -> return $! SlowFail bs "non-canonical negative int"

go_fast_end !bs (ConsumeInt64Canonical k) =
  case tryConsumeInt64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> return $! SlowFail bs "expected int64"
    DecodedToken sz (I64# i#)
      | isInt64Canonical sz i# -> k i# >>= go_fast_end (BS.unsafeDrop sz bs)
      | otherwise              -> return $! SlowFail bs "non-canonical int64"

go_fast_end !bs (ConsumeListLen64Canonical k) =
  case tryConsumeListLen64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> return $! SlowFail bs "expected list len 64"
    DecodedToken sz (I64# i#)
        -- List length can't be negative, cast it to Word64#.
      | isWord64Canonical sz (int64ToWord64# i#) ->
          k i# >>= go_fast_end (BS.unsafeDrop sz bs)
      | otherwise ->
          return $! SlowFail bs "non-canonical list len 64"

go_fast_end !bs (ConsumeMapLen64Canonical k) =
  case tryConsumeMapLen64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> return $! SlowFail bs "expected map len 64"
    DecodedToken sz (I64# i#)
        -- Map length can't be negative, cast it to Word64#.
      | isWord64Canonical sz (int64ToWord64# i#) ->
          k i# >>= go_fast_end (BS.unsafeDrop sz bs)
      | otherwise ->
          return $! SlowFail bs "non-canonical map len 64"

go_fast_end !bs (ConsumeTag64Canonical k) =
  case tryConsumeTag64 (BS.unsafeHead bs) bs of
    DecodeFailure             -> return $! SlowFail bs "expected tag64"
    DecodedToken sz (W64# w#)
      | isWord64Canonical sz w# -> k w# >>= go_fast_end (BS.unsafeDrop sz bs)
      | otherwise               -> return $! SlowFail bs "non-canonical tag64"

#endif

go_fast_end !ByteString
bs (ConsumeInteger k :: Integer -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (BigIntToken Integer)
tryConsumeInteger (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure                         -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected integer"
      DecodedToken sz :: Int
sz (BigIntToken _ n :: Integer
n)     -> Integer -> ST s (DecodeAction s a)
k Integer
n ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
      DecodedToken sz :: Int
sz (BigUIntNeedBody _ len :: Int
len) -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenBytes (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ((Integer -> ST s (DecodeAction s a))
-> ByteString -> ST s (DecodeAction s a)
forall s a.
(Integer -> ST s (DecodeAction s a))
-> ByteString -> ST s (DecodeAction s a)
adjustContBigUIntNeedBody Integer -> ST s (DecodeAction s a)
k) Int
len
      DecodedToken sz :: Int
sz (BigNIntNeedBody _ len :: Int
len) -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenBytes (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ((Integer -> ST s (DecodeAction s a))
-> ByteString -> ST s (DecodeAction s a)
forall s a.
(Integer -> ST s (DecodeAction s a))
-> ByteString -> ST s (DecodeAction s a)
adjustContBigNIntNeedBody Integer -> ST s (DecodeAction s a)
k) Int
len
      DecodedToken sz :: Int
sz  BigUIntNeedHeader    -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> DecodeAction s a -> SlowPath s a
forall s a. ByteString -> DecodeAction s a -> SlowPath s a
SlowDecodeAction      (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ((Integer -> ST s (DecodeAction s a)) -> DecodeAction s a
forall s a.
(Integer -> ST s (DecodeAction s a)) -> DecodeAction s a
adjustContBigUIntNeedHeader Integer -> ST s (DecodeAction s a)
k)
      DecodedToken sz :: Int
sz  BigNIntNeedHeader    -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> DecodeAction s a -> SlowPath s a
forall s a. ByteString -> DecodeAction s a -> SlowPath s a
SlowDecodeAction      (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ((Integer -> ST s (DecodeAction s a)) -> DecodeAction s a
forall s a.
(Integer -> ST s (DecodeAction s a)) -> DecodeAction s a
adjustContBigNIntNeedHeader Integer -> ST s (DecodeAction s a)
k)

go_fast_end !ByteString
bs (ConsumeFloat k :: Float# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Float
tryConsumeFloat (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure     -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected float"
      DecodedToken sz :: Int
sz (F# f# :: Float#
f#) -> Float# -> ST s (DecodeAction s a)
k Float#
f# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeDouble k :: Double# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Double
tryConsumeDouble (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected double"
      DecodedToken sz :: Int
sz (D# f# :: Double#
f#) -> Double# -> ST s (DecodeAction s a)
k Double#
f# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeBytes k :: ByteString -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeBytes (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure                   -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected bytes"
      DecodedToken sz :: Int
sz (Fits _ bstr :: ByteString
bstr)   -> ByteString -> ST s (DecodeAction s a)
k ByteString
bstr ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
      DecodedToken sz :: Int
sz (TooLong _ len :: Int
len) -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenBytes
                                                   (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ByteString -> ST s (DecodeAction s a)
k Int
len

go_fast_end !ByteString
bs (ConsumeByteArray k :: ByteArray -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeBytes (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure                   -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected string"
      DecodedToken sz :: Int
sz (Fits _ str :: ByteString
str)    -> (ByteArray -> ST s (DecodeAction s a)
k (ByteArray -> ST s (DecodeAction s a))
-> ByteArray -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> ByteArray
BA.fromByteString ByteString
str)
                                         ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
      DecodedToken sz :: Int
sz (TooLong _ len :: Int
len) -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenByteArray
                                                   (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ByteArray -> ST s (DecodeAction s a)
k Int
len

go_fast_end !ByteString
bs (ConsumeString k :: Text -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeString (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure                   -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected string"
      DecodedToken sz :: Int
sz (Fits _ str :: ByteString
str)    -> case ByteString -> Either UnicodeException Text
T.decodeUtf8' ByteString
str of
        Right t :: Text
t -> Text -> ST s (DecodeAction s a)
k Text
t ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        Left _e :: UnicodeException
_e -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "invalid UTF8"
      DecodedToken sz :: Int
sz (TooLong _ len :: Int
len) -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (Text -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (Text -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenString
                                                   (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) Text -> ST s (DecodeAction s a)
k Int
len

go_fast_end !ByteString
bs (ConsumeUtf8ByteArray k :: ByteArray -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeString (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure                   -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected string"
      DecodedToken sz :: Int
sz (Fits _ str :: ByteString
str)    -> (ByteArray -> ST s (DecodeAction s a)
k (ByteArray -> ST s (DecodeAction s a))
-> ByteArray -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> ByteArray
BA.fromByteString ByteString
str)
                                         ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
      DecodedToken sz :: Int
sz (TooLong _ len :: Int
len) -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenUtf8ByteArray
                                                   (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ByteArray -> ST s (DecodeAction s a)
k Int
len

go_fast_end !ByteString
bs (ConsumeBool k :: Bool -> ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken Bool
tryConsumeBool (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected bool"
      DecodedToken sz :: Int
sz b :: Bool
b -> Bool -> ST s (DecodeAction s a)
k Bool
b ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeSimple k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeSimple (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected simple"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#) -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeIntegerCanonical k :: Integer -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (BigIntToken Integer)
tryConsumeInteger (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure                         -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected integer"
      DecodedToken sz :: Int
sz (BigIntToken True n :: Integer
n)  -> Integer -> ST s (DecodeAction s a)
k Integer
n ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
      DecodedToken sz :: Int
sz (BigUIntNeedBody True len :: Int
len) -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenBytes
        (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ((Integer -> ST s (DecodeAction s a))
-> ByteString -> ST s (DecodeAction s a)
forall s a.
(Integer -> ST s (DecodeAction s a))
-> ByteString -> ST s (DecodeAction s a)
adjustContCanonicalBigUIntNeedBody Integer -> ST s (DecodeAction s a)
k) Int
len
      DecodedToken sz :: Int
sz (BigNIntNeedBody True len :: Int
len) -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenBytes
        (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ((Integer -> ST s (DecodeAction s a))
-> ByteString -> ST s (DecodeAction s a)
forall s a.
(Integer -> ST s (DecodeAction s a))
-> ByteString -> ST s (DecodeAction s a)
adjustContCanonicalBigNIntNeedBody Integer -> ST s (DecodeAction s a)
k) Int
len
      DecodedToken sz :: Int
sz  BigUIntNeedHeader -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> DecodeAction s a -> SlowPath s a
forall s a. ByteString -> DecodeAction s a -> SlowPath s a
SlowDecodeAction
        (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ((Integer -> ST s (DecodeAction s a)) -> DecodeAction s a
forall s a.
(Integer -> ST s (DecodeAction s a)) -> DecodeAction s a
adjustContCanonicalBigUIntNeedHeader Integer -> ST s (DecodeAction s a)
k)
      DecodedToken sz :: Int
sz  BigNIntNeedHeader -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> DecodeAction s a -> SlowPath s a
forall s a. ByteString -> DecodeAction s a -> SlowPath s a
SlowDecodeAction
        (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ((Integer -> ST s (DecodeAction s a)) -> DecodeAction s a
forall s a.
(Integer -> ST s (DecodeAction s a)) -> DecodeAction s a
adjustContCanonicalBigNIntNeedHeader Integer -> ST s (DecodeAction s a)
k)
      _ -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical integer"

go_fast_end !ByteString
bs (ConsumeFloat16Canonical k :: Float# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Float
tryConsumeFloat (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure     -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected float"
      DecodedToken sz :: Int
sz f :: Float
f@(F# f# :: Float#
f#)
        | Int -> ByteString -> Float -> Bool
isFloat16Canonical Int
sz ByteString
bs Float
f -> Float# -> ST s (DecodeAction s a)
k Float#
f# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise                  -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical float16"

go_fast_end !ByteString
bs (ConsumeFloatCanonical k :: Float# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Float
tryConsumeFloat (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure     -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected float"
      DecodedToken sz :: Int
sz f :: Float
f@(F# f# :: Float#
f#)
        | Int -> ByteString -> Float -> Bool
isFloatCanonical Int
sz ByteString
bs Float
f -> Float# -> ST s (DecodeAction s a)
k Float#
f# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise                -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical float"

go_fast_end !ByteString
bs (ConsumeDoubleCanonical k :: Double# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Double
tryConsumeDouble (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected double"
      DecodedToken sz :: Int
sz f :: Double
f@(D# f# :: Double#
f#)
        | Int -> ByteString -> Double -> Bool
isDoubleCanonical Int
sz ByteString
bs Double
f -> Double# -> ST s (DecodeAction s a)
k Double#
f# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise                 -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical double"

go_fast_end !ByteString
bs (ConsumeBytesCanonical k :: ByteString -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeBytes (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure         -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected bytes"
      DecodedToken sz :: Int
sz token :: LongToken ByteString
token -> case LongToken ByteString
token of
        Fits True bstr :: ByteString
bstr   -> ByteString -> ST s (DecodeAction s a)
k ByteString
bstr ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        TooLong True len :: Int
len -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteString -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenBytes (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ByteString -> ST s (DecodeAction s a)
k Int
len
        _                -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical length prefix"

go_fast_end !ByteString
bs (ConsumeByteArrayCanonical k :: ByteArray -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeBytes (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure         -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected string"
      DecodedToken sz :: Int
sz token :: LongToken ByteString
token -> case LongToken ByteString
token of
        Fits True str :: ByteString
str    ->
          (ByteArray -> ST s (DecodeAction s a)
k (ByteArray -> ST s (DecodeAction s a))
-> ByteArray -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> ByteArray
BA.fromByteString ByteString
str) ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        TooLong True len :: Int
len ->
           SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenByteArray (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ByteArray -> ST s (DecodeAction s a)
k Int
len
        _                -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical length prefix"

go_fast_end !ByteString
bs (ConsumeStringCanonical k :: Text -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeString (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure         -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected string"
      DecodedToken sz :: Int
sz token :: LongToken ByteString
token -> case LongToken ByteString
token of
        Fits True str :: ByteString
str    -> case ByteString -> Either UnicodeException Text
T.decodeUtf8' ByteString
str of
          Right t :: Text
t -> Text -> ST s (DecodeAction s a)
k Text
t ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
          Left _e :: UnicodeException
_e -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "invalid UTF8"
        TooLong True len :: Int
len -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (Text -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (Text -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenString (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) Text -> ST s (DecodeAction s a)
k Int
len
        _                -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical length prefix"

go_fast_end !ByteString
bs (ConsumeUtf8ByteArrayCanonical k :: ByteArray -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeString (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure                 -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected string"
      DecodedToken sz :: Int
sz token :: LongToken ByteString
token -> case LongToken ByteString
token of
        Fits True str :: ByteString
str    ->
          (ByteArray -> ST s (DecodeAction s a)
k (ByteArray -> ST s (DecodeAction s a))
-> ByteArray -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> ByteArray
BA.fromByteString ByteString
str) ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        TooLong True len :: Int
len ->
          SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
forall s a.
ByteString
-> (ByteArray -> ST s (DecodeAction s a)) -> Int -> SlowPath s a
SlowConsumeTokenUtf8ByteArray (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs) ByteArray -> ST s (DecodeAction s a)
k Int
len
        _                ->
          SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical length prefix"

go_fast_end !ByteString
bs (ConsumeSimpleCanonical k :: Word# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Word
tryConsumeSimple (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected simple"
      DecodedToken sz :: Int
sz (W# w# :: Word#
w#)
        | Int -> Word# -> Bool
isSimpleCanonical Int
sz Word#
w# -> Word# -> ST s (DecodeAction s a)
k Word#
w# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)
        | Bool
otherwise               -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "non-canonical simple"

go_fast_end !ByteString
bs (ConsumeBytesIndef k :: ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken ()
tryConsumeBytesIndef (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected bytes start"
      DecodedToken sz :: Int
sz _ -> ST s (DecodeAction s a)
k ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeStringIndef k :: ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken ()
tryConsumeStringIndef (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected string start"
      DecodedToken sz :: Int
sz _ -> ST s (DecodeAction s a)
k ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeListLenIndef k :: ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken ()
tryConsumeListLenIndef (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected list start"
      DecodedToken sz :: Int
sz _ -> ST s (DecodeAction s a)
k ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeMapLenIndef k :: ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken ()
tryConsumeMapLenIndef (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected map start"
      DecodedToken sz :: Int
sz _ -> ST s (DecodeAction s a)
k ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeNull k :: ST s (DecodeAction s a)
k) =
    case Word8 -> DecodedToken ()
tryConsumeNull (ByteString -> Word8
BS.unsafeHead ByteString
bs) of
      DecodeFailure     -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected null"
      DecodedToken sz :: Int
sz _ -> ST s (DecodeAction s a)
k ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeListLenOrIndef k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeListLenOrIndef (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected list len or indef"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)

go_fast_end !ByteString
bs (ConsumeMapLenOrIndef k :: Int# -> ST s (DecodeAction s a)
k) =
    case Word8 -> ByteString -> DecodedToken Int
tryConsumeMapLenOrIndef (ByteString -> Word8
BS.unsafeHead ByteString
bs) ByteString
bs of
      DecodeFailure           -> SlowPath s a -> ST s (SlowPath s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SlowPath s a -> ST s (SlowPath s a))
-> SlowPath s a -> ST s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> String -> SlowPath s a
forall s a. ByteString -> String -> SlowPath s a
SlowFail ByteString
bs "expected map len or indef"
      DecodedToken sz :: Int
sz (I# n# :: Int#
n#) -> Int# -> ST s (DecodeAction s a)
k Int#
n# ST s (DecodeAction s a)
-> (DecodeAction s a -> ST s (SlowPath s a)) -> ST s (SlowPath s a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end (Int -> ByteString -> ByteString
BS.unsafeDrop Int
sz ByteString
bs)


-- The slow path starts off by running the fast path on the current chunk
-- then looking at where it finished, fixing up the chunk boundary issues,
-- getting more input and going around again.
--
-- The offset here is the offset after of all data consumed so far,
-- so not including the current chunk.
--
go_slow :: DecodeAction s a -> ByteString -> ByteOffset
        -> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow :: DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow da :: DecodeAction s a
da bs :: ByteString
bs !ByteOffset
offset = do
  SlowPath s a
slowpath <- ST s (SlowPath s a) -> IncrementalDecoder s (SlowPath s a)
forall s a. ST s a -> IncrementalDecoder s a
lift (ST s (SlowPath s a) -> IncrementalDecoder s (SlowPath s a))
-> ST s (SlowPath s a) -> IncrementalDecoder s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$ ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast ByteString
bs DecodeAction s a
da
  case SlowPath s a
slowpath of
    FastDone bs' :: ByteString
bs' x :: a
x -> (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
bs', ByteOffset
offset', a
x)
      where
        !offset' :: ByteOffset
offset' = ByteOffset
offset ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs')

    SlowConsumeTokenBytes bs' :: ByteString
bs' k :: ByteString -> ST s (DecodeAction s a)
k len :: Int
len -> do
      (bstr :: ByteString
bstr, bs'' :: ByteString
bs'') <- Int
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
forall s.
Int
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
getTokenVarLen Int
len ByteString
bs' ByteOffset
offset'
      ST s (DecodeAction s a) -> IncrementalDecoder s (DecodeAction s a)
forall s a. ST s a -> IncrementalDecoder s a
lift (ByteString -> ST s (DecodeAction s a)
k ByteString
bstr) IncrementalDecoder s (DecodeAction s a)
-> (DecodeAction s a
    -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \daz :: DecodeAction s a
daz -> DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow DecodeAction s a
daz ByteString
bs'' (ByteOffset
offset' ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 Int
len)
      where
        !offset' :: ByteOffset
offset' = ByteOffset
offset ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs')

    SlowConsumeTokenByteArray bs' :: ByteString
bs' k :: ByteArray -> ST s (DecodeAction s a)
k len :: Int
len -> do
      (bstr :: ByteString
bstr, bs'' :: ByteString
bs'') <- Int
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
forall s.
Int
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
getTokenVarLen Int
len ByteString
bs' ByteOffset
offset'
      let !str :: ByteArray
str = ByteString -> ByteArray
BA.fromByteString ByteString
bstr
      ST s (DecodeAction s a) -> IncrementalDecoder s (DecodeAction s a)
forall s a. ST s a -> IncrementalDecoder s a
lift (ByteArray -> ST s (DecodeAction s a)
k ByteArray
str) IncrementalDecoder s (DecodeAction s a)
-> (DecodeAction s a
    -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \daz :: DecodeAction s a
daz -> DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow DecodeAction s a
daz ByteString
bs'' (ByteOffset
offset' ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 Int
len)
      where
        !offset' :: ByteOffset
offset' = ByteOffset
offset ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs')

    SlowConsumeTokenString bs' :: ByteString
bs' k :: Text -> ST s (DecodeAction s a)
k len :: Int
len -> do
      (bstr :: ByteString
bstr, bs'' :: ByteString
bs'') <- Int
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
forall s.
Int
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
getTokenVarLen Int
len ByteString
bs' ByteOffset
offset'
      case ByteString -> Either UnicodeException Text
T.decodeUtf8' ByteString
bstr of
        Right str :: Text
str -> ST s (DecodeAction s a) -> IncrementalDecoder s (DecodeAction s a)
forall s a. ST s a -> IncrementalDecoder s a
lift (Text -> ST s (DecodeAction s a)
k Text
str) IncrementalDecoder s (DecodeAction s a)
-> (DecodeAction s a
    -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \daz :: DecodeAction s a
daz ->
                     DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow DecodeAction s a
daz ByteString
bs'' (ByteOffset
offset' ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 Int
len)
        Left _e :: UnicodeException
_e   -> ByteString
-> ByteOffset
-> String
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
ByteString -> ByteOffset -> String -> IncrementalDecoder s a
decodeFail ByteString
bs' ByteOffset
offset' "invalid UTF8"
      where
        !offset' :: ByteOffset
offset' = ByteOffset
offset ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs')

    SlowConsumeTokenUtf8ByteArray bs' :: ByteString
bs' k :: ByteArray -> ST s (DecodeAction s a)
k len :: Int
len -> do
      (bstr :: ByteString
bstr, bs'' :: ByteString
bs'') <- Int
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
forall s.
Int
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
getTokenVarLen Int
len ByteString
bs' ByteOffset
offset'
      let !str :: ByteArray
str = ByteString -> ByteArray
BA.fromByteString ByteString
bstr
      ST s (DecodeAction s a) -> IncrementalDecoder s (DecodeAction s a)
forall s a. ST s a -> IncrementalDecoder s a
lift (ByteArray -> ST s (DecodeAction s a)
k ByteArray
str) IncrementalDecoder s (DecodeAction s a)
-> (DecodeAction s a
    -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \daz :: DecodeAction s a
daz -> DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow DecodeAction s a
daz ByteString
bs'' (ByteOffset
offset' ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 Int
len)
      where
        !offset' :: ByteOffset
offset' = ByteOffset
offset ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs')

    -- we didn't have enough input in the buffer
    SlowDecodeAction bs' :: ByteString
bs' da' :: DecodeAction s a
da' | ByteString -> Bool
BS.null ByteString
bs' -> do
      -- in this case we're exactly out of input
      -- so we can get more input and carry on
      Maybe ByteString
mbs <- IncrementalDecoder s (Maybe ByteString)
forall s. IncrementalDecoder s (Maybe ByteString)
needChunk
      case Maybe ByteString
mbs of
        Nothing   -> ByteString
-> ByteOffset
-> String
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
ByteString -> ByteOffset -> String -> IncrementalDecoder s a
decodeFail ByteString
bs' ByteOffset
offset' "end of input"
        Just bs'' :: ByteString
bs'' -> DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow DecodeAction s a
da' ByteString
bs'' ByteOffset
offset'
      where
        !offset' :: ByteOffset
offset' = ByteOffset
offset ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs')

    SlowDecodeAction bs' :: ByteString
bs' da' :: DecodeAction s a
da' ->
      -- of course we should only end up here when we really are out of
      -- input, otherwise go_fast_end could have continued
      Bool
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (ByteString -> Int
BS.length ByteString
bs' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Word8 -> Int
tokenSize (ByteString -> Word8
BS.head ByteString
bs')) (IncrementalDecoder s (ByteString, ByteOffset, a)
 -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a b. (a -> b) -> a -> b
$
      DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow_fixup DecodeAction s a
da' ByteString
bs' ByteOffset
offset'
      where
        !offset' :: ByteOffset
offset' = ByteOffset
offset ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs')

    SlowPeekByteOffset bs' :: ByteString
bs' k :: Int# -> ST s (DecodeAction s a)
k ->
      ST s (DecodeAction s a) -> IncrementalDecoder s (DecodeAction s a)
forall s a. ST s a -> IncrementalDecoder s a
lift (Int# -> ST s (DecodeAction s a)
k Int#
off#) IncrementalDecoder s (DecodeAction s a)
-> (DecodeAction s a
    -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \daz :: DecodeAction s a
daz -> DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow DecodeAction s a
daz ByteString
bs' ByteOffset
offset'
      where
        !offset' :: ByteOffset
offset'@(I64# off# :: Int#
off#) = ByteOffset
offset ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs')

    SlowFail bs' :: ByteString
bs' msg :: String
msg -> ByteString
-> ByteOffset
-> String
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
ByteString -> ByteOffset -> String -> IncrementalDecoder s a
decodeFail ByteString
bs' ByteOffset
offset' String
msg
      where
        !offset' :: ByteOffset
offset' = ByteOffset
offset ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs')

-- The complicated case is when a token spans a chunk boundary.
--
-- Our goal is to get enough input so that go_fast_end can consume exactly one
-- token without need for further fixups.
--
go_slow_fixup :: DecodeAction s a -> ByteString -> ByteOffset
              -> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow_fixup :: DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow_fixup da :: DecodeAction s a
da !ByteString
bs !ByteOffset
offset = do
    let !hdr :: Word8
hdr = ByteString -> Word8
BS.head ByteString
bs
        !sz :: Int
sz  = Word8 -> Int
tokenSize Word8
hdr
    Maybe ByteString
mbs <- IncrementalDecoder s (Maybe ByteString)
forall s. IncrementalDecoder s (Maybe ByteString)
needChunk
    case Maybe ByteString
mbs of
      Nothing -> ByteString
-> ByteOffset
-> String
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
ByteString -> ByteOffset -> String -> IncrementalDecoder s a
decodeFail ByteString
bs ByteOffset
offset "end of input"

      Just bs' :: ByteString
bs'
          -- We have enough input now, try reading one final token
        | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
BS.length ByteString
bs' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
sz
       -> DecodeAction s a
-> Int
-> ByteString
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> Int
-> ByteString
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow_overlapped DecodeAction s a
da Int
sz ByteString
bs ByteString
bs' ByteOffset
offset

          -- We still don't have enough input, get more
        | Bool
otherwise
       -> DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow_fixup DecodeAction s a
da (ByteString
bs ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
bs') ByteOffset
offset

-- We've now got more input, but we have one token that spanned the old and
-- new input buffers, so we have to decode that one before carrying on
go_slow_overlapped :: DecodeAction s a -> Int -> ByteString -> ByteString
                   -> ByteOffset
                   -> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow_overlapped :: DecodeAction s a
-> Int
-> ByteString
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow_overlapped da :: DecodeAction s a
da sz :: Int
sz bs_cur :: ByteString
bs_cur bs_next :: ByteString
bs_next !ByteOffset
offset =

    -- we have:
    --   sz            the size of the pending input token
    --   bs_cur        the tail end of the previous input buffer
    --   bs_next       the next input chunk

    -- we know the old buffer is too small, but the combo is enough
    Bool
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (ByteString -> Int
BS.length ByteString
bs_cur Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (IncrementalDecoder s (ByteString, ByteOffset, a)
 -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a b. (a -> b) -> a -> b
$
    Bool
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (ByteString -> Int
BS.length ByteString
bs_cur Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
BS.length ByteString
bs_next Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
sz) (IncrementalDecoder s (ByteString, ByteOffset, a)
 -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a b. (a -> b) -> a -> b
$

    -- we make:
    --   bs_tok        a buffer containing only the pending input token
    --   bs'           the tail of the next input chunk,
    --                   which will become the next input buffer

    let bs_tok :: ByteString
bs_tok   = ByteString
bs_cur ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Int -> ByteString -> ByteString
BS.unsafeTake (Int
sz Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs_cur) ByteString
bs_next
        bs' :: ByteString
bs'      =           Int -> ByteString -> ByteString
BS.unsafeDrop (Int
sz Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs_cur) ByteString
bs_next
        offset' :: ByteOffset
offset'  = ByteOffset
offset ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 Int
sz in

    -- so the token chunk should be exactly the right size
    Bool
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (ByteString -> Int
BS.length ByteString
bs_tok Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz) (IncrementalDecoder s (ByteString, ByteOffset, a)
 -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a b. (a -> b) -> a -> b
$
    -- and overall we shouldn't loose any input
    Bool
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (ByteString -> Int
BS.length ByteString
bs_cur Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
BS.length ByteString
bs_next Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
BS.length ByteString
bs') (IncrementalDecoder s (ByteString, ByteOffset, a)
 -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a b. (a -> b) -> a -> b
$ do

    -- so now we can run the fast path to consume just this one token
    SlowPath s a
slowpath <- ST s (SlowPath s a) -> IncrementalDecoder s (SlowPath s a)
forall s a. ST s a -> IncrementalDecoder s a
lift (ST s (SlowPath s a) -> IncrementalDecoder s (SlowPath s a))
-> ST s (SlowPath s a) -> IncrementalDecoder s (SlowPath s a)
forall a b. (a -> b) -> a -> b
$ ByteString -> DecodeAction s a -> ST s (SlowPath s a)
forall s a. ByteString -> DecodeAction s a -> ST s (SlowPath s a)
go_fast_end ByteString
bs_tok DecodeAction s a
da
    case SlowPath s a
slowpath of

      -- typically we'll fall out of the fast path having
      -- consumed exactly one token, now with no trailing data
      SlowDecodeAction bs_empty :: ByteString
bs_empty da' :: DecodeAction s a
da' ->
        Bool
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (ByteString -> Bool
BS.null ByteString
bs_empty) (IncrementalDecoder s (ByteString, ByteOffset, a)
 -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a b. (a -> b) -> a -> b
$
        DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow DecodeAction s a
da' ByteString
bs' ByteOffset
offset'

      -- but the other possibilities can happen too
      FastDone bs_empty :: ByteString
bs_empty x :: a
x ->
        Bool
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (ByteString -> Bool
BS.null ByteString
bs_empty) (IncrementalDecoder s (ByteString, ByteOffset, a)
 -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a b. (a -> b) -> a -> b
$
        (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
bs', ByteOffset
offset', a
x)

      SlowConsumeTokenBytes bs_empty :: ByteString
bs_empty k :: ByteString -> ST s (DecodeAction s a)
k len :: Int
len ->
        Bool
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (ByteString -> Bool
BS.null ByteString
bs_empty) (IncrementalDecoder s (ByteString, ByteOffset, a)
 -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a b. (a -> b) -> a -> b
$ do
        (bstr :: ByteString
bstr, bs'' :: ByteString
bs'') <- ByteString
-> ByteOffset
-> Int
-> IncrementalDecoder s (ByteString, ByteString)
forall s.
ByteString
-> ByteOffset
-> Int
-> IncrementalDecoder s (ByteString, ByteString)
getTokenShortOrVarLen ByteString
bs' ByteOffset
offset' Int
len
        ST s (DecodeAction s a) -> IncrementalDecoder s (DecodeAction s a)
forall s a. ST s a -> IncrementalDecoder s a
lift (ByteString -> ST s (DecodeAction s a)
k ByteString
bstr) IncrementalDecoder s (DecodeAction s a)
-> (DecodeAction s a
    -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \daz :: DecodeAction s a
daz -> DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow DecodeAction s a
daz ByteString
bs'' (ByteOffset
offset' ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 Int
len)

      SlowConsumeTokenByteArray bs_empty :: ByteString
bs_empty k :: ByteArray -> ST s (DecodeAction s a)
k len :: Int
len ->
        Bool
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (ByteString -> Bool
BS.null ByteString
bs_empty) (IncrementalDecoder s (ByteString, ByteOffset, a)
 -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a b. (a -> b) -> a -> b
$ do
        (bstr :: ByteString
bstr, bs'' :: ByteString
bs'') <- ByteString
-> ByteOffset
-> Int
-> IncrementalDecoder s (ByteString, ByteString)
forall s.
ByteString
-> ByteOffset
-> Int
-> IncrementalDecoder s (ByteString, ByteString)
getTokenShortOrVarLen ByteString
bs' ByteOffset
offset' Int
len
        let !ba :: ByteArray
ba = ByteString -> ByteArray
BA.fromByteString ByteString
bstr
        ST s (DecodeAction s a) -> IncrementalDecoder s (DecodeAction s a)
forall s a. ST s a -> IncrementalDecoder s a
lift (ByteArray -> ST s (DecodeAction s a)
k ByteArray
ba) IncrementalDecoder s (DecodeAction s a)
-> (DecodeAction s a
    -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \daz :: DecodeAction s a
daz -> DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow DecodeAction s a
daz ByteString
bs'' (ByteOffset
offset' ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 Int
len)

      SlowConsumeTokenString bs_empty :: ByteString
bs_empty k :: Text -> ST s (DecodeAction s a)
k len :: Int
len ->
        Bool
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (ByteString -> Bool
BS.null ByteString
bs_empty) (IncrementalDecoder s (ByteString, ByteOffset, a)
 -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a b. (a -> b) -> a -> b
$ do
        (bstr :: ByteString
bstr, bs'' :: ByteString
bs'') <- ByteString
-> ByteOffset
-> Int
-> IncrementalDecoder s (ByteString, ByteString)
forall s.
ByteString
-> ByteOffset
-> Int
-> IncrementalDecoder s (ByteString, ByteString)
getTokenShortOrVarLen ByteString
bs' ByteOffset
offset' Int
len
        case ByteString -> Either UnicodeException Text
T.decodeUtf8' ByteString
bstr of
          Right str :: Text
str -> ST s (DecodeAction s a) -> IncrementalDecoder s (DecodeAction s a)
forall s a. ST s a -> IncrementalDecoder s a
lift (Text -> ST s (DecodeAction s a)
k Text
str) IncrementalDecoder s (DecodeAction s a)
-> (DecodeAction s a
    -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \daz :: DecodeAction s a
daz ->
                       DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow DecodeAction s a
daz ByteString
bs'' (ByteOffset
offset' ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 Int
len)
          Left _e :: UnicodeException
_e   -> ByteString
-> ByteOffset
-> String
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
ByteString -> ByteOffset -> String -> IncrementalDecoder s a
decodeFail ByteString
bs' ByteOffset
offset' "invalid UTF8"

      SlowConsumeTokenUtf8ByteArray bs_empty :: ByteString
bs_empty k :: ByteArray -> ST s (DecodeAction s a)
k len :: Int
len ->
        Bool
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (ByteString -> Bool
BS.null ByteString
bs_empty) (IncrementalDecoder s (ByteString, ByteOffset, a)
 -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a b. (a -> b) -> a -> b
$ do
        (bstr :: ByteString
bstr, bs'' :: ByteString
bs'') <- ByteString
-> ByteOffset
-> Int
-> IncrementalDecoder s (ByteString, ByteString)
forall s.
ByteString
-> ByteOffset
-> Int
-> IncrementalDecoder s (ByteString, ByteString)
getTokenShortOrVarLen ByteString
bs' ByteOffset
offset' Int
len
        let !ba :: ByteArray
ba = ByteString -> ByteArray
BA.fromByteString ByteString
bstr
        ST s (DecodeAction s a) -> IncrementalDecoder s (DecodeAction s a)
forall s a. ST s a -> IncrementalDecoder s a
lift (ByteArray -> ST s (DecodeAction s a)
k ByteArray
ba) IncrementalDecoder s (DecodeAction s a)
-> (DecodeAction s a
    -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \daz :: DecodeAction s a
daz -> DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow DecodeAction s a
daz ByteString
bs'' (ByteOffset
offset' ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 Int
len)

      SlowPeekByteOffset bs_empty :: ByteString
bs_empty k :: Int# -> ST s (DecodeAction s a)
k ->
        Bool
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (ByteString -> Bool
BS.null ByteString
bs_empty) (IncrementalDecoder s (ByteString, ByteOffset, a)
 -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall a b. (a -> b) -> a -> b
$ do
        ST s (DecodeAction s a) -> IncrementalDecoder s (DecodeAction s a)
forall s a. ST s a -> IncrementalDecoder s a
lift (Int# -> ST s (DecodeAction s a)
k Int#
off#) IncrementalDecoder s (DecodeAction s a)
-> (DecodeAction s a
    -> IncrementalDecoder s (ByteString, ByteOffset, a))
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \daz :: DecodeAction s a
daz -> DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
DecodeAction s a
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteOffset, a)
go_slow DecodeAction s a
daz ByteString
bs' ByteOffset
offset'
        where
          !(I64# off# :: Int#
off#) = ByteOffset
offset'

      SlowFail bs_unconsumed :: ByteString
bs_unconsumed msg :: String
msg ->
        ByteString
-> ByteOffset
-> String
-> IncrementalDecoder s (ByteString, ByteOffset, a)
forall s a.
ByteString -> ByteOffset -> String -> IncrementalDecoder s a
decodeFail (ByteString
bs_unconsumed ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
bs') ByteOffset
offset'' String
msg
        where
          !offset'' :: ByteOffset
offset'' = ByteOffset
offset ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
intToInt64 (Int
sz Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs_unconsumed)
  where
    {-# INLINE getTokenShortOrVarLen #-}
    getTokenShortOrVarLen :: BS.ByteString
                          -> ByteOffset
                          -> Int
                          -> IncrementalDecoder s (ByteString, ByteString)
    getTokenShortOrVarLen :: ByteString
-> ByteOffset
-> Int
-> IncrementalDecoder s (ByteString, ByteString)
getTokenShortOrVarLen bs' :: ByteString
bs' offset' :: ByteOffset
offset' len :: Int
len
      | ByteString -> Int
BS.length ByteString
bs' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
len = Int
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
forall s.
Int
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
getTokenVarLen Int
len ByteString
bs' ByteOffset
offset'
      | Bool
otherwise           = let !bstr :: ByteString
bstr = Int -> ByteString -> ByteString
BS.take Int
len ByteString
bs'
                                  !bs'' :: ByteString
bs'' = Int -> ByteString -> ByteString
BS.drop Int
len ByteString
bs'
                               in (ByteString, ByteString)
-> IncrementalDecoder s (ByteString, ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
bstr, ByteString
bs'')


-- TODO FIXME: we can do slightly better here. If we're returning a
-- lazy string (String, lazy Text, lazy ByteString) then we don't have
-- to strictify here and if we're returning a strict string perhaps we
-- can still stream the utf8 validation/converstion

-- TODO FIXME: also consider sharing or not sharing here, and possibly
-- rechunking.

getTokenVarLen :: Int -> ByteString -> ByteOffset
               -> IncrementalDecoder s (ByteString, ByteString)
getTokenVarLen :: Int
-> ByteString
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
getTokenVarLen len :: Int
len bs :: ByteString
bs offset :: ByteOffset
offset =
    Bool
-> IncrementalDecoder s (ByteString, ByteString)
-> IncrementalDecoder s (ByteString, ByteString)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> ByteString -> Int
BS.length ByteString
bs) (IncrementalDecoder s (ByteString, ByteString)
 -> IncrementalDecoder s (ByteString, ByteString))
-> IncrementalDecoder s (ByteString, ByteString)
-> IncrementalDecoder s (ByteString, ByteString)
forall a b. (a -> b) -> a -> b
$ do
    Maybe ByteString
mbs <- IncrementalDecoder s (Maybe ByteString)
forall s. IncrementalDecoder s (Maybe ByteString)
needChunk
    case Maybe ByteString
mbs of
      Nothing -> ByteString
-> ByteOffset
-> String
-> IncrementalDecoder s (ByteString, ByteString)
forall s a.
ByteString -> ByteOffset -> String -> IncrementalDecoder s a
decodeFail ByteString
BS.empty ByteOffset
offset "end of input"
      Just bs' :: ByteString
bs'
        | let n :: Int
n = Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs
        , ByteString -> Int
BS.length ByteString
bs' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n ->
            let !tok :: ByteString
tok = ByteString
bs ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Int -> ByteString -> ByteString
BS.unsafeTake Int
n ByteString
bs'
             in (ByteString, ByteString)
-> IncrementalDecoder s (ByteString, ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
tok, Int -> ByteString -> ByteString
BS.drop Int
n ByteString
bs')

        | Bool
otherwise -> [ByteString]
-> Int
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
forall s.
[ByteString]
-> Int
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
getTokenVarLenSlow
                         [ByteString
bs',ByteString
bs]
                         (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
BS.length ByteString
bs'))
                         ByteOffset
offset

getTokenVarLenSlow :: [ByteString] -> Int -> ByteOffset
                   -> IncrementalDecoder s (ByteString, ByteString)
getTokenVarLenSlow :: [ByteString]
-> Int
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
getTokenVarLenSlow bss :: [ByteString]
bss n :: Int
n offset :: ByteOffset
offset = do
    Maybe ByteString
mbs <- IncrementalDecoder s (Maybe ByteString)
forall s. IncrementalDecoder s (Maybe ByteString)
needChunk
    case Maybe ByteString
mbs of
      Nothing -> ByteString
-> ByteOffset
-> String
-> IncrementalDecoder s (ByteString, ByteString)
forall s a.
ByteString -> ByteOffset -> String -> IncrementalDecoder s a
decodeFail ByteString
BS.empty ByteOffset
offset "end of input"
      Just bs :: ByteString
bs
        | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n ->
            let !tok :: ByteString
tok = [ByteString] -> ByteString
BS.concat ([ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse (Int -> ByteString -> ByteString
BS.unsafeTake Int
n ByteString
bs ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
bss))
             in (ByteString, ByteString)
-> IncrementalDecoder s (ByteString, ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
tok, Int -> ByteString -> ByteString
BS.drop Int
n ByteString
bs)
        | Bool
otherwise -> [ByteString]
-> Int
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
forall s.
[ByteString]
-> Int
-> ByteOffset
-> IncrementalDecoder s (ByteString, ByteString)
getTokenVarLenSlow (ByteString
bsByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
:[ByteString]
bss) (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
bs) ByteOffset
offset



tokenSize :: Word8 -> Int
tokenSize :: Word8 -> Int
tokenSize hdr :: Word8
hdr =
    Word8 -> Int
word8ToInt (Word8 -> Int) -> Word8 -> Int
forall a b. (a -> b) -> a -> b
$
      UArray Word8 Word8
decodeTableSz UArray Word8 Word8 -> Int -> Word8
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> Int -> e
`A.unsafeAt` (Word8 -> Int
word8ToInt Word8
hdr Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x1f)

decodeTableSz :: UArray Word8 Word8
decodeTableSz :: UArray Word8 Word8
decodeTableSz =
  (Word8, Word8) -> [(Word8, Word8)] -> UArray Word8 Word8
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [(i, e)] -> a i e
array (0, 0x1f) ([(Word8, Word8)] -> UArray Word8 Word8)
-> [(Word8, Word8)] -> UArray Word8 Word8
forall a b. (a -> b) -> a -> b
$
      [ (Word8 -> Word8 -> Word8
encodeHeader 0 Word8
n, 1) | Word8
n <- [0..0x1f] ]
   [(Word8, Word8)] -> [(Word8, Word8)] -> [(Word8, Word8)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 0 Word8
n, Word8
s) | (n :: Word8
n, s :: Word8
s) <- [Word8] -> [Word8] -> [(Word8, Word8)]
forall a b. [a] -> [b] -> [(a, b)]
zip [24..27] [2,3,5,9] ]

decodeTokenTypeTable :: Array Word8 TokenType
decodeTokenTypeTable :: Array Word8 TokenType
decodeTokenTypeTable =
  (Word8, Word8) -> [(Word8, TokenType)] -> Array Word8 TokenType
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
(i, i) -> [(i, e)] -> a i e
array (Word8
forall a. Bounded a => a
minBound, Word8
forall a. Bounded a => a
maxBound) ([(Word8, TokenType)] -> Array Word8 TokenType)
-> [(Word8, TokenType)] -> Array Word8 TokenType
forall a b. (a -> b) -> a -> b
$
    [ (Word8 -> Word8 -> Word8
encodeHeader 0 Word8
n,  TokenType
TypeUInt) | Word8
n <-  [0..26] ]
 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 0 27, TokenType
TypeUInt64)
    , (Word8 -> Word8 -> Word8
encodeHeader 0 31, TokenType
TypeInvalid) ]

 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 1 Word8
n,  TokenType
TypeNInt) | Word8
n <-  [0..26] ]
 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 1 27, TokenType
TypeNInt64)
    , (Word8 -> Word8 -> Word8
encodeHeader 1 31, TokenType
TypeInvalid) ]

 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 2 Word8
n,  TokenType
TypeBytes) | Word8
n <-  [0..27] ]
 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 2 31, TokenType
TypeBytesIndef) ]

 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 3 Word8
n,  TokenType
TypeString) | Word8
n <-  [0..27] ]
 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 3 31, TokenType
TypeStringIndef) ]

 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 4 Word8
n,  TokenType
TypeListLen) | Word8
n <-  [0..26] ]
 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 4 27, TokenType
TypeListLen64)
    , (Word8 -> Word8 -> Word8
encodeHeader 4 31, TokenType
TypeListLenIndef) ]

 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 5 Word8
n,  TokenType
TypeMapLen) | Word8
n <-  [0..26] ]
 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 5 27, TokenType
TypeMapLen64)
    , (Word8 -> Word8 -> Word8
encodeHeader 5 31, TokenType
TypeMapLenIndef) ]

 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 6 Word8
n,  TokenType
TypeTag) | Word8
n <- 0Word8 -> [Word8] -> [Word8]
forall a. a -> [a] -> [a]
:1Word8 -> [Word8] -> [Word8]
forall a. a -> [a] -> [a]
:[4..26] ]
 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 6 2,  TokenType
TypeInteger)
    , (Word8 -> Word8 -> Word8
encodeHeader 6 3,  TokenType
TypeInteger)
    , (Word8 -> Word8 -> Word8
encodeHeader 6 27, TokenType
TypeTag64)
    , (Word8 -> Word8 -> Word8
encodeHeader 6 31, TokenType
TypeInvalid) ]

 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 7 Word8
n,  TokenType
TypeSimple) | Word8
n <-  [0..19] ]
 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader 7 20, TokenType
TypeBool)
    , (Word8 -> Word8 -> Word8
encodeHeader 7 21, TokenType
TypeBool)
    , (Word8 -> Word8 -> Word8
encodeHeader 7 22, TokenType
TypeNull)
    , (Word8 -> Word8 -> Word8
encodeHeader 7 23, TokenType
TypeSimple)
    , (Word8 -> Word8 -> Word8
encodeHeader 7 24, TokenType
TypeSimple)
    , (Word8 -> Word8 -> Word8
encodeHeader 7 25, TokenType
TypeFloat16)
    , (Word8 -> Word8 -> Word8
encodeHeader 7 26, TokenType
TypeFloat32)
    , (Word8 -> Word8 -> Word8
encodeHeader 7 27, TokenType
TypeFloat64)
    , (Word8 -> Word8 -> Word8
encodeHeader 7 31, TokenType
TypeBreak) ]

 [(Word8, TokenType)]
-> [(Word8, TokenType)] -> [(Word8, TokenType)]
forall a. [a] -> [a] -> [a]
++ [ (Word8 -> Word8 -> Word8
encodeHeader Word8
mt Word8
n, TokenType
TypeInvalid) | Word8
mt <- [0..7], Word8
n <- [28..30] ]

encodeHeader :: Word8 -> Word8 -> Word8
encodeHeader :: Word8 -> Word8 -> Word8
encodeHeader mt :: Word8
mt ai :: Word8
ai = Word8
mt Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`shiftL` 5 Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
ai

data DecodedToken a = DecodedToken !Int !a | DecodeFailure
  deriving Int -> DecodedToken a -> ShowS
[DecodedToken a] -> ShowS
DecodedToken a -> String
(Int -> DecodedToken a -> ShowS)
-> (DecodedToken a -> String)
-> ([DecodedToken a] -> ShowS)
-> Show (DecodedToken a)
forall a. Show a => Int -> DecodedToken a -> ShowS
forall a. Show a => [DecodedToken a] -> ShowS
forall a. Show a => DecodedToken a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DecodedToken a] -> ShowS
$cshowList :: forall a. Show a => [DecodedToken a] -> ShowS
show :: DecodedToken a -> String
$cshow :: forall a. Show a => DecodedToken a -> String
showsPrec :: Int -> DecodedToken a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> DecodedToken a -> ShowS
Show
-- TODO add classification for DecodeFailure

-- | Note that canonicity information is calculated lazily. This way we don't
-- need to concern ourselves with two distinct paths, while according to
-- benchmarks it doesn't affect performance in the non-canonical case.
data LongToken a = Fits Bool {- canonical? -} !a
                 | TooLong Bool {- canonical? -} !Int
  deriving Int -> LongToken a -> ShowS
[LongToken a] -> ShowS
LongToken a -> String
(Int -> LongToken a -> ShowS)
-> (LongToken a -> String)
-> ([LongToken a] -> ShowS)
-> Show (LongToken a)
forall a. Show a => Int -> LongToken a -> ShowS
forall a. Show a => [LongToken a] -> ShowS
forall a. Show a => LongToken a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LongToken a] -> ShowS
$cshowList :: forall a. Show a => [LongToken a] -> ShowS
show :: LongToken a -> String
$cshow :: forall a. Show a => LongToken a -> String
showsPrec :: Int -> LongToken a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> LongToken a -> ShowS
Show

-- Canoncal NaN floats:
--
-- In these float/double canonical tests we check NaNs are canonical too.
-- There are lots of bit values representing NaN, for each of the flat types.
-- The rule from CBOR RFC 7049, section 3.9 is that the canonical NaN is the
-- CBOR term f97e00 which is the canonical half-float representation. We do
-- this by testing for the size being 3 (since tryConsumeFloat/Double only
-- return 3 when the header byte is 0xf9) and the 16 bytes being 0x7e00.

{-# INLINE isFloat16Canonical #-}
isFloat16Canonical :: Int -> BS.ByteString -> Float -> Bool
isFloat16Canonical :: Int -> ByteString -> Float -> Bool
isFloat16Canonical sz :: Int
sz bs :: ByteString
bs f :: Float
f
  | Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= 3   = Bool
False
  | Float -> Bool
forall a. RealFloat a => a -> Bool
isNaN Float
f   = ByteString -> Word16
eatTailWord16 ByteString
bs Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
== 0x7e00
  | Bool
otherwise = Bool
True

{-# INLINE isFloatCanonical #-}
isFloatCanonical :: Int -> BS.ByteString -> Float -> Bool
isFloatCanonical :: Int -> ByteString -> Float -> Bool
isFloatCanonical sz :: Int
sz bs :: ByteString
bs f :: Float
f
  | Float -> Bool
forall a. RealFloat a => a -> Bool
isNaN Float
f   = Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 3 Bool -> Bool -> Bool
&& ByteString -> Word16
eatTailWord16 ByteString
bs Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
== 0x7e00
  | Bool
otherwise = Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 5

{-# INLINE isDoubleCanonical #-}
isDoubleCanonical :: Int -> BS.ByteString -> Double -> Bool
isDoubleCanonical :: Int -> ByteString -> Double -> Bool
isDoubleCanonical sz :: Int
sz bs :: ByteString
bs f :: Double
f
  | Double -> Bool
forall a. RealFloat a => a -> Bool
isNaN Double
f   = Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 3 Bool -> Bool -> Bool
&& ByteString -> Word16
eatTailWord16 ByteString
bs Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
== 0x7e00
  | Bool
otherwise = Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 9

{-# INLINE isWordCanonical #-}
isWordCanonical :: Int -> Word# -> Bool
isWordCanonical :: Int -> Word# -> Bool
isWordCanonical sz :: Int
sz w# :: Word#
w#
  | Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 2   = Int# -> Bool
isTrue# (Word#
w# Word# -> Word# -> Int#
`gtWord#` 0x17##)
  | Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 3   = Int# -> Bool
isTrue# (Word#
w# Word# -> Word# -> Int#
`gtWord#` 0xff##)
  | Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 5   = Int# -> Bool
isTrue# (Word#
w# Word# -> Word# -> Int#
`gtWord#` 0xffff##)
  | Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 9   = Int# -> Bool
isTrue# (Word#
w# Word# -> Word# -> Int#
`gtWord#` 0xffffffff##)
  | Bool
otherwise = Bool
True

{-# INLINE isIntCanonical #-}
isIntCanonical :: Int -> Int# -> Bool
isIntCanonical :: Int -> Int# -> Bool
isIntCanonical sz :: Int
sz i# :: Int#
i#
  | Int# -> Bool
isTrue# (Int#
i# Int# -> Int# -> Int#
<# 0#) = Int -> Word# -> Bool
isWordCanonical Int
sz (Word# -> Word#
not# Word#
w#)
  | Bool
otherwise          = Int -> Word# -> Bool
isWordCanonical Int
sz       Word#
w#
  where
    w# :: Word#
w# = Int# -> Word#
int2Word# Int#
i#

#if defined(ARCH_32bit)
{-# INLINE isWord64Canonical #-}
isWord64Canonical :: Int -> Word64# -> Bool
isWord64Canonical sz w#
  | sz == 2   = isTrue# (w# `gtWord64#` wordToWord64# 0x17##)
  | sz == 3   = isTrue# (w# `gtWord64#` wordToWord64# 0xff##)
  | sz == 5   = isTrue# (w# `gtWord64#` wordToWord64# 0xffff##)
  | sz == 9   = isTrue# (w# `gtWord64#` wordToWord64# 0xffffffff##)
  | otherwise = True

{-# INLINE isInt64Canonical #-}
isInt64Canonical :: Int -> Int64# -> Bool
isInt64Canonical sz i#
  | isTrue# (i# `ltInt64#` intToInt64# 0#) = isWord64Canonical sz (not64# w#)
  | otherwise                              = isWord64Canonical sz         w#
  where
    w# = int64ToWord64# i#
#endif

{-# INLINE isSimpleCanonical #-}
isSimpleCanonical :: Int -> Word# -> Bool
isSimpleCanonical :: Int -> Word# -> Bool
isSimpleCanonical 2 w# :: Word#
w# = Int# -> Bool
isTrue# (Word#
w# Word# -> Word# -> Int#
`gtWord#` 0x17##)
isSimpleCanonical _ _  = Bool
True -- only size 1 and 2 are possible here


-- TODO FIXME: check with 7.10 and file ticket:
-- a case analysis against 0x00 .. 0xff :: Word8 turns into a huge chain
-- of >= tests. It could use a jump table, or at least it could use a binary
-- division. Whereas for Int or Word it does the right thing.

{-# INLINE tryConsumeWord #-}
tryConsumeWord :: Word8 -> ByteString -> DecodedToken Word
tryConsumeWord :: Word8 -> ByteString -> DecodedToken Word
tryConsumeWord hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of
  -- Positive integers (type 0)
  0x00 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 0
  0x01 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 1
  0x02 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 2
  0x03 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 3
  0x04 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 4
  0x05 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 5
  0x06 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 6
  0x07 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 7
  0x08 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 8
  0x09 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 9
  0x0a -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 10
  0x0b -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 11
  0x0c -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 12
  0x0d -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 13
  0x0e -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 14
  0x0f -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 15
  0x10 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 16
  0x11 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 17
  0x12 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 18
  0x13 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 19
  0x14 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 20
  0x15 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 21
  0x16 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 22
  0x17 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 23
  0x18 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 2 (Word -> DecodedToken Word) -> Word -> DecodedToken Word
forall a b. (a -> b) -> a -> b
$! Word8 -> Word
word8ToWord  (ByteString -> Word8
eatTailWord8 ByteString
bs)
  0x19 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 3 (Word -> DecodedToken Word) -> Word -> DecodedToken Word
forall a b. (a -> b) -> a -> b
$! Word16 -> Word
word16ToWord (ByteString -> Word16
eatTailWord16 ByteString
bs)
  0x1a -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 5 (Word -> DecodedToken Word) -> Word -> DecodedToken Word
forall a b. (a -> b) -> a -> b
$! Word32 -> Word
word32ToWord (ByteString -> Word32
eatTailWord32 ByteString
bs)
#if defined(ARCH_64bit)
  0x1b -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 9 (Word -> DecodedToken Word) -> Word -> DecodedToken Word
forall a b. (a -> b) -> a -> b
$! Word64 -> Word
word64ToWord (ByteString -> Word64
eatTailWord64 ByteString
bs)
#else
  0x1b -> case word64ToWord (eatTailWord64 bs) of
            Just n  -> DecodedToken 9 n
            Nothing -> DecodeFailure
#endif
  _    -> DecodedToken Word
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeNegWord #-}
tryConsumeNegWord :: Word8 -> ByteString -> DecodedToken Word
tryConsumeNegWord :: Word8 -> ByteString -> DecodedToken Word
tryConsumeNegWord hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of
  -- Positive integers (type 0)
  0x20 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 0
  0x21 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 1
  0x22 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 2
  0x23 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 3
  0x24 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 4
  0x25 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 5
  0x26 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 6
  0x27 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 7
  0x28 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 8
  0x29 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 9
  0x2a -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 10
  0x2b -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 11
  0x2c -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 12
  0x2d -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 13
  0x2e -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 14
  0x2f -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 15
  0x30 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 16
  0x31 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 17
  0x32 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 18
  0x33 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 19
  0x34 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 20
  0x35 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 21
  0x36 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 22
  0x37 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 23
  0x38 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 2 (Word -> DecodedToken Word) -> Word -> DecodedToken Word
forall a b. (a -> b) -> a -> b
$! (Word8 -> Word
word8ToWord  (ByteString -> Word8
eatTailWord8 ByteString
bs))
  0x39 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 3 (Word -> DecodedToken Word) -> Word -> DecodedToken Word
forall a b. (a -> b) -> a -> b
$! (Word16 -> Word
word16ToWord (ByteString -> Word16
eatTailWord16 ByteString
bs))
  0x3a -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 5 (Word -> DecodedToken Word) -> Word -> DecodedToken Word
forall a b. (a -> b) -> a -> b
$! (Word32 -> Word
word32ToWord (ByteString -> Word32
eatTailWord32 ByteString
bs))
#if defined(ARCH_64bit)
  0x3b -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 9 (Word -> DecodedToken Word) -> Word -> DecodedToken Word
forall a b. (a -> b) -> a -> b
$! (Word64 -> Word
word64ToWord (ByteString -> Word64
eatTailWord64 ByteString
bs))
#else
  0x3b -> case word64ToWord (eatTailWord64 bs) of
            Just n  -> DecodedToken 9 n
            Nothing -> DecodeFailure
#endif
  _    -> DecodedToken Word
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeInt #-}
tryConsumeInt :: Word8 -> ByteString -> DecodedToken Int
tryConsumeInt :: Word8 -> ByteString -> DecodedToken Int
tryConsumeInt hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of
  -- Positive integers (type 0)
  0x00 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 0
  0x01 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 1
  0x02 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 2
  0x03 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 3
  0x04 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 4
  0x05 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 5
  0x06 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 6
  0x07 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 7
  0x08 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 8
  0x09 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 9
  0x0a -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 10
  0x0b -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 11
  0x0c -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 12
  0x0d -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 13
  0x0e -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 14
  0x0f -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 15
  0x10 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 16
  0x11 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 17
  0x12 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 18
  0x13 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 19
  0x14 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 20
  0x15 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 21
  0x16 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 22
  0x17 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 23
  0x18 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 2 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (Word8 -> Int
word8ToInt  (ByteString -> Word8
eatTailWord8 ByteString
bs))
  0x19 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 3 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (Word16 -> Int
word16ToInt (ByteString -> Word16
eatTailWord16 ByteString
bs))
#if defined(ARCH_64bit)
  0x1a -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 5 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (Word32 -> Int
word32ToInt (ByteString -> Word32
eatTailWord32 ByteString
bs))
#else
  0x1a -> case word32ToInt (eatTailWord32 bs) of
            Just n  -> DecodedToken 5 n
            Nothing -> DecodeFailure
#endif
  0x1b -> case Word64 -> Maybe Int
word64ToInt (ByteString -> Word64
eatTailWord64 ByteString
bs) of
            Just n :: Int
n  -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 9 Int
n
            Nothing -> DecodedToken Int
forall a. DecodedToken a
DecodeFailure

  -- Negative integers (type 1)
  0x20 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-1)
  0x21 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-2)
  0x22 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-3)
  0x23 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-4)
  0x24 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-5)
  0x25 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-6)
  0x26 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-7)
  0x27 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-8)
  0x28 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-9)
  0x29 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-10)
  0x2a -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-11)
  0x2b -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-12)
  0x2c -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-13)
  0x2d -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-14)
  0x2e -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-15)
  0x2f -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-16)
  0x30 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-17)
  0x31 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-18)
  0x32 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-19)
  0x33 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-20)
  0x34 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-21)
  0x35 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-22)
  0x36 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-23)
  0x37 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-24)
  0x38 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 2 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (-1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Word8 -> Int
word8ToInt  (ByteString -> Word8
eatTailWord8 ByteString
bs))
  0x39 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 3 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (-1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Word16 -> Int
word16ToInt (ByteString -> Word16
eatTailWord16 ByteString
bs))
#if defined(ARCH_64bit)
  0x3a -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 5 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (-1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Word32 -> Int
word32ToInt (ByteString -> Word32
eatTailWord32 ByteString
bs))
#else
  0x3a -> case word32ToInt (eatTailWord32 bs) of
            Just n  -> DecodedToken 5 (-1 - n)
            Nothing -> DecodeFailure
#endif
  0x3b -> case Word64 -> Maybe Int
word64ToInt (ByteString -> Word64
eatTailWord64 ByteString
bs) of
            Just n :: Int
n  -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 9 (-1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n)
            Nothing -> DecodedToken Int
forall a. DecodedToken a
DecodeFailure
  _    -> DecodedToken Int
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeInteger #-}
tryConsumeInteger :: Word8 -> ByteString -> DecodedToken (BigIntToken Integer)
tryConsumeInteger :: Word8 -> ByteString -> DecodedToken (BigIntToken Integer)
tryConsumeInteger hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of

  -- Positive integers (type 0)
  0x00 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 0)
  0x01 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 1)
  0x02 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 2)
  0x03 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 3)
  0x04 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 4)
  0x05 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 5)
  0x06 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 6)
  0x07 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 7)
  0x08 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 8)
  0x09 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 9)
  0x0a -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 10)
  0x0b -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 11)
  0x0c -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 12)
  0x0d -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 13)
  0x0e -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 14)
  0x0f -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 15)
  0x10 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 16)
  0x11 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 17)
  0x12 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 18)
  0x13 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 19)
  0x14 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 20)
  0x15 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 21)
  0x16 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 22)
  0x17 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True 23)

  0x18 -> let !w :: Word8
w@(W8# w# :: Word#
w#) = ByteString -> Word8
eatTailWord8 ByteString
bs
              sz :: Int
sz = 2
          in Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken Int
sz (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken (Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w#)   (Integer -> BigIntToken Integer) -> Integer -> BigIntToken Integer
forall a b. (a -> b) -> a -> b
$! Word8 -> Integer
forall a. Integral a => a -> Integer
toInteger Word8
w)
  0x19 -> let !w :: Word16
w@(W16# w# :: Word#
w#) = ByteString -> Word16
eatTailWord16 ByteString
bs
              sz :: Int
sz = 3
          in Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken Int
sz (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken (Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w#)   (Integer -> BigIntToken Integer) -> Integer -> BigIntToken Integer
forall a b. (a -> b) -> a -> b
$! Word16 -> Integer
forall a. Integral a => a -> Integer
toInteger Word16
w)
  0x1a -> let !w :: Word32
w@(W32# w# :: Word#
w#) = ByteString -> Word32
eatTailWord32 ByteString
bs
              sz :: Int
sz = 5
          in Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken Int
sz (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken (Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w#)   (Integer -> BigIntToken Integer) -> Integer -> BigIntToken Integer
forall a b. (a -> b) -> a -> b
$! Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger Word32
w)
  0x1b -> let !w :: Word64
w@(W64# w# :: Word#
w#) = ByteString -> Word64
eatTailWord64 ByteString
bs
              sz :: Int
sz = 9
#if defined(ARCH_32bit)
          in DecodedToken sz (BigIntToken (isWord64Canonical sz w#) $! toInteger w)
#else
          in Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken Int
sz (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken (Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w#)   (Integer -> BigIntToken Integer) -> Integer -> BigIntToken Integer
forall a b. (a -> b) -> a -> b
$! Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
w)
#endif

  -- Negative integers (type 1)
  0x20 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-1))
  0x21 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-2))
  0x22 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-3))
  0x23 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-4))
  0x24 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-5))
  0x25 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-6))
  0x26 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-7))
  0x27 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-8))
  0x28 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-9))
  0x29 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-10))
  0x2a -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-11))
  0x2b -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-12))
  0x2c -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-13))
  0x2d -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-14))
  0x2e -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-15))
  0x2f -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-16))
  0x30 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-17))
  0x31 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-18))
  0x32 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-19))
  0x33 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-20))
  0x34 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-21))
  0x35 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-22))
  0x36 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-23))
  0x37 -> Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken Bool
True (-24))
  0x38 -> let !w :: Word8
w@(W8# w# :: Word#
w#) = ByteString -> Word8
eatTailWord8 ByteString
bs
              sz :: Int
sz = 2
          in Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken Int
sz (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken (Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w#)   (Integer -> BigIntToken Integer) -> Integer -> BigIntToken Integer
forall a b. (a -> b) -> a -> b
$! (-1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Word8 -> Integer
forall a. Integral a => a -> Integer
toInteger Word8
w))
  0x39 -> let !w :: Word16
w@(W16# w# :: Word#
w#) = ByteString -> Word16
eatTailWord16 ByteString
bs
              sz :: Int
sz = 3
          in Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken Int
sz (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken (Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w#)   (Integer -> BigIntToken Integer) -> Integer -> BigIntToken Integer
forall a b. (a -> b) -> a -> b
$! (-1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Word16 -> Integer
forall a. Integral a => a -> Integer
toInteger Word16
w))
  0x3a -> let !w :: Word32
w@(W32# w# :: Word#
w#) = ByteString -> Word32
eatTailWord32 ByteString
bs
              sz :: Int
sz = 5
          in Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken Int
sz (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken (Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w#)   (Integer -> BigIntToken Integer) -> Integer -> BigIntToken Integer
forall a b. (a -> b) -> a -> b
$! (-1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger Word32
w))
  0x3b -> let !w :: Word64
w@(W64# w# :: Word#
w#) = ByteString -> Word64
eatTailWord64 ByteString
bs
              sz :: Int
sz = 9
#if defined(ARCH_32bit)
          in DecodedToken sz (BigIntToken (isWord64Canonical sz w#) $! (-1 - toInteger w))
#else
          in Int -> BigIntToken Integer -> DecodedToken (BigIntToken Integer)
forall a. Int -> a -> DecodedToken a
DecodedToken Int
sz (Bool -> Integer -> BigIntToken Integer
forall a. Bool -> Integer -> BigIntToken a
BigIntToken (Int -> Word# -> Bool
isWordCanonical Int
sz Word#
w#)   (Integer -> BigIntToken Integer) -> Integer -> BigIntToken Integer
forall a b. (a -> b) -> a -> b
$! (-1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
w))
#endif

  0xc2 -> ByteString -> DecodedToken (BigIntToken Integer)
forall a. ByteString -> DecodedToken (BigIntToken a)
readBigUInt ByteString
bs
  0xc3 -> ByteString -> DecodedToken (BigIntToken Integer)
forall a. ByteString -> DecodedToken (BigIntToken a)
readBigNInt ByteString
bs

  _    -> DecodedToken (BigIntToken Integer)
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeBytes #-}
tryConsumeBytes :: Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeBytes :: Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeBytes hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of

  -- Bytes (type 2)
  0x40 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 0 ByteString
bs
  0x41 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 1 ByteString
bs
  0x42 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 2 ByteString
bs
  0x43 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 3 ByteString
bs
  0x44 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 4 ByteString
bs
  0x45 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 5 ByteString
bs
  0x46 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 6 ByteString
bs
  0x47 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 7 ByteString
bs
  0x48 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 8 ByteString
bs
  0x49 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 9 ByteString
bs
  0x4a -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 10 ByteString
bs
  0x4b -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 11 ByteString
bs
  0x4c -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 12 ByteString
bs
  0x4d -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 13 ByteString
bs
  0x4e -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 14 ByteString
bs
  0x4f -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 15 ByteString
bs
  0x50 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 16 ByteString
bs
  0x51 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 17 ByteString
bs
  0x52 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 18 ByteString
bs
  0x53 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 19 ByteString
bs
  0x54 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 20 ByteString
bs
  0x55 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 21 ByteString
bs
  0x56 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 22 ByteString
bs
  0x57 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 23 ByteString
bs
  0x58 -> ByteString -> DecodedToken (LongToken ByteString)
readBytes8  ByteString
bs
  0x59 -> ByteString -> DecodedToken (LongToken ByteString)
readBytes16 ByteString
bs
  0x5a -> ByteString -> DecodedToken (LongToken ByteString)
readBytes32 ByteString
bs
  0x5b -> ByteString -> DecodedToken (LongToken ByteString)
readBytes64 ByteString
bs
  _    -> DecodedToken (LongToken ByteString)
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeString #-}
tryConsumeString :: Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeString :: Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeString hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of

  -- Strings (type 3)
  0x60 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 0 ByteString
bs
  0x61 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 1 ByteString
bs
  0x62 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 2 ByteString
bs
  0x63 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 3 ByteString
bs
  0x64 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 4 ByteString
bs
  0x65 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 5 ByteString
bs
  0x66 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 6 ByteString
bs
  0x67 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 7 ByteString
bs
  0x68 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 8 ByteString
bs
  0x69 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 9 ByteString
bs
  0x6a -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 10 ByteString
bs
  0x6b -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 11 ByteString
bs
  0x6c -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 12 ByteString
bs
  0x6d -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 13 ByteString
bs
  0x6e -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 14 ByteString
bs
  0x6f -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 15 ByteString
bs
  0x70 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 16 ByteString
bs
  0x71 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 17 ByteString
bs
  0x72 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 18 ByteString
bs
  0x73 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 19 ByteString
bs
  0x74 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 20 ByteString
bs
  0x75 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 21 ByteString
bs
  0x76 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 22 ByteString
bs
  0x77 -> Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall 23 ByteString
bs
  0x78 -> ByteString -> DecodedToken (LongToken ByteString)
readBytes8  ByteString
bs
  0x79 -> ByteString -> DecodedToken (LongToken ByteString)
readBytes16 ByteString
bs
  0x7a -> ByteString -> DecodedToken (LongToken ByteString)
readBytes32 ByteString
bs
  0x7b -> ByteString -> DecodedToken (LongToken ByteString)
readBytes64 ByteString
bs
  _    -> DecodedToken (LongToken ByteString)
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeListLen #-}
tryConsumeListLen :: Word8 -> ByteString -> DecodedToken Int
tryConsumeListLen :: Word8 -> ByteString -> DecodedToken Int
tryConsumeListLen hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of
  -- List structures (type 4)
  0x80 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 0
  0x81 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 1
  0x82 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 2
  0x83 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 3
  0x84 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 4
  0x85 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 5
  0x86 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 6
  0x87 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 7
  0x88 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 8
  0x89 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 9
  0x8a -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 10
  0x8b -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 11
  0x8c -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 12
  0x8d -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 13
  0x8e -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 14
  0x8f -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 15
  0x90 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 16
  0x91 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 17
  0x92 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 18
  0x93 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 19
  0x94 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 20
  0x95 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 21
  0x96 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 22
  0x97 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 23
  0x98 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 2 (Word8 -> Int
word8ToInt  (ByteString -> Word8
eatTailWord8 ByteString
bs))
  0x99 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 3 (Word16 -> Int
word16ToInt (ByteString -> Word16
eatTailWord16 ByteString
bs))
#if defined(ARCH_64bit)
  0x9a -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 5 (Word32 -> Int
word32ToInt (ByteString -> Word32
eatTailWord32 ByteString
bs))
#else
  0x9a -> case word32ToInt (eatTailWord32 bs) of
            Just n  -> DecodedToken 5 n
            Nothing -> DecodeFailure
#endif
  0x9b -> case Word64 -> Maybe Int
word64ToInt (ByteString -> Word64
eatTailWord64 ByteString
bs) of
            Just n :: Int
n  -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 9 Int
n
            Nothing -> DecodedToken Int
forall a. DecodedToken a
DecodeFailure
  _    -> DecodedToken Int
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeMapLen #-}
tryConsumeMapLen :: Word8 -> ByteString -> DecodedToken Int
tryConsumeMapLen :: Word8 -> ByteString -> DecodedToken Int
tryConsumeMapLen hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of
  -- Map structures (type 5)
  0xa0 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 0
  0xa1 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 1
  0xa2 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 2
  0xa3 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 3
  0xa4 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 4
  0xa5 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 5
  0xa6 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 6
  0xa7 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 7
  0xa8 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 8
  0xa9 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 9
  0xaa -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 10
  0xab -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 11
  0xac -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 12
  0xad -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 13
  0xae -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 14
  0xaf -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 15
  0xb0 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 16
  0xb1 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 17
  0xb2 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 18
  0xb3 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 19
  0xb4 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 20
  0xb5 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 21
  0xb6 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 22
  0xb7 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 23
  0xb8 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 2 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (Word8 -> Int
word8ToInt  (ByteString -> Word8
eatTailWord8 ByteString
bs))
  0xb9 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 3 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (Word16 -> Int
word16ToInt (ByteString -> Word16
eatTailWord16 ByteString
bs))
#if defined(ARCH_64bit)
  0xba -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 5 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (Word32 -> Int
word32ToInt (ByteString -> Word32
eatTailWord32 ByteString
bs))
#else
  0xba -> case word32ToInt (eatTailWord32 bs) of
            Just n  -> DecodedToken 5 n
            Nothing -> DecodeFailure
#endif
  0xbb -> case Word64 -> Maybe Int
word64ToInt (ByteString -> Word64
eatTailWord64 ByteString
bs) of
            Just n :: Int
n  -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 9 Int
n
            Nothing -> DecodedToken Int
forall a. DecodedToken a
DecodeFailure
  _    -> DecodedToken Int
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeListLenIndef #-}
tryConsumeListLenIndef :: Word8 -> DecodedToken ()
tryConsumeListLenIndef :: Word8 -> DecodedToken ()
tryConsumeListLenIndef hdr :: Word8
hdr = case Word8 -> Word
word8ToWord Word8
hdr of
  0x9f -> Int -> () -> DecodedToken ()
forall a. Int -> a -> DecodedToken a
DecodedToken 1 ()
  _    -> DecodedToken ()
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeMapLenIndef #-}
tryConsumeMapLenIndef :: Word8 -> DecodedToken ()
tryConsumeMapLenIndef :: Word8 -> DecodedToken ()
tryConsumeMapLenIndef hdr :: Word8
hdr = case Word8 -> Word
word8ToWord Word8
hdr of
  0xbf -> Int -> () -> DecodedToken ()
forall a. Int -> a -> DecodedToken a
DecodedToken 1 ()
  _    -> DecodedToken ()
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeListLenOrIndef #-}
tryConsumeListLenOrIndef :: Word8 -> ByteString -> DecodedToken Int
tryConsumeListLenOrIndef :: Word8 -> ByteString -> DecodedToken Int
tryConsumeListLenOrIndef hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of

  -- List structures (type 4)
  0x80 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 0
  0x81 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 1
  0x82 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 2
  0x83 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 3
  0x84 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 4
  0x85 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 5
  0x86 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 6
  0x87 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 7
  0x88 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 8
  0x89 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 9
  0x8a -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 10
  0x8b -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 11
  0x8c -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 12
  0x8d -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 13
  0x8e -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 14
  0x8f -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 15
  0x90 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 16
  0x91 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 17
  0x92 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 18
  0x93 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 19
  0x94 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 20
  0x95 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 21
  0x96 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 22
  0x97 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 23
  0x98 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 2 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (Word8 -> Int
word8ToInt  (ByteString -> Word8
eatTailWord8 ByteString
bs))
  0x99 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 3 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (Word16 -> Int
word16ToInt (ByteString -> Word16
eatTailWord16 ByteString
bs))
#if defined(ARCH_64bit)
  0x9a -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 5 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (Word32 -> Int
word32ToInt (ByteString -> Word32
eatTailWord32 ByteString
bs))
#else
  0x9a -> case word32ToInt (eatTailWord32 bs) of
            Just n  -> DecodedToken 5 n
            Nothing -> DecodeFailure
#endif
  0x9b -> case Word64 -> Maybe Int
word64ToInt (ByteString -> Word64
eatTailWord64 ByteString
bs) of
            Just n :: Int
n  -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 9 Int
n
            Nothing -> DecodedToken Int
forall a. DecodedToken a
DecodeFailure
  0x9f -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-1) -- indefinite length
  _    -> DecodedToken Int
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeMapLenOrIndef #-}
tryConsumeMapLenOrIndef :: Word8 -> ByteString -> DecodedToken Int
tryConsumeMapLenOrIndef :: Word8 -> ByteString -> DecodedToken Int
tryConsumeMapLenOrIndef hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of

  -- Map structures (type 5)
  0xa0 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 0
  0xa1 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 1
  0xa2 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 2
  0xa3 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 3
  0xa4 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 4
  0xa5 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 5
  0xa6 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 6
  0xa7 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 7
  0xa8 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 8
  0xa9 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 9
  0xaa -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 10
  0xab -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 11
  0xac -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 12
  0xad -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 13
  0xae -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 14
  0xaf -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 15
  0xb0 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 16
  0xb1 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 17
  0xb2 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 18
  0xb3 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 19
  0xb4 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 20
  0xb5 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 21
  0xb6 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 22
  0xb7 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 23
  0xb8 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 2 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (Word8 -> Int
word8ToInt  (ByteString -> Word8
eatTailWord8 ByteString
bs))
  0xb9 -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 3 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (Word16 -> Int
word16ToInt (ByteString -> Word16
eatTailWord16 ByteString
bs))
#if defined(ARCH_64bit)
  0xba -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 5 (Int -> DecodedToken Int) -> Int -> DecodedToken Int
forall a b. (a -> b) -> a -> b
$! (Word32 -> Int
word32ToInt (ByteString -> Word32
eatTailWord32 ByteString
bs))
#else
  0xba -> case word32ToInt (eatTailWord32 bs) of
            Just n  -> DecodedToken 5 n
            Nothing -> DecodeFailure
#endif
  0xbb -> case Word64 -> Maybe Int
word64ToInt (ByteString -> Word64
eatTailWord64 ByteString
bs) of
            Just n :: Int
n  -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 9 Int
n
            Nothing -> DecodedToken Int
forall a. DecodedToken a
DecodeFailure
  0xbf -> Int -> Int -> DecodedToken Int
forall a. Int -> a -> DecodedToken a
DecodedToken 1 (-1) -- indefinite length
  _    -> DecodedToken Int
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeTag #-}
tryConsumeTag :: Word8 -> ByteString -> DecodedToken Word
tryConsumeTag :: Word8 -> ByteString -> DecodedToken Word
tryConsumeTag hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of

  -- Tagged values (type 6)
  0xc0 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 0
  0xc1 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 1
  0xc2 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 2
  0xc3 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 3
  0xc4 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 4
  0xc5 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 5
  0xc6 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 6
  0xc7 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 7
  0xc8 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 8
  0xc9 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 9
  0xca -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 10
  0xcb -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 11
  0xcc -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 12
  0xcd -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 13
  0xce -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 14
  0xcf -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 15
  0xd0 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 16
  0xd1 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 17
  0xd2 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 18
  0xd3 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 19
  0xd4 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 20
  0xd5 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 21
  0xd6 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 22
  0xd7 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 23
  0xd8 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 2 (Word -> DecodedToken Word) -> Word -> DecodedToken Word
forall a b. (a -> b) -> a -> b
$! (Word8 -> Word
word8ToWord  (ByteString -> Word8
eatTailWord8 ByteString
bs))
  0xd9 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 3 (Word -> DecodedToken Word) -> Word -> DecodedToken Word
forall a b. (a -> b) -> a -> b
$! (Word16 -> Word
word16ToWord (ByteString -> Word16
eatTailWord16 ByteString
bs))
  0xda -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 5 (Word -> DecodedToken Word) -> Word -> DecodedToken Word
forall a b. (a -> b) -> a -> b
$! (Word32 -> Word
word32ToWord (ByteString -> Word32
eatTailWord32 ByteString
bs))
#if defined(ARCH_64bit)
  0xdb -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 9 (Word -> DecodedToken Word) -> Word -> DecodedToken Word
forall a b. (a -> b) -> a -> b
$! (Word64 -> Word
word64ToWord (ByteString -> Word64
eatTailWord64 ByteString
bs))
#else
  0xdb -> case word64ToWord (eatTailWord64 bs) of
            Just n  -> DecodedToken 9 n
            Nothing -> DecodeFailure
#endif
  _    -> DecodedToken Word
forall a. DecodedToken a
DecodeFailure

--
-- 64-on-32 bit code paths
--

#if defined(ARCH_32bit)
tryConsumeWord64 :: Word8 -> ByteString -> DecodedToken Word64
tryConsumeWord64 hdr !bs = case word8ToWord hdr of
  -- Positive integers (type 0)
  0x00 -> DecodedToken 1 0
  0x01 -> DecodedToken 1 1
  0x02 -> DecodedToken 1 2
  0x03 -> DecodedToken 1 3
  0x04 -> DecodedToken 1 4
  0x05 -> DecodedToken 1 5
  0x06 -> DecodedToken 1 6
  0x07 -> DecodedToken 1 7
  0x08 -> DecodedToken 1 8
  0x09 -> DecodedToken 1 9
  0x0a -> DecodedToken 1 10
  0x0b -> DecodedToken 1 11
  0x0c -> DecodedToken 1 12
  0x0d -> DecodedToken 1 13
  0x0e -> DecodedToken 1 14
  0x0f -> DecodedToken 1 15
  0x10 -> DecodedToken 1 16
  0x11 -> DecodedToken 1 17
  0x12 -> DecodedToken 1 18
  0x13 -> DecodedToken 1 19
  0x14 -> DecodedToken 1 20
  0x15 -> DecodedToken 1 21
  0x16 -> DecodedToken 1 22
  0x17 -> DecodedToken 1 23
  0x18 -> DecodedToken 2 $! (word8ToWord64  (eatTailWord8  bs))
  0x19 -> DecodedToken 3 $! (word16ToWord64 (eatTailWord16 bs))
  0x1a -> DecodedToken 5 $! (word32ToWord64 (eatTailWord32 bs))
  0x1b -> DecodedToken 9 $!                 (eatTailWord64 bs)
  _    -> DecodeFailure
{-# INLINE tryConsumeWord64 #-}

tryConsumeNegWord64 :: Word8 -> ByteString -> DecodedToken Word64
tryConsumeNegWord64 hdr !bs = case word8ToWord hdr of
  -- Positive integers (type 0)
  0x20 -> DecodedToken 1 0
  0x21 -> DecodedToken 1 1
  0x22 -> DecodedToken 1 2
  0x23 -> DecodedToken 1 3
  0x24 -> DecodedToken 1 4
  0x25 -> DecodedToken 1 5
  0x26 -> DecodedToken 1 6
  0x27 -> DecodedToken 1 7
  0x28 -> DecodedToken 1 8
  0x29 -> DecodedToken 1 9
  0x2a -> DecodedToken 1 10
  0x2b -> DecodedToken 1 11
  0x2c -> DecodedToken 1 12
  0x2d -> DecodedToken 1 13
  0x2e -> DecodedToken 1 14
  0x2f -> DecodedToken 1 15
  0x30 -> DecodedToken 1 16
  0x31 -> DecodedToken 1 17
  0x32 -> DecodedToken 1 18
  0x33 -> DecodedToken 1 19
  0x34 -> DecodedToken 1 20
  0x35 -> DecodedToken 1 21
  0x36 -> DecodedToken 1 22
  0x37 -> DecodedToken 1 23
  0x38 -> DecodedToken 2 $! (word8ToWord64  (eatTailWord8  bs))
  0x39 -> DecodedToken 3 $! (word16ToWord64 (eatTailWord16 bs))
  0x3a -> DecodedToken 5 $! (word32ToWord64 (eatTailWord32 bs))
  0x3b -> DecodedToken 9 $!                 (eatTailWord64 bs)
  _    -> DecodeFailure
{-# INLINE tryConsumeNegWord64 #-}

tryConsumeInt64 :: Word8 -> ByteString -> DecodedToken Int64
tryConsumeInt64 hdr !bs = case word8ToWord hdr of
  -- Positive integers (type 0)
  0x00 -> DecodedToken 1 0
  0x01 -> DecodedToken 1 1
  0x02 -> DecodedToken 1 2
  0x03 -> DecodedToken 1 3
  0x04 -> DecodedToken 1 4
  0x05 -> DecodedToken 1 5
  0x06 -> DecodedToken 1 6
  0x07 -> DecodedToken 1 7
  0x08 -> DecodedToken 1 8
  0x09 -> DecodedToken 1 9
  0x0a -> DecodedToken 1 10
  0x0b -> DecodedToken 1 11
  0x0c -> DecodedToken 1 12
  0x0d -> DecodedToken 1 13
  0x0e -> DecodedToken 1 14
  0x0f -> DecodedToken 1 15
  0x10 -> DecodedToken 1 16
  0x11 -> DecodedToken 1 17
  0x12 -> DecodedToken 1 18
  0x13 -> DecodedToken 1 19
  0x14 -> DecodedToken 1 20
  0x15 -> DecodedToken 1 21
  0x16 -> DecodedToken 1 22
  0x17 -> DecodedToken 1 23
  0x18 -> DecodedToken 2 $! (word8ToInt64  (eatTailWord8  bs))
  0x19 -> DecodedToken 3 $! (word16ToInt64 (eatTailWord16 bs))
  0x1a -> DecodedToken 5 $! (word32ToInt64 (eatTailWord32 bs))
  0x1b -> case word64ToInt64 (eatTailWord64 bs) of
            Just n  -> DecodedToken 9 n
            Nothing -> DecodeFailure

  -- Negative integers (type 1)
  0x20 -> DecodedToken 1 (-1)
  0x21 -> DecodedToken 1 (-2)
  0x22 -> DecodedToken 1 (-3)
  0x23 -> DecodedToken 1 (-4)
  0x24 -> DecodedToken 1 (-5)
  0x25 -> DecodedToken 1 (-6)
  0x26 -> DecodedToken 1 (-7)
  0x27 -> DecodedToken 1 (-8)
  0x28 -> DecodedToken 1 (-9)
  0x29 -> DecodedToken 1 (-10)
  0x2a -> DecodedToken 1 (-11)
  0x2b -> DecodedToken 1 (-12)
  0x2c -> DecodedToken 1 (-13)
  0x2d -> DecodedToken 1 (-14)
  0x2e -> DecodedToken 1 (-15)
  0x2f -> DecodedToken 1 (-16)
  0x30 -> DecodedToken 1 (-17)
  0x31 -> DecodedToken 1 (-18)
  0x32 -> DecodedToken 1 (-19)
  0x33 -> DecodedToken 1 (-20)
  0x34 -> DecodedToken 1 (-21)
  0x35 -> DecodedToken 1 (-22)
  0x36 -> DecodedToken 1 (-23)
  0x37 -> DecodedToken 1 (-24)
  0x38 -> DecodedToken 2 $! (-1 - word8ToInt64  (eatTailWord8  bs))
  0x39 -> DecodedToken 3 $! (-1 - word16ToInt64 (eatTailWord16 bs))
  0x3a -> DecodedToken 5 $! (-1 - word32ToInt64 (eatTailWord32 bs))
  0x3b -> case word64ToInt64 (eatTailWord64 bs) of
            Just n  -> DecodedToken 9 (-1 - n)
            Nothing -> DecodeFailure
  _    -> DecodeFailure
{-# INLINE tryConsumeInt64 #-}

tryConsumeListLen64 :: Word8 -> ByteString -> DecodedToken Int64
tryConsumeListLen64 hdr !bs = case word8ToWord hdr of
  -- List structures (type 4)
  0x80 -> DecodedToken 1 0
  0x81 -> DecodedToken 1 1
  0x82 -> DecodedToken 1 2
  0x83 -> DecodedToken 1 3
  0x84 -> DecodedToken 1 4
  0x85 -> DecodedToken 1 5
  0x86 -> DecodedToken 1 6
  0x87 -> DecodedToken 1 7
  0x88 -> DecodedToken 1 8
  0x89 -> DecodedToken 1 9
  0x8a -> DecodedToken 1 10
  0x8b -> DecodedToken 1 11
  0x8c -> DecodedToken 1 12
  0x8d -> DecodedToken 1 13
  0x8e -> DecodedToken 1 14
  0x8f -> DecodedToken 1 15
  0x90 -> DecodedToken 1 16
  0x91 -> DecodedToken 1 17
  0x92 -> DecodedToken 1 18
  0x93 -> DecodedToken 1 19
  0x94 -> DecodedToken 1 20
  0x95 -> DecodedToken 1 21
  0x96 -> DecodedToken 1 22
  0x97 -> DecodedToken 1 23
  0x98 -> DecodedToken 2 $! (word8ToInt64  (eatTailWord8  bs))
  0x99 -> DecodedToken 3 $! (word16ToInt64 (eatTailWord16 bs))
  0x9a -> DecodedToken 5 $! (word32ToInt64 (eatTailWord32 bs))
  0x9b -> case word64ToInt64 (eatTailWord64 bs) of
            Just n  -> DecodedToken 9 n
            Nothing -> DecodeFailure
  _    -> DecodeFailure
{-# INLINE tryConsumeListLen64 #-}

tryConsumeMapLen64 :: Word8 -> ByteString -> DecodedToken Int64
tryConsumeMapLen64 hdr !bs = case word8ToWord hdr of
  -- Map structures (type 5)
  0xa0 -> DecodedToken 1 0
  0xa1 -> DecodedToken 1 1
  0xa2 -> DecodedToken 1 2
  0xa3 -> DecodedToken 1 3
  0xa4 -> DecodedToken 1 4
  0xa5 -> DecodedToken 1 5
  0xa6 -> DecodedToken 1 6
  0xa7 -> DecodedToken 1 7
  0xa8 -> DecodedToken 1 8
  0xa9 -> DecodedToken 1 9
  0xaa -> DecodedToken 1 10
  0xab -> DecodedToken 1 11
  0xac -> DecodedToken 1 12
  0xad -> DecodedToken 1 13
  0xae -> DecodedToken 1 14
  0xaf -> DecodedToken 1 15
  0xb0 -> DecodedToken 1 16
  0xb1 -> DecodedToken 1 17
  0xb2 -> DecodedToken 1 18
  0xb3 -> DecodedToken 1 19
  0xb4 -> DecodedToken 1 20
  0xb5 -> DecodedToken 1 21
  0xb6 -> DecodedToken 1 22
  0xb7 -> DecodedToken 1 23
  0xb8 -> DecodedToken 2 $! (word8ToInt64  (eatTailWord8  bs))
  0xb9 -> DecodedToken 3 $! (word16ToInt64 (eatTailWord16 bs))
  0xba -> DecodedToken 5 $! (word32ToInt64 (eatTailWord32 bs))
  0xbb -> case word64ToInt64 (eatTailWord64 bs) of
            Just n  -> DecodedToken 9 n
            Nothing -> DecodeFailure
  _    -> DecodeFailure
{-# INLINE tryConsumeMapLen64 #-}

tryConsumeTag64 :: Word8 -> ByteString -> DecodedToken Word64
tryConsumeTag64 hdr !bs = case word8ToWord hdr of
  -- Tagged values (type 6)
  0xc0 -> DecodedToken 1 0
  0xc1 -> DecodedToken 1 1
  0xc2 -> DecodedToken 1 2
  0xc3 -> DecodedToken 1 3
  0xc4 -> DecodedToken 1 4
  0xc5 -> DecodedToken 1 5
  0xc6 -> DecodedToken 1 6
  0xc7 -> DecodedToken 1 7
  0xc8 -> DecodedToken 1 8
  0xc9 -> DecodedToken 1 9
  0xca -> DecodedToken 1 10
  0xcb -> DecodedToken 1 11
  0xcc -> DecodedToken 1 12
  0xcd -> DecodedToken 1 13
  0xce -> DecodedToken 1 14
  0xcf -> DecodedToken 1 15
  0xd0 -> DecodedToken 1 16
  0xd1 -> DecodedToken 1 17
  0xd2 -> DecodedToken 1 18
  0xd3 -> DecodedToken 1 19
  0xd4 -> DecodedToken 1 20
  0xd5 -> DecodedToken 1 21
  0xd6 -> DecodedToken 1 22
  0xd7 -> DecodedToken 1 23
  0xd8 -> DecodedToken 2 $! (word8ToWord64  (eatTailWord8  bs))
  0xd9 -> DecodedToken 3 $! (word16ToWord64 (eatTailWord16 bs))
  0xda -> DecodedToken 5 $! (word32ToWord64 (eatTailWord32 bs))
  0xdb -> DecodedToken 9 $!                 (eatTailWord64 bs)
  _    -> DecodeFailure
{-# INLINE tryConsumeTag64 #-}
#endif

{-# INLINE tryConsumeFloat #-}
tryConsumeFloat :: Word8 -> ByteString -> DecodedToken Float
tryConsumeFloat :: Word8 -> ByteString -> DecodedToken Float
tryConsumeFloat hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of
  0xf9 -> Int -> Float -> DecodedToken Float
forall a. Int -> a -> DecodedToken a
DecodedToken 3 (Float -> DecodedToken Float) -> Float -> DecodedToken Float
forall a b. (a -> b) -> a -> b
$! (Word16 -> Float
wordToFloat16 (ByteString -> Word16
eatTailWord16 ByteString
bs))
  0xfa -> Int -> Float -> DecodedToken Float
forall a. Int -> a -> DecodedToken a
DecodedToken 5 (Float -> DecodedToken Float) -> Float -> DecodedToken Float
forall a b. (a -> b) -> a -> b
$! (Word32 -> Float
wordToFloat32 (ByteString -> Word32
eatTailWord32 ByteString
bs))
  _    -> DecodedToken Float
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeDouble #-}
tryConsumeDouble :: Word8 -> ByteString -> DecodedToken Double
tryConsumeDouble :: Word8 -> ByteString -> DecodedToken Double
tryConsumeDouble hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of
  0xf9 -> Int -> Double -> DecodedToken Double
forall a. Int -> a -> DecodedToken a
DecodedToken 3 (Double -> DecodedToken Double) -> Double -> DecodedToken Double
forall a b. (a -> b) -> a -> b
$! (Float -> Double
float2Double (Float -> Double) -> Float -> Double
forall a b. (a -> b) -> a -> b
$ Word16 -> Float
wordToFloat16 (ByteString -> Word16
eatTailWord16 ByteString
bs))
  0xfa -> Int -> Double -> DecodedToken Double
forall a. Int -> a -> DecodedToken a
DecodedToken 5 (Double -> DecodedToken Double) -> Double -> DecodedToken Double
forall a b. (a -> b) -> a -> b
$! (Float -> Double
float2Double (Float -> Double) -> Float -> Double
forall a b. (a -> b) -> a -> b
$ Word32 -> Float
wordToFloat32 (ByteString -> Word32
eatTailWord32 ByteString
bs))
  0xfb -> Int -> Double -> DecodedToken Double
forall a. Int -> a -> DecodedToken a
DecodedToken 9 (Double -> DecodedToken Double) -> Double -> DecodedToken Double
forall a b. (a -> b) -> a -> b
$!                (Word64 -> Double
wordToFloat64 (ByteString -> Word64
eatTailWord64 ByteString
bs))
  _    -> DecodedToken Double
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeBool #-}
tryConsumeBool :: Word8 -> DecodedToken Bool
tryConsumeBool :: Word8 -> DecodedToken Bool
tryConsumeBool hdr :: Word8
hdr = case Word8 -> Word
word8ToWord Word8
hdr of
  0xf4 -> Int -> Bool -> DecodedToken Bool
forall a. Int -> a -> DecodedToken a
DecodedToken 1 Bool
False
  0xf5 -> Int -> Bool -> DecodedToken Bool
forall a. Int -> a -> DecodedToken a
DecodedToken 1 Bool
True
  _    -> DecodedToken Bool
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeSimple #-}
tryConsumeSimple :: Word8 -> ByteString -> DecodedToken Word
tryConsumeSimple :: Word8 -> ByteString -> DecodedToken Word
tryConsumeSimple hdr :: Word8
hdr !ByteString
bs = case Word8 -> Word
word8ToWord Word8
hdr of

  -- Simple and floats (type 7)
  0xe0 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 0
  0xe1 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 1
  0xe2 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 2
  0xe3 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 3
  0xe4 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 4
  0xe5 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 5
  0xe6 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 6
  0xe7 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 7
  0xe8 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 8
  0xe9 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 9
  0xea -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 10
  0xeb -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 11
  0xec -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 12
  0xed -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 13
  0xee -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 14
  0xef -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 15
  0xf0 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 16
  0xf1 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 17
  0xf2 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 18
  0xf3 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 19
  0xf4 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 20
  0xf5 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 21
  0xf6 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 22
  0xf7 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 1 23
  0xf8 -> Int -> Word -> DecodedToken Word
forall a. Int -> a -> DecodedToken a
DecodedToken 2 (Word -> DecodedToken Word) -> Word -> DecodedToken Word
forall a b. (a -> b) -> a -> b
$! (Word8 -> Word
word8ToWord (ByteString -> Word8
eatTailWord8 ByteString
bs))
  _    -> DecodedToken Word
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeBytesIndef #-}
tryConsumeBytesIndef :: Word8 -> DecodedToken ()
tryConsumeBytesIndef :: Word8 -> DecodedToken ()
tryConsumeBytesIndef hdr :: Word8
hdr = case Word8 -> Word
word8ToWord Word8
hdr of
  0x5f -> Int -> () -> DecodedToken ()
forall a. Int -> a -> DecodedToken a
DecodedToken 1 ()
  _    -> DecodedToken ()
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeStringIndef #-}
tryConsumeStringIndef :: Word8 -> DecodedToken ()
tryConsumeStringIndef :: Word8 -> DecodedToken ()
tryConsumeStringIndef hdr :: Word8
hdr = case Word8 -> Word
word8ToWord Word8
hdr of
  0x7f -> Int -> () -> DecodedToken ()
forall a. Int -> a -> DecodedToken a
DecodedToken 1 ()
  _    -> DecodedToken ()
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeNull #-}
tryConsumeNull :: Word8 -> DecodedToken ()
tryConsumeNull :: Word8 -> DecodedToken ()
tryConsumeNull hdr :: Word8
hdr = case Word8 -> Word
word8ToWord Word8
hdr of
  0xf6 -> Int -> () -> DecodedToken ()
forall a. Int -> a -> DecodedToken a
DecodedToken 1 ()
  _    -> DecodedToken ()
forall a. DecodedToken a
DecodeFailure


{-# INLINE tryConsumeBreakOr #-}
tryConsumeBreakOr :: Word8 -> DecodedToken ()
tryConsumeBreakOr :: Word8 -> DecodedToken ()
tryConsumeBreakOr hdr :: Word8
hdr = case Word8 -> Word
word8ToWord Word8
hdr of
  0xff -> Int -> () -> DecodedToken ()
forall a. Int -> a -> DecodedToken a
DecodedToken 1 ()
  _    -> DecodedToken ()
forall a. DecodedToken a
DecodeFailure

{-# INLINE readBytesSmall #-}
readBytesSmall :: Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall :: Int -> ByteString -> DecodedToken (LongToken ByteString)
readBytesSmall n :: Int
n bs :: ByteString
bs
  -- if n <= bound then ok return it all
  | Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
hdrsz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteString -> Int
BS.length ByteString
bs
  = Int -> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a. Int -> a -> DecodedToken a
DecodedToken (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
hdrsz) (LongToken ByteString -> DecodedToken (LongToken ByteString))
-> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a b. (a -> b) -> a -> b
$ Bool -> ByteString -> LongToken ByteString
forall a. Bool -> a -> LongToken a
Fits Bool
True (ByteString -> LongToken ByteString)
-> ByteString -> LongToken ByteString
forall a b. (a -> b) -> a -> b
$
      Int -> ByteString -> ByteString
BS.unsafeTake Int
n (Int -> ByteString -> ByteString
BS.unsafeDrop Int
hdrsz ByteString
bs)

  -- if n > bound then slow path, multi-chunk
  | Bool
otherwise
  = Int -> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a. Int -> a -> DecodedToken a
DecodedToken Int
hdrsz (LongToken ByteString -> DecodedToken (LongToken ByteString))
-> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a b. (a -> b) -> a -> b
$ Bool -> Int -> LongToken ByteString
forall a. Bool -> Int -> LongToken a
TooLong Bool
True Int
n
  where
    hdrsz :: Int
hdrsz = 1

{-# INLINE readBytes8 #-}
{-# INLINE readBytes16 #-}
{-# INLINE readBytes32 #-}
{-# INLINE readBytes64 #-}
readBytes8, readBytes16, readBytes32, readBytes64
  :: ByteString -> DecodedToken (LongToken ByteString)

readBytes8 :: ByteString -> DecodedToken (LongToken ByteString)
readBytes8 bs :: ByteString
bs
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
hdrsz
  = Int -> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a. Int -> a -> DecodedToken a
DecodedToken (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
hdrsz) (LongToken ByteString -> DecodedToken (LongToken ByteString))
-> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a b. (a -> b) -> a -> b
$ Bool -> ByteString -> LongToken ByteString
forall a. Bool -> a -> LongToken a
Fits Bool
lengthCanonical (ByteString -> LongToken ByteString)
-> ByteString -> LongToken ByteString
forall a b. (a -> b) -> a -> b
$
      Int -> ByteString -> ByteString
BS.unsafeTake Int
n (Int -> ByteString -> ByteString
BS.unsafeDrop Int
hdrsz ByteString
bs)

  -- if n > bound then slow path, multi-chunk
  | Bool
otherwise
  = Int -> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a. Int -> a -> DecodedToken a
DecodedToken Int
hdrsz (LongToken ByteString -> DecodedToken (LongToken ByteString))
-> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a b. (a -> b) -> a -> b
$ Bool -> Int -> LongToken ByteString
forall a. Bool -> Int -> LongToken a
TooLong Bool
lengthCanonical Int
n
  where
    hdrsz :: Int
hdrsz           = 2
    !n :: Int
n@(I# n# :: Int#
n#)      = Word8 -> Int
word8ToInt (ByteString -> Word8
eatTailWord8 ByteString
bs)
    lengthCanonical :: Bool
lengthCanonical = Int -> Int# -> Bool
isIntCanonical Int
hdrsz Int#
n#

readBytes16 :: ByteString -> DecodedToken (LongToken ByteString)
readBytes16 bs :: ByteString
bs
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
hdrsz
  = Int -> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a. Int -> a -> DecodedToken a
DecodedToken (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
hdrsz) (LongToken ByteString -> DecodedToken (LongToken ByteString))
-> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a b. (a -> b) -> a -> b
$ Bool -> ByteString -> LongToken ByteString
forall a. Bool -> a -> LongToken a
Fits Bool
lengthCanonical (ByteString -> LongToken ByteString)
-> ByteString -> LongToken ByteString
forall a b. (a -> b) -> a -> b
$
      Int -> ByteString -> ByteString
BS.unsafeTake Int
n (Int -> ByteString -> ByteString
BS.unsafeDrop Int
hdrsz ByteString
bs)

  -- if n > bound then slow path, multi-chunk
  | Bool
otherwise
  = Int -> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a. Int -> a -> DecodedToken a
DecodedToken Int
hdrsz (LongToken ByteString -> DecodedToken (LongToken ByteString))
-> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a b. (a -> b) -> a -> b
$ Bool -> Int -> LongToken ByteString
forall a. Bool -> Int -> LongToken a
TooLong Bool
lengthCanonical Int
n
  where
    hdrsz :: Int
hdrsz           = 3
    !n :: Int
n@(I# n# :: Int#
n#)      = Word16 -> Int
word16ToInt (ByteString -> Word16
eatTailWord16 ByteString
bs)
    lengthCanonical :: Bool
lengthCanonical = Int -> Int# -> Bool
isIntCanonical Int
hdrsz Int#
n#

readBytes32 :: ByteString -> DecodedToken (LongToken ByteString)
readBytes32 bs :: ByteString
bs = case Word32 -> Int
word32ToInt (ByteString -> Word32
eatTailWord32 ByteString
bs) of
#if defined(ARCH_32bit)
    Just n@(I# n#)
#else
    n :: Int
n@(I# n# :: Int#
n#)
#endif
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
hdrsz
                  -> Int -> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a. Int -> a -> DecodedToken a
DecodedToken (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
hdrsz) (LongToken ByteString -> DecodedToken (LongToken ByteString))
-> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a b. (a -> b) -> a -> b
$ Bool -> ByteString -> LongToken ByteString
forall a. Bool -> a -> LongToken a
Fits (Int -> Int# -> Bool
isIntCanonical Int
hdrsz Int#
n#) (ByteString -> LongToken ByteString)
-> ByteString -> LongToken ByteString
forall a b. (a -> b) -> a -> b
$
                       Int -> ByteString -> ByteString
BS.unsafeTake Int
n (Int -> ByteString -> ByteString
BS.unsafeDrop Int
hdrsz ByteString
bs)

      -- if n > bound then slow path, multi-chunk
      | Bool
otherwise -> Int -> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a. Int -> a -> DecodedToken a
DecodedToken Int
hdrsz (LongToken ByteString -> DecodedToken (LongToken ByteString))
-> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a b. (a -> b) -> a -> b
$ Bool -> Int -> LongToken ByteString
forall a. Bool -> Int -> LongToken a
TooLong (Int -> Int# -> Bool
isIntCanonical Int
hdrsz Int#
n#) Int
n

#if defined(ARCH_32bit)
    Nothing       -> DecodeFailure
#endif
  where
    hdrsz :: Int
hdrsz = 5

readBytes64 :: ByteString -> DecodedToken (LongToken ByteString)
readBytes64 bs :: ByteString
bs = case Word64 -> Maybe Int
word64ToInt (ByteString -> Word64
eatTailWord64 ByteString
bs) of
    Just n :: Int
n@(I# n# :: Int#
n#)
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= ByteString -> Int
BS.length ByteString
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
hdrsz
                  -> Int -> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a. Int -> a -> DecodedToken a
DecodedToken (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
hdrsz) (LongToken ByteString -> DecodedToken (LongToken ByteString))
-> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a b. (a -> b) -> a -> b
$ Bool -> ByteString -> LongToken ByteString
forall a. Bool -> a -> LongToken a
Fits (Int -> Int# -> Bool
isIntCanonical Int
hdrsz Int#
n#) (ByteString -> LongToken ByteString)
-> ByteString -> LongToken ByteString
forall a b. (a -> b) -> a -> b
$
                            Int -> ByteString -> ByteString
BS.unsafeTake Int
n (Int -> ByteString -> ByteString
BS.unsafeDrop Int
hdrsz ByteString
bs)

      -- if n > bound then slow path, multi-chunk
      | Bool
otherwise -> Int -> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a. Int -> a -> DecodedToken a
DecodedToken Int
hdrsz (LongToken ByteString -> DecodedToken (LongToken ByteString))
-> LongToken ByteString -> DecodedToken (LongToken ByteString)
forall a b. (a -> b) -> a -> b
$ Bool -> Int -> LongToken ByteString
forall a. Bool -> Int -> LongToken a
TooLong (Int -> Int# -> Bool
isIntCanonical Int
hdrsz Int#
n#) Int
n

    Nothing       -> DecodedToken (LongToken ByteString)
forall a. DecodedToken a
DecodeFailure
  where
    hdrsz :: Int
hdrsz = 9

------------------------------------------------------------------------------
-- Reading big integers
--

-- Big ints consist of two CBOR tokens: a tag token (2 for positive, 3 for
-- negative) followed by a bytes token. Our usual invariant (for go_fast and
-- go_fast_end) only guarantees that we've got enough space to decode the
-- first token. So given that there's two tokens and the second is variable
-- length then there are several points where we can discover we're out of
-- input buffer space.
--
-- In those cases we need to break out of the fast path but we must arrange
-- things so that we can continue later once we've got more input buffer.
--
-- In particular, we might run out of space when:
--   1. trying to decode the header of the second token (bytes); or
--   2. trying to read the bytes body
--
--- The existing mechanisms we've got to drop out of the fast path are:
--   * SlowDecodeAction to re-read a whole token
--   * SlowConsumeTokenBytes to read the body of a bytes token
--
-- Of course when we resume we need to convert the bytes into an integer.
-- Rather than making new fast path return mechanisms we can reuse the
-- existing ones, so long as we're prepared to allocate new continuation
-- closures. This seems a reasonable price to pay to reduce complexity since
-- decoding a big int across an input buffer boundary ought to be rare, and
-- allocating a new continuation closure isn't that expensive.
--
-- Note that canonicity information is calculated lazily. This way we don't need
-- to concern ourselves with two distinct paths, while according to benchmarks
-- it doesn't affect performance in the non-canonical case.

data BigIntToken a = BigIntToken     Bool {- canonical? -} Integer
                   | BigUIntNeedBody Bool {- canonical? -} Int
                   | BigNIntNeedBody Bool {- canonical? -} Int
                   | BigUIntNeedHeader
                   | BigNIntNeedHeader

-- So when we have to break out because we can't read the whole bytes body
-- in one go then we need to use SlowConsumeTokenBytes but we can adjust the
-- continuation so that when we get the ByteString back we convert it to an
-- Integer before calling the original continuation.

adjustContBigUIntNeedBody, adjustContBigNIntNeedBody
  :: (Integer -> ST s (DecodeAction s a))
  -> (ByteString -> ST s (DecodeAction s a))

adjustContBigUIntNeedBody :: (Integer -> ST s (DecodeAction s a))
-> ByteString -> ST s (DecodeAction s a)
adjustContBigUIntNeedBody k :: Integer -> ST s (DecodeAction s a)
k = \bs :: ByteString
bs -> Integer -> ST s (DecodeAction s a)
k (Integer -> ST s (DecodeAction s a))
-> Integer -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> Integer
uintegerFromBytes ByteString
bs
adjustContBigNIntNeedBody :: (Integer -> ST s (DecodeAction s a))
-> ByteString -> ST s (DecodeAction s a)
adjustContBigNIntNeedBody k :: Integer -> ST s (DecodeAction s a)
k = \bs :: ByteString
bs -> Integer -> ST s (DecodeAction s a)
k (Integer -> ST s (DecodeAction s a))
-> Integer -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> Integer
nintegerFromBytes ByteString
bs

adjustContCanonicalBigUIntNeedBody, adjustContCanonicalBigNIntNeedBody
  :: (Integer -> ST s (DecodeAction s a))
  -> (ByteString -> ST s (DecodeAction s a))

adjustContCanonicalBigUIntNeedBody :: (Integer -> ST s (DecodeAction s a))
-> ByteString -> ST s (DecodeAction s a)
adjustContCanonicalBigUIntNeedBody k :: Integer -> ST s (DecodeAction s a)
k = \bs :: ByteString
bs ->
  if ByteString -> Bool
isBigIntRepCanonical ByteString
bs
  then Integer -> ST s (DecodeAction s a)
k (Integer -> ST s (DecodeAction s a))
-> Integer -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> Integer
uintegerFromBytes ByteString
bs
  else DecodeAction s a -> ST s (DecodeAction s a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DecodeAction s a -> ST s (DecodeAction s a))
-> DecodeAction s a -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! String -> DecodeAction s a
forall s a. String -> DecodeAction s a
D.Fail ("non-canonical integer")

adjustContCanonicalBigNIntNeedBody :: (Integer -> ST s (DecodeAction s a))
-> ByteString -> ST s (DecodeAction s a)
adjustContCanonicalBigNIntNeedBody k :: Integer -> ST s (DecodeAction s a)
k = \bs :: ByteString
bs ->
  if ByteString -> Bool
isBigIntRepCanonical ByteString
bs
  then Integer -> ST s (DecodeAction s a)
k (Integer -> ST s (DecodeAction s a))
-> Integer -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> Integer
nintegerFromBytes ByteString
bs
  else DecodeAction s a -> ST s (DecodeAction s a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DecodeAction s a -> ST s (DecodeAction s a))
-> DecodeAction s a -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! String -> DecodeAction s a
forall s a. String -> DecodeAction s a
D.Fail ("non-canonical integer")

-- And when we have to break out because we can't read the bytes token header
-- in one go then we need to use SlowDecodeAction but we have to make two
-- adjustments. When we resume we need to read a bytes token, not a big int.
-- That is we don't want to re-read the tag token. Indeed we cannot even if we
-- wanted to because the slow path code only guarantees to arrange for one
-- complete token header in the input buffer. So we must pretend that we did
-- in fact want to read a bytes token using ConsumeBytes, and then we can
-- adjust the continuation for that in the same way as above.

adjustContBigUIntNeedHeader, adjustContBigNIntNeedHeader
  :: (Integer -> ST s (DecodeAction s a))
  -> DecodeAction s a

adjustContBigUIntNeedHeader :: (Integer -> ST s (DecodeAction s a)) -> DecodeAction s a
adjustContBigUIntNeedHeader k :: Integer -> ST s (DecodeAction s a)
k = (ByteString -> ST s (DecodeAction s a)) -> DecodeAction s a
forall s a.
(ByteString -> ST s (DecodeAction s a)) -> DecodeAction s a
ConsumeBytes (\bs :: ByteString
bs -> Integer -> ST s (DecodeAction s a)
k (Integer -> ST s (DecodeAction s a))
-> Integer -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> Integer
uintegerFromBytes ByteString
bs)
adjustContBigNIntNeedHeader :: (Integer -> ST s (DecodeAction s a)) -> DecodeAction s a
adjustContBigNIntNeedHeader k :: Integer -> ST s (DecodeAction s a)
k = (ByteString -> ST s (DecodeAction s a)) -> DecodeAction s a
forall s a.
(ByteString -> ST s (DecodeAction s a)) -> DecodeAction s a
ConsumeBytes (\bs :: ByteString
bs -> Integer -> ST s (DecodeAction s a)
k (Integer -> ST s (DecodeAction s a))
-> Integer -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> Integer
nintegerFromBytes ByteString
bs)

adjustContCanonicalBigUIntNeedHeader, adjustContCanonicalBigNIntNeedHeader
  :: (Integer -> ST s (DecodeAction s a))
  -> DecodeAction s a

adjustContCanonicalBigUIntNeedHeader :: (Integer -> ST s (DecodeAction s a)) -> DecodeAction s a
adjustContCanonicalBigUIntNeedHeader k :: Integer -> ST s (DecodeAction s a)
k = (ByteString -> ST s (DecodeAction s a)) -> DecodeAction s a
forall s a.
(ByteString -> ST s (DecodeAction s a)) -> DecodeAction s a
ConsumeBytesCanonical ((ByteString -> ST s (DecodeAction s a)) -> DecodeAction s a)
-> (ByteString -> ST s (DecodeAction s a)) -> DecodeAction s a
forall a b. (a -> b) -> a -> b
$ \bs :: ByteString
bs ->
  if ByteString -> Bool
isBigIntRepCanonical ByteString
bs
  then Integer -> ST s (DecodeAction s a)
k (Integer -> ST s (DecodeAction s a))
-> Integer -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> Integer
uintegerFromBytes ByteString
bs
  else DecodeAction s a -> ST s (DecodeAction s a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DecodeAction s a -> ST s (DecodeAction s a))
-> DecodeAction s a -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! String -> DecodeAction s a
forall s a. String -> DecodeAction s a
D.Fail ("non-canonical integer")

adjustContCanonicalBigNIntNeedHeader :: (Integer -> ST s (DecodeAction s a)) -> DecodeAction s a
adjustContCanonicalBigNIntNeedHeader k :: Integer -> ST s (DecodeAction s a)
k = (ByteString -> ST s (DecodeAction s a)) -> DecodeAction s a
forall s a.
(ByteString -> ST s (DecodeAction s a)) -> DecodeAction s a
ConsumeBytesCanonical ((ByteString -> ST s (DecodeAction s a)) -> DecodeAction s a)
-> (ByteString -> ST s (DecodeAction s a)) -> DecodeAction s a
forall a b. (a -> b) -> a -> b
$ \bs :: ByteString
bs ->
  if ByteString -> Bool
isBigIntRepCanonical ByteString
bs
  then Integer -> ST s (DecodeAction s a)
k (Integer -> ST s (DecodeAction s a))
-> Integer -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! ByteString -> Integer
nintegerFromBytes ByteString
bs
  else DecodeAction s a -> ST s (DecodeAction s a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DecodeAction s a -> ST s (DecodeAction s a))
-> DecodeAction s a -> ST s (DecodeAction s a)
forall a b. (a -> b) -> a -> b
$! String -> DecodeAction s a
forall s a. String -> DecodeAction s a
D.Fail ("non-canonical integer")

-- So finally when reading the input buffer we check if we have enough space
-- to read the header of the bytes token and then try to read the bytes body,
-- using the appropriate break-out codes as above.

{-# INLINE readBigUInt #-}
readBigUInt :: ByteString -> DecodedToken (BigIntToken a)
readBigUInt :: ByteString -> DecodedToken (BigIntToken a)
readBigUInt bs :: ByteString
bs
    | let bs' :: ByteString
bs' = ByteString -> ByteString
BS.unsafeTail ByteString
bs
    , Bool -> Bool
not (ByteString -> Bool
BS.null ByteString
bs')
    , let !hdr :: Word8
hdr = ByteString -> Word8
BS.unsafeHead ByteString
bs'
    , ByteString -> Int
BS.length ByteString
bs' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Word8 -> Int
tokenSize Word8
hdr
    = case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeBytes Word8
hdr ByteString
bs' of
        DecodeFailure                           -> DecodedToken (BigIntToken a)
forall a. DecodedToken a
DecodeFailure
        DecodedToken sz :: Int
sz (Fits canonical :: Bool
canonical bstr :: ByteString
bstr)   -> Int -> BigIntToken a -> DecodedToken (BigIntToken a)
forall a. Int -> a -> DecodedToken a
DecodedToken (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
sz)
          (Bool -> Integer -> BigIntToken a
forall a. Bool -> Integer -> BigIntToken a
BigIntToken (Bool
canonical Bool -> Bool -> Bool
&& ByteString -> Bool
isBigIntRepCanonical ByteString
bstr)
                       (ByteString -> Integer
uintegerFromBytes ByteString
bstr))
        DecodedToken sz :: Int
sz (TooLong canonical :: Bool
canonical len :: Int
len) ->
          Int -> BigIntToken a -> DecodedToken (BigIntToken a)
forall a. Int -> a -> DecodedToken a
DecodedToken (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
sz) (Bool -> Int -> BigIntToken a
forall a. Bool -> Int -> BigIntToken a
BigUIntNeedBody Bool
canonical Int
len)

    | Bool
otherwise
    = Int -> BigIntToken a -> DecodedToken (BigIntToken a)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 BigIntToken a
forall a. BigIntToken a
BigUIntNeedHeader

{-# INLINE readBigNInt #-}
readBigNInt :: ByteString -> DecodedToken (BigIntToken a)
readBigNInt :: ByteString -> DecodedToken (BigIntToken a)
readBigNInt bs :: ByteString
bs
    | let bs' :: ByteString
bs' = ByteString -> ByteString
BS.unsafeTail ByteString
bs
    , Bool -> Bool
not (ByteString -> Bool
BS.null ByteString
bs')
    , let !hdr :: Word8
hdr = ByteString -> Word8
BS.unsafeHead ByteString
bs'
    , ByteString -> Int
BS.length ByteString
bs' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Word8 -> Int
tokenSize Word8
hdr
    = case Word8 -> ByteString -> DecodedToken (LongToken ByteString)
tryConsumeBytes Word8
hdr ByteString
bs' of
        DecodeFailure                           -> DecodedToken (BigIntToken a)
forall a. DecodedToken a
DecodeFailure
        DecodedToken sz :: Int
sz (Fits canonical :: Bool
canonical bstr :: ByteString
bstr)   -> Int -> BigIntToken a -> DecodedToken (BigIntToken a)
forall a. Int -> a -> DecodedToken a
DecodedToken (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
sz)
          (Bool -> Integer -> BigIntToken a
forall a. Bool -> Integer -> BigIntToken a
BigIntToken (Bool
canonical Bool -> Bool -> Bool
&& ByteString -> Bool
isBigIntRepCanonical ByteString
bstr)
                       (ByteString -> Integer
nintegerFromBytes ByteString
bstr))
        DecodedToken sz :: Int
sz (TooLong canonical :: Bool
canonical len :: Int
len) ->
          Int -> BigIntToken a -> DecodedToken (BigIntToken a)
forall a. Int -> a -> DecodedToken a
DecodedToken (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
sz) (Bool -> Int -> BigIntToken a
forall a. Bool -> Int -> BigIntToken a
BigNIntNeedBody Bool
canonical Int
len)

    | Bool
otherwise
    = Int -> BigIntToken a -> DecodedToken (BigIntToken a)
forall a. Int -> a -> DecodedToken a
DecodedToken 1 BigIntToken a
forall a. BigIntToken a
BigNIntNeedHeader

-- Binary representation of a big integer is canonical if it's at least 9 bytes
-- long (as for smaller values the canonical representation is the same one as
-- for Int) and the leading byte is not zero (meaning that it's the smallest
-- representation for the number in question).
isBigIntRepCanonical :: ByteString -> Bool
isBigIntRepCanonical :: ByteString -> Bool
isBigIntRepCanonical bstr :: ByteString
bstr = ByteString -> Int
BS.length ByteString
bstr Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 8 Bool -> Bool -> Bool
&& ByteString -> Word8
BS.unsafeHead ByteString
bstr Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= 0x00