module Data.ByteString.Read.Fractional
(
fractional
, double
, fractional10
, fractional'
) where
import GHC.TypeLits.Compat
import Data.Proxy.Compat
import Data.ByteString.Read.Class as C
integral :: forall proxy n r s. (Radix n, ReadFractional r, Ord (Fraction r), Num (Fraction r), Source s)
=> proxy n -> s -> (Fraction r, Int, Int, s)
integral pn = loop 0 0 0
where
pr :: Proxy r
pr = Proxy
loop !i !d !ad !s
| C.null s = (i, d, ad, s)
| not (isDigit pn (C.head s)) = (i, d, ad, s)
| maybe False (i >=) (maxValue pr) = loop i d (ad + 1) (C.tail s)
| otherwise = loop
(i * fromIntegral (natVal pn) + (fromIntegral $ unsafeToDigit pn (C.head s) :: Fraction r))
(d+1) ad (C.tail s)
toFractional :: (Radix b, ReadFractional r, Fractional r)
=> proxy b -> Fraction r -> Fraction r -> Int -> Int -> r
toFractional p q r du d = fromFraction q * radix ^ du + fromFraction r / radix ^ d
where
radix = fromIntegral (natVal p)
fractional' :: (Radix b, ReadFractional r, Source s) => proxy b -> s -> Maybe (r, s)
fractional' pn s = case integral pn s of
(_, 0, _, _) -> Nothing
(q, _, d, s1)
| C.null s1 -> Just (fromFraction q * fromIntegral (natVal pn) ^ d, C.empty)
| C.head s1 /= dot -> Just (fromFraction q, s1)
| otherwise -> case integral pn (C.tail s1) of
(_, 0, _, _) -> Just (fromFraction q, s1)
(r, d', _, s2) -> Just (toFractional pn q r d d', s2)
where
dot = 46
exponential :: forall s. Source s => s -> (Int, s)
exponential s0
| C.null s0 = (0, s0)
| isE (C.head s0) = sign (C.tail s0)
| otherwise = (0, s0)
where
isE w = w == 101 || w == 69
minus = 45
plus = 43
sign s1
| C.null s1 = (0, s0)
| C.head s1 == plus = expPart $ C.tail s1
| C.head s1 == minus = let (e, s) = expPart $ C.tail s1 in (e, s)
| otherwise = expPart s1
expPart s2 = case integral (Proxy :: Proxy 10) s2 :: (Fraction Double, Int, Int, s) of
(_, 0, _, _) -> (0, s0)
(e, _, _, s) -> (fromFraction e, s)
setExpPart :: Fractional f => Int -> f -> f
setExpPart e f
| e >= 0 = f * 10 ^ e
| otherwise = f / 10 ^ abs e
fractional10 :: (ReadFractional r, Source s) => s -> Maybe (r, s)
fractional10 s = fractional' (Proxy :: Proxy 10) s >>= \(f, s') ->
let (e, s'') = exponential s'
in Just (setExpPart e f, s'')
fractional :: (ReadFractional r, Source s) => s -> Maybe (r, s)
fractional s0
| C.null s0 = Nothing
| C.head s0 == zero = radix $ C.tail s0
| otherwise = fractional10 s0
where
zero = 48
isX w = w == 120 || w == 88
isO w = w == 111 || w == 79
radix s1
| C.null s1 = Just (0, C.empty)
| isX (C.head s1) = fractional' (Proxy :: Proxy 16) (C.tail s1)
| isO (C.head s1) = fractional' (Proxy :: Proxy 8) (C.tail s1)
| otherwise = fractional10 s0
double :: Source s => s -> Maybe (Double, s)
double = fractional