{-# LANGUAGE BangPatterns, NamedFieldPuns, OverloadedStrings, UnboxedTuples, CPP #-}
{-# LANGUAGE Trustworthy #-}

-- |
-- Module      : Data.Text.Read
-- Copyright   : (c) 2010, 2011 Bryan O'Sullivan
--
-- License     : BSD-style
-- Maintainer  : bos@serpentine.com
-- Portability : GHC
--
-- Functions used frequently when reading textual data.
module Data.Text.Read
    (
      Reader
    , decimal
    , hexadecimal
    , signed
    , rational
    , double
    ) where

import Control.Monad (liftM)
import Data.Char (ord)
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Ratio ((%))
import Data.Text as T
import Data.Text.Internal as T (Text(..))
import Data.Text.Array as A
import Data.Text.Internal.Private (spanAscii_)
import Data.Text.Internal.Read
import Data.Text.Internal.Builder.Int.Digits (Base(..), base10, base16)
import qualified Data.Text.Unsafe as TU
import Data.Word (Word, Word8, Word16, Word32, Word64)

-- | Read some text.  If the read succeeds, return its value and the
-- remaining text, otherwise an error message.
type Reader a = Text -> Either String (a, Text)
type Parser a = IParser Text a

-- | Read a decimal integer.  The input must begin with at least one
-- decimal digit, and is consumed until a non-digit or end of string
-- is reached.
--
-- This function does not handle leading sign characters.  If you need
-- to handle signed input, use @'signed' 'decimal'@.
--
-- /Note/: For fixed-width integer types, this function does not
-- attempt to detect overflow, so a sufficiently long input may give
-- incorrect results.  If you are worried about overflow, use
-- 'Integer' for your result type.
decimal :: Integral a => Reader a
{-# SPECIALIZE decimal :: Reader Int #-}
{-# SPECIALIZE decimal :: Reader Int8 #-}
{-# SPECIALIZE decimal :: Reader Int16 #-}
{-# SPECIALIZE decimal :: Reader Int32 #-}
{-# SPECIALIZE decimal :: Reader Int64 #-}
{-# SPECIALIZE decimal :: Reader Integer #-}
{-# SPECIALIZE decimal :: Reader Data.Word.Word #-}
{-# SPECIALIZE decimal :: Reader Word8 #-}
{-# SPECIALIZE decimal :: Reader Word16 #-}
{-# SPECIALIZE decimal :: Reader Word32 #-}
{-# SPECIALIZE decimal :: Reader Word64 #-}
decimal :: forall a. Integral a => Reader a
decimal Text
txt
    | Text -> Bool
T.null Text
h  = String -> Either String (a, Text)
forall a b. a -> Either a b
Left String
"input does not start with a digit"
    | Bool
otherwise = (a, Text) -> Either String (a, Text)
forall a b. b -> Either a b
Right (Base -> (Char -> Word) -> Text -> a
forall a. Integral a => Base -> (Char -> Word) -> Text -> a
integer Base
base10 (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word) -> (Char -> Int) -> Char -> Word
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
digitToInt) Text
h, Text
t)
  where (# Text
h,Text
t #)  = (Word8 -> Bool) -> Text -> (# Text, Text #)
spanAscii_ (\Word8
w -> Word8
w Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- Char -> Word8
ord8 Char
'0' Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Word8
10) Text
txt

-- | Read a hexadecimal integer, consisting of an optional leading
-- @\"0x\"@ followed by at least one hexadecimal digit. Input is
-- consumed until a non-hex-digit or end of string is reached.
-- This function is case insensitive.
--
-- This function does not handle leading sign characters.  If you need
-- to handle signed input, use @'signed' 'hexadecimal'@.
--
-- /Note/: For fixed-width integer types, this function does not
-- attempt to detect overflow, so a sufficiently long input may give
-- incorrect results.  If you are worried about overflow, use
-- 'Integer' for your result type.
hexadecimal :: Integral a => Reader a
{-# SPECIALIZE hexadecimal :: Reader Int #-}
{-# SPECIALIZE hexadecimal :: Reader Int8 #-}
{-# SPECIALIZE hexadecimal :: Reader Int16 #-}
{-# SPECIALIZE hexadecimal :: Reader Int32 #-}
{-# SPECIALIZE hexadecimal :: Reader Int64 #-}
{-# SPECIALIZE hexadecimal :: Reader Integer #-}
{-# SPECIALIZE hexadecimal :: Reader Word #-}
{-# SPECIALIZE hexadecimal :: Reader Word8 #-}
{-# SPECIALIZE hexadecimal :: Reader Word16 #-}
{-# SPECIALIZE hexadecimal :: Reader Word32 #-}
{-# SPECIALIZE hexadecimal :: Reader Word64 #-}
hexadecimal :: forall a. Integral a => Reader a
hexadecimal Text
txt
    | Text
h Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"0x" Bool -> Bool -> Bool
|| Text
h Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"0X" = Reader a
forall a. Integral a => Reader a
hex Text
t
    | Bool
otherwise              = Reader a
forall a. Integral a => Reader a
hex Text
txt
 where (Text
h,Text
t) = Int -> Text -> (Text, Text)
T.splitAt Int
2 Text
txt

hex :: Integral a => Reader a
{-# SPECIALIZE hex :: Reader Int #-}
{-# SPECIALIZE hex :: Reader Int8 #-}
{-# SPECIALIZE hex :: Reader Int16 #-}
{-# SPECIALIZE hex :: Reader Int32 #-}
{-# SPECIALIZE hex :: Reader Int64 #-}
{-# SPECIALIZE hex :: Reader Integer #-}
{-# SPECIALIZE hex :: Reader Word #-}
{-# SPECIALIZE hex :: Reader Word8 #-}
{-# SPECIALIZE hex :: Reader Word16 #-}
{-# SPECIALIZE hex :: Reader Word32 #-}
{-# SPECIALIZE hex :: Reader Word64 #-}
hex :: forall a. Integral a => Reader a
hex Text
txt
    | Text -> Bool
T.null Text
h  = String -> Either String (a, Text)
forall a b. a -> Either a b
Left String
"input does not start with a hexadecimal digit"
    | Bool
otherwise = (a, Text) -> Either String (a, Text)
forall a b. b -> Either a b
Right (Base -> (Char -> Word) -> Text -> a
forall a. Integral a => Base -> (Char -> Word) -> Text -> a
integer Base
base16 (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word) -> (Char -> Int) -> Char -> Word
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
hexDigitToInt) Text
h, Text
t)
  where (# Text
h,Text
t #)  = (Word8 -> Bool) -> Text -> (# Text, Text #)
spanAscii_ (\Word8
w -> Word8
w Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- Char -> Word8
ord8 Char
'0' Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Word8
10 Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- Char -> Word8
ord8 Char
'A' Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Word8
6 Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- Char -> Word8
ord8 Char
'a' Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Word8
6) Text
txt

-- | Internal function to read a base-@base@ number faster than the naive quadratic algorithm.
--
-- The input text must only contain ASCII digits, to be read by the @Char -> Word@ function
-- which will be either 'hexDigitToInt' (hexadecimal) or 'digitToInt' (decimal only).
-- It may be arbitrarily long.
integer :: Integral a => Base -> (Char -> Word) -> Text -> a
integer :: forall a. Integral a => Base -> (Char -> Word) -> Text -> a
integer (Base { Word
base :: Word
base :: Base -> Word
base, Integer
maxWord :: Integer
maxWord :: Base -> Integer
maxWord, Int
maxDigits :: Int
maxDigits :: Base -> Int
maxDigits }) Char -> Word
digit Text
t0
  | Text -> Int
TU.lengthWord8 Text
t0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
maxDigits = Word -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word -> (Char -> Word) -> Text -> Word
word Word
base Char -> Word
digit Text
t0)
  | Bool
otherwise = [a] -> Text -> a
scanWords [Text -> a
word' (Int -> Text -> Text
TU.takeWord8 Int
mswSize Text
t0)] (Int -> Text -> Text
TU.dropWord8 Int
mswSize Text
t0)
  where
    -- Split the most significant word from t0 so the remainder's length is a multiple of maxDigits
    mswSize :: Int
mswSize = (Text -> Int
TU.lengthWord8 Text
t0 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
maxDigits Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
    word' :: Text -> a
word' = Word -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word -> a) -> (Text -> Word) -> Text -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> (Char -> Word) -> Text -> Word
word Word
base Char -> Word
digit

    -- The length of t is a positive multiple of maxDigits
    scanWords :: [a] -> Text -> a
scanWords [a]
chunks Text
t
      | Text -> Int
TU.lengthWord8 Text
t Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
maxDigits = a -> [a] -> a
forall {t}. Num t => t -> [t] -> t
reduce (Integer -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
maxWord) (Text -> a
word' Text
t a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
chunks)
      | Bool
otherwise = [a] -> Text -> a
scanWords (Text -> a
word' (Int -> Text -> Text
TU.takeWord8 Int
maxDigits Text
t) a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
chunks) (Int -> Text -> Text
TU.dropWord8 Int
maxDigits Text
t)

    reduce :: t -> [t] -> t
reduce !t
_ [t
c] = t
c
    reduce t
width [t]
cs = t -> [t] -> t
reduce (t
width t -> t -> t
forall a. Num a => a -> a -> a
* t
width) (t -> [t] -> [t]
forall {t}. Num t => t -> [t] -> [t]
pairs t
width [t]
cs)

    pairs :: t -> [t] -> [t]
pairs !t
_ [] = []
    pairs t
_ [t
c] = [t
c]
    pairs t
width (t
c0 : t
c1 : [t]
cs) = t
c0 t -> t -> t
forall a. Num a => a -> a -> a
+ t
width t -> t -> t
forall a. Num a => a -> a -> a
* t
c1 t -> [t] -> [t]
forall a. a -> [a] -> [a]
: t -> [t] -> [t]
pairs t
width [t]
cs

-- | Internal function to read a base-@b@ number into a 'Word'.
--
-- The text must only contain ASCII digits and the result must not to overflow 'Word'.
word :: Word -> (Char -> Word) -> Text -> Word
word :: Word -> (Char -> Word) -> Text -> Word
word Word
base Char -> Word
digit = (Word -> Char -> Word) -> Word -> Text -> Word
forall a. (a -> Char -> a) -> a -> Text -> a
T.foldl' (\Word
n Char
d -> Word
n Word -> Word -> Word
forall a. Num a => a -> a -> a
* Word
base Word -> Word -> Word
forall a. Num a => a -> a -> a
+ Word -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Char -> Word
digit Char
d)) Word
0

-- | Read an optional leading sign character (@\'-\'@ or @\'+\'@) and
-- apply it to the result of applying the given reader.
signed :: Num a => Reader a -> Reader a
{-# INLINE signed #-}
signed :: forall a. Num a => Reader a -> Reader a
signed Reader a
f = IParser Text a -> Reader a
forall t a. IParser t a -> IReader t a
runP (IParser Text a -> IParser Text a
forall a. Num a => Parser a -> Parser a
signa (Reader a -> IParser Text a
forall t a. IReader t a -> IParser t a
P Reader a
f))

-- | Read a rational number.
--
-- This function accepts an optional leading sign character, followed
-- by at least one decimal digit.  The syntax similar to that accepted
-- by the 'read' function, with the exception that a trailing @\'.\'@
-- or @\'e\'@ /not/ followed by a number is not consumed.
--
-- Examples (with behaviour identical to 'read'):
--
-- >rational "3"     == Right (3.0, "")
-- >rational "3.1"   == Right (3.1, "")
-- >rational "3e4"   == Right (30000.0, "")
-- >rational "3.1e4" == Right (31000.0, "")
-- >rational ".3"    == Left "input does not start with a digit"
-- >rational "e3"    == Left "input does not start with a digit"
--
-- Examples of differences from 'read':
--
-- >rational "3.foo" == Right (3.0, ".foo")
-- >rational "3e"    == Right (3.0, "e")
-- >rational "1e9223372036854775808" == Left "exponent out of range"
rational :: Fractional a => Reader a
{-# SPECIALIZE rational :: Reader Double #-}
rational :: forall a. Fractional a => Reader a
rational = (Integer -> Integer -> Integer -> Int -> a) -> Reader a
forall a.
Fractional a =>
(Integer -> Integer -> Integer -> Int -> a) -> Reader a
floaty ((Integer -> Integer -> Integer -> Int -> a) -> Reader a)
-> (Integer -> Integer -> Integer -> Int -> a) -> Reader a
forall a b. (a -> b) -> a -> b
$ \Integer
real Integer
frac Integer
fracDenom Int
power ->
  -- We must be careful to prevent DDoS attacks: if the return type is 'Double',
  -- a client rightfully expects 'rational' to operate within bounded memory.
  -- Thus if power is small, we can compute fraction with full precision and divide.
  -- Otherwise divide first, apply fromRational and scale last:
  -- the small loss of precision for Double does not matter much because the result is
  -- likely infinity or zero anyway.
  if - Int
1000 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
power Bool -> Bool -> Bool
&& Int
power Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
1000
  then let n :: Integer
n = Integer
real Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
fracDenom Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
frac
       in Rational -> a
forall a. Fractional a => Rational -> a
fromRational (Rational -> a) -> Rational -> a
forall a b. (a -> b) -> a -> b
$ if Int
power Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0
                         then (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
10 Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
power) Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Integer
fracDenom
                         else Integer
n Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% (Integer
fracDenom Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
10 Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Int -> Int
forall a. Num a => a -> a
negate Int
power)
  else Rational -> a
forall a. Fractional a => Rational -> a
fromRational (Integer
real Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Integer
1 Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
+ Integer
frac Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
% Integer
fracDenom) a -> a -> a
forall a. Num a => a -> a -> a
* (a
10 a -> Int -> a
forall a b. (Fractional a, Integral b) => a -> b -> a
^^ Int
power)

-- | Read a rational number.
--
-- The syntax accepted by this function is the same as for 'rational'.
--
-- /Note/: This function is almost ten times faster than 'rational',
-- but is slightly less accurate.
--
-- The 'Double' type supports about 16 decimal places of accuracy.
-- For 94.2% of numbers, this function and 'rational' give identical
-- results, but for the remaining 5.8%, this function loses precision
-- around the 15th decimal place.  For 0.001% of numbers, this
-- function will lose precision at the 13th or 14th decimal place.
double :: Reader Double
double :: Reader Double
double = (Integer -> Integer -> Integer -> Int -> Double) -> Reader Double
forall a.
Fractional a =>
(Integer -> Integer -> Integer -> Int -> a) -> Reader a
floaty ((Integer -> Integer -> Integer -> Int -> Double) -> Reader Double)
-> (Integer -> Integer -> Integer -> Int -> Double)
-> Reader Double
forall a b. (a -> b) -> a -> b
$ \Integer
real Integer
frac Integer
fracDenom Int
power ->
                   (Integer -> Double
forall a. Num a => Integer -> a
fromInteger Integer
real Double -> Double -> Double
forall a. Num a => a -> a -> a
+
                   Integer -> Double
forall a. Num a => Integer -> a
fromInteger Integer
frac Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Integer -> Double
forall a. Num a => Integer -> a
fromInteger Integer
fracDenom) Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
10 Double -> Int -> Double
forall a b. (Fractional a, Integral b) => a -> b -> a
^^ Int
power)

signa :: Num a => Parser a -> Parser a
{-# SPECIALIZE signa :: Parser Int -> Parser Int #-}
{-# SPECIALIZE signa :: Parser Int8 -> Parser Int8 #-}
{-# SPECIALIZE signa :: Parser Int16 -> Parser Int16 #-}
{-# SPECIALIZE signa :: Parser Int32 -> Parser Int32 #-}
{-# SPECIALIZE signa :: Parser Int64 -> Parser Int64 #-}
{-# SPECIALIZE signa :: Parser Integer -> Parser Integer #-}
signa :: forall a. Num a => Parser a -> Parser a
signa Parser a
p = do
  Word8
sw <- Parser Word8
sign
  if Word8
sw Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Char -> Word8
ord8 Char
'+' then Parser a
p else a -> a
forall a. Num a => a -> a
negate (a -> a) -> Parser a -> Parser a
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` Parser a
p

sign :: Parser Word8
sign :: Parser Word8
sign = Word8 -> Parser Word8 -> Parser Word8
forall a t. a -> IParser t a -> IParser t a
perhaps (Char -> Word8
ord8 Char
'+') (Parser Word8 -> Parser Word8) -> Parser Word8 -> Parser Word8
forall a b. (a -> b) -> a -> b
$ (Word8 -> Bool) -> Parser Word8
charAscii (\Word8
c -> Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Char -> Word8
ord8 Char
'-' Bool -> Bool -> Bool
|| Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Char -> Word8
ord8 Char
'+')

asciiDigits :: Parser Text
asciiDigits :: Parser Text
asciiDigits = IReader Text Text -> Parser Text
forall t a. IReader t a -> IParser t a
P (IReader Text Text -> Parser Text)
-> IReader Text Text -> Parser Text
forall a b. (a -> b) -> a -> b
$ \Text
txt -> let (# Text
h, Text
t #) = (Word8 -> Bool) -> Text -> (# Text, Text #)
spanAscii_ (\Word8
w -> Word8
w Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- Char -> Word8
ord8 Char
'0' Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Word8
10) Text
txt in (Text, Text) -> Either String (Text, Text)
forall a b. b -> Either a b
Right (Text
h, Text
t)

charAscii :: (Word8 -> Bool) -> Parser Word8
charAscii :: (Word8 -> Bool) -> Parser Word8
charAscii Word8 -> Bool
p = IReader Text Word8 -> Parser Word8
forall t a. IReader t a -> IParser t a
P (IReader Text Word8 -> Parser Word8)
-> IReader Text Word8 -> Parser Word8
forall a b. (a -> b) -> a -> b
$ \(Text Array
arr Int
off Int
len) -> let c :: Word8
c = Array -> Int -> Word8
A.unsafeIndex Array
arr Int
off in
  if Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Word8 -> Bool
p Word8
c
  then (Word8, Text) -> Either String (Word8, Text)
forall a b. b -> Either a b
Right (Word8
c, Array -> Int -> Int -> Text
Text Array
arr (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))
  else String -> Either String (Word8, Text)
forall a b. a -> Either a b
Left String
"character does not match"

floaty :: Fractional a => (Integer -> Integer -> Integer -> Int -> a) -> Reader a
{-# INLINE floaty #-}
floaty :: forall a.
Fractional a =>
(Integer -> Integer -> Integer -> Int -> a) -> Reader a
floaty Integer -> Integer -> Integer -> Int -> a
f = IParser Text a -> IReader Text a
forall t a. IParser t a -> IReader t a
runP (IParser Text a -> IReader Text a)
-> IParser Text a -> IReader Text a
forall a b. (a -> b) -> a -> b
$ do
  Word8
sw <- Parser Word8
sign
  Integer
real <- IReader Text Integer -> IParser Text Integer
forall t a. IReader t a -> IParser t a
P IReader Text Integer
forall a. Integral a => Reader a
decimal
  T Integer
fraction Int
fracDigits <- T -> IParser Text T -> IParser Text T
forall a t. a -> IParser t a -> IParser t a
perhaps (Integer -> Int -> T
T Integer
0 Int
0) (IParser Text T -> IParser Text T)
-> IParser Text T -> IParser Text T
forall a b. (a -> b) -> a -> b
$ do
    Word8
_ <- (Word8 -> Bool) -> Parser Word8
charAscii (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Char -> Word8
ord8 Char
'.')
    Int
digits <- IReader Text Int -> IParser Text Int
forall t a. IReader t a -> IParser t a
P (IReader Text Int -> IParser Text Int)
-> IReader Text Int -> IParser Text Int
forall a b. (a -> b) -> a -> b
$ \Text
t -> (Int, Text) -> Either String (Int, Text)
forall a b. b -> Either a b
Right (let (# Text
hd, Text
_ #) = (Word8 -> Bool) -> Text -> (# Text, Text #)
spanAscii_ (\Word8
w -> Word8
w Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- Char -> Word8
ord8 Char
'0' Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Word8
10) Text
t in Text -> Int
T.length Text
hd, Text
t)
    Integer
n <- IReader Text Integer -> IParser Text Integer
forall t a. IReader t a -> IParser t a
P IReader Text Integer
forall a. Integral a => Reader a
decimal
    T -> IParser Text T
forall a. a -> IParser Text a
forall (m :: * -> *) a. Monad m => a -> m a
return (T -> IParser Text T) -> T -> IParser Text T
forall a b. (a -> b) -> a -> b
$ Integer -> Int -> T
T Integer
n Int
digits
  let e :: Word8 -> Bool
e Word8
c = Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Char -> Word8
ord8 Char
'e' Bool -> Bool -> Bool
|| Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Char -> Word8
ord8 Char
'E'
  Text
beforeE <- Parser Text
forall t. IParser t t
snapshot
  Int
power <- Parser Word8
-> IParser Text Int
-> (Word8 -> IParser Text Int)
-> IParser Text Int
forall t a b.
IParser t a -> IParser t b -> (a -> IParser t b) -> IParser t b
catchOrBind ((Word8 -> Bool) -> Parser Word8
charAscii Word8 -> Bool
e) (Int -> IParser Text Int
forall a. a -> IParser Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
0) ((Word8 -> IParser Text Int) -> IParser Text Int)
-> (Word8 -> IParser Text Int) -> IParser Text Int
forall a b. (a -> b) -> a -> b
$ \Word8
_ -> do
    Word8
esw <- Parser Word8
sign
    Text
ds <- Parser Text
asciiDigits
    if Text -> Bool
T.null Text
ds
      then Text -> Int -> IParser Text Int
forall t a. t -> a -> IParser t a
reset Text
beforeE Int
0
      else if Text -> Int
TU.lengthWord8 ((Char -> Bool) -> Text -> Text
T.dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'0') Text
ds) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
maxExponentDigits
        then String -> IParser Text Int
forall t a. String -> IParser t a
err String
"exponent out of range"
        else Int -> IParser Text Int
forall a. a -> IParser Text a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> IParser Text Int) -> Int -> IParser Text Int
forall a b. (a -> b) -> a -> b
$ (if Word8
esw Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Char -> Word8
ord8 Char
'+' then Int -> Int
forall a. a -> a
id else Int -> Int
forall a. Num a => a -> a
negate)
                      ((Int -> Char -> Int) -> Int -> Text -> Int
forall a. (a -> Char -> a) -> a -> Text -> a
T.foldl' (\Int
n Char
d -> Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
10 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Char -> Int
digitToInt Char
d) Int
0 Text
ds)
  let n :: a
n = if Int
fracDigits Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
&& Int
power Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
          then Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
real
          else Integer -> Integer -> Integer -> Int -> a
f Integer
real Integer
fraction (Integer
10 Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
fracDigits) Int
power
  a -> IParser Text a
forall a. a -> IParser Text a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> IParser Text a) -> a -> IParser Text a
forall a b. (a -> b) -> a -> b
$! if Word8
sw Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Char -> Word8
ord8 Char
'+'
            then a
n
            else -a
n

ord8 :: Char -> Word8
ord8 :: Char -> Word8
ord8 = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> (Char -> Int) -> Char -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
ord