{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
module DataFrame.Internal.Parsing.Fast.Int (parseIntField#) where
import qualified Data.ByteString as BS
import qualified Data.ByteString.Unsafe as BSU
import GHC.Exts (Int (..), Int#)
import DataFrame.Internal.Parsing.Fast.Common
parseIntField# :: BS.ByteString -> Int -> Int -> (# Int#, Int# #)
parseIntField# :: ByteString -> Int -> Int -> (# Int#, Int# #)
parseIntField# ByteString
bs Int
start Int
end0
| Int
i0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
end = (# Int#, Int# #)
none
| Bool
otherwise =
let !c0 :: Word8
c0 = ByteString -> Int -> Word8
BSU.unsafeIndex ByteString
bs Int
i0
!neg :: Bool
neg = Word8
c0 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x2D
!i1 :: Int
i1 = if Bool
neg Bool -> Bool -> Bool
|| Word8
c0 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x2B then Int
i0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 else Int
i0
in if Int
i1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
end Bool -> Bool -> Bool
|| Bool -> Bool
not (Word8 -> Bool
isDigitByte (ByteString -> Int -> Word8
BSU.unsafeIndex ByteString
bs Int
i1))
then (# Int#, Int# #)
none
else
let !iz :: Int
iz = ByteString -> Int -> Int -> Int
skipZeroes ByteString
bs Int
i1 Int
end
in ByteString
-> Int
-> Int
-> (Int -> Word64 -> (# Int#, Int# #))
-> (# Int#, Int# #)
forall r. ByteString -> Int -> Int -> (Int -> Word64 -> r) -> r
takeDigits64 ByteString
bs Int
iz Int
end ((Int -> Word64 -> (# Int#, Int# #)) -> (# Int#, Int# #))
-> (Int -> Word64 -> (# Int#, Int# #)) -> (# Int#, Int# #)
forall a b. (a -> b) -> a -> b
$ \Int
dEnd Word64
w ->
if Int
dEnd Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
end Bool -> Bool -> Bool
|| Int
dEnd Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
iz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
19
then (# Int#, Int# #)
none
else
if Bool
neg
then
if Word64
w Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
9223372036854775808
then Int -> (# Int#, Int# #)
done (Int -> Int
forall a. Num a => a -> a
negate (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
w))
else (# Int#, Int# #)
none
else
if Word64
w Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
9223372036854775807
then Int -> (# Int#, Int# #)
done (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
w)
else (# Int#, Int# #)
none
where
!i0 :: Int
i0 = ByteString -> Int -> Int -> Int
skipStrip ByteString
bs Int
start Int
end0
!end :: Int
end = ByteString -> Int -> Int -> Int
skipStripEnd ByteString
bs Int
i0 Int
end0
none :: (# Int#, Int# #)
none = (# Int#
0#, Int#
0# #)
done :: Int -> (# Int#, Int# #)
done :: Int -> (# Int#, Int# #)
done (I# Int#
n) = (# Int#
1#, Int#
n #)
{-# INLINE done #-}
{-# INLINE parseIntField# #-}