module Unicode.Char.Numeric
(
isNumeric
, numericValue
, integerValue
, intToDigiT
, isDigit
, isOctDigit
, isHexDigit
, digitToInt
, intToDigit
) where
import Data.Char (digitToInt, intToDigit, isDigit, isHexDigit, isOctDigit)
import Data.Int (Int64)
import Data.Maybe (isJust)
import Data.Ratio (denominator, numerator)
import GHC.Exts (Char (..), Int (..), chr#, isTrue#, (+#), (<=#), (>=#))
import qualified Unicode.Internal.Char.DerivedNumericValues as V
{-# INLINE isNumeric #-}
isNumeric :: Char -> Bool
isNumeric :: Char -> Bool
isNumeric = Maybe Rational -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Rational -> Bool)
-> (Char -> Maybe Rational) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Maybe Rational
V.numericValue
{-# INLINE numericValue #-}
numericValue :: Char -> Maybe Rational
numericValue :: Char -> Maybe Rational
numericValue = Char -> Maybe Rational
V.numericValue
{-# INLINE integerValue #-}
{-# SPECIALIZE integerValue :: Char -> Maybe Integer #-}
{-# SPECIALIZE integerValue :: Char -> Maybe Int64 #-}
{-# SPECIALIZE integerValue :: Char -> Maybe Int #-}
integerValue :: (Integral a) => Char -> Maybe a
integerValue :: forall a. Integral a => Char -> Maybe a
integerValue Char
c = do
Rational
r <- Char -> Maybe Rational
V.numericValue Char
c
if Rational -> Integer
forall a. Ratio a -> a
denominator Rational
r Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1
then a -> Maybe a
forall a. a -> Maybe a
Just (Integer -> a
forall a. Num a => Integer -> a
fromInteger (Rational -> Integer
forall a. Ratio a -> a
numerator Rational
r))
else Maybe a
forall a. Maybe a
Nothing
intToDigiT :: Int -> Char
intToDigiT :: Int -> Char
intToDigiT (I# Int#
i)
| Int# -> Bool
isTrue# (Int#
i Int# -> Int# -> Int#
>=# Int#
0#) Bool -> Bool -> Bool
&& Int# -> Bool
isTrue# (Int#
i Int# -> Int# -> Int#
<=# Int#
9#) = Char# -> Char
C# (Int# -> Char#
chr# (Int#
0x30# Int# -> Int# -> Int#
+# Int#
i))
| Int# -> Bool
isTrue# (Int#
i Int# -> Int# -> Int#
>=# Int#
10#) Bool -> Bool -> Bool
&& Int# -> Bool
isTrue# (Int#
i Int# -> Int# -> Int#
<=# Int#
15#) = Char# -> Char
C# (Int# -> Char#
chr# (Int#
0x37# Int# -> Int# -> Int#
+# Int#
i))
| Bool
otherwise = [Char] -> Char
forall a. [Char] -> a
errorWithoutStackTrace
([Char]
"Unicode.Char.Numeric.intToDigiT: not a digit " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show (Int# -> Int
I# Int#
i))