module Data.ByteString.Read.Integral
( integral'
, integral
, int
) where
import Data.Proxy.Compat
import GHC.TypeLits.Compat
import Data.ByteString.Read.Class as C
integral_ :: (Radix b, Num n, Source s) => proxy b -> s -> (n, Int, s)
integral_ pn = loop 0 0
where
loop !i !d !s
| C.null s = (i, d, s)
| not (isDigit pn (C.head s)) = (i, d, s)
| otherwise = loop
(i * fromIntegral (natVal pn) + (fromIntegral $ unsafeToDigit pn (C.head s)))
(d+1) (C.tail s)
integral' :: (Radix b, Num n, Source s) => proxy b -> s -> Maybe (n, s)
integral' pn s0 = case integral_ pn s0 of
(_, 0, _) -> Nothing
(n, _, s) -> Just (n, s)
integral :: (Num n, Source s) => s -> Maybe (n, s)
integral = integral' (Proxy :: Proxy 10)
int :: Source s => s -> Maybe (Int, s)
int = integral