{-# LANGUAGE OverloadedStrings #-}
module Cometa.Encoding.Integer
( Radix,
mkRadix,
radixValue,
binary,
decimal,
hexadecimal,
fromText,
fromTextAuto,
toText,
toUpperText,
ByteOrder (..),
fromBytes,
toBytes,
modularInverse,
modularPower,
)
where
import Cometa.Error (Error (..))
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Char (chr, isAsciiLower, isAsciiUpper, isDigit, ord)
import Data.List (unfoldr)
import Data.Text (Text)
import qualified Data.Text as T
import Numeric (showIntAtBase)
newtype Radix = Radix Int deriving (Radix -> Radix -> Bool
(Radix -> Radix -> Bool) -> (Radix -> Radix -> Bool) -> Eq Radix
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Radix -> Radix -> Bool
== :: Radix -> Radix -> Bool
$c/= :: Radix -> Radix -> Bool
/= :: Radix -> Radix -> Bool
Eq, Eq Radix
Eq Radix =>
(Radix -> Radix -> Ordering)
-> (Radix -> Radix -> Bool)
-> (Radix -> Radix -> Bool)
-> (Radix -> Radix -> Bool)
-> (Radix -> Radix -> Bool)
-> (Radix -> Radix -> Radix)
-> (Radix -> Radix -> Radix)
-> Ord Radix
Radix -> Radix -> Bool
Radix -> Radix -> Ordering
Radix -> Radix -> Radix
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Radix -> Radix -> Ordering
compare :: Radix -> Radix -> Ordering
$c< :: Radix -> Radix -> Bool
< :: Radix -> Radix -> Bool
$c<= :: Radix -> Radix -> Bool
<= :: Radix -> Radix -> Bool
$c> :: Radix -> Radix -> Bool
> :: Radix -> Radix -> Bool
$c>= :: Radix -> Radix -> Bool
>= :: Radix -> Radix -> Bool
$cmax :: Radix -> Radix -> Radix
max :: Radix -> Radix -> Radix
$cmin :: Radix -> Radix -> Radix
min :: Radix -> Radix -> Radix
Ord, Int -> Radix -> ShowS
[Radix] -> ShowS
Radix -> [Char]
(Int -> Radix -> ShowS)
-> (Radix -> [Char]) -> ([Radix] -> ShowS) -> Show Radix
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Radix -> ShowS
showsPrec :: Int -> Radix -> ShowS
$cshow :: Radix -> [Char]
show :: Radix -> [Char]
$cshowList :: [Radix] -> ShowS
showList :: [Radix] -> ShowS
Show)
data ByteOrder = LittleEndian | BigEndian deriving (ByteOrder -> ByteOrder -> Bool
(ByteOrder -> ByteOrder -> Bool)
-> (ByteOrder -> ByteOrder -> Bool) -> Eq ByteOrder
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ByteOrder -> ByteOrder -> Bool
== :: ByteOrder -> ByteOrder -> Bool
$c/= :: ByteOrder -> ByteOrder -> Bool
/= :: ByteOrder -> ByteOrder -> Bool
Eq, Eq ByteOrder
Eq ByteOrder =>
(ByteOrder -> ByteOrder -> Ordering)
-> (ByteOrder -> ByteOrder -> Bool)
-> (ByteOrder -> ByteOrder -> Bool)
-> (ByteOrder -> ByteOrder -> Bool)
-> (ByteOrder -> ByteOrder -> Bool)
-> (ByteOrder -> ByteOrder -> ByteOrder)
-> (ByteOrder -> ByteOrder -> ByteOrder)
-> Ord ByteOrder
ByteOrder -> ByteOrder -> Bool
ByteOrder -> ByteOrder -> Ordering
ByteOrder -> ByteOrder -> ByteOrder
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ByteOrder -> ByteOrder -> Ordering
compare :: ByteOrder -> ByteOrder -> Ordering
$c< :: ByteOrder -> ByteOrder -> Bool
< :: ByteOrder -> ByteOrder -> Bool
$c<= :: ByteOrder -> ByteOrder -> Bool
<= :: ByteOrder -> ByteOrder -> Bool
$c> :: ByteOrder -> ByteOrder -> Bool
> :: ByteOrder -> ByteOrder -> Bool
$c>= :: ByteOrder -> ByteOrder -> Bool
>= :: ByteOrder -> ByteOrder -> Bool
$cmax :: ByteOrder -> ByteOrder -> ByteOrder
max :: ByteOrder -> ByteOrder -> ByteOrder
$cmin :: ByteOrder -> ByteOrder -> ByteOrder
min :: ByteOrder -> ByteOrder -> ByteOrder
Ord, Int -> ByteOrder -> ShowS
[ByteOrder] -> ShowS
ByteOrder -> [Char]
(Int -> ByteOrder -> ShowS)
-> (ByteOrder -> [Char])
-> ([ByteOrder] -> ShowS)
-> Show ByteOrder
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ByteOrder -> ShowS
showsPrec :: Int -> ByteOrder -> ShowS
$cshow :: ByteOrder -> [Char]
show :: ByteOrder -> [Char]
$cshowList :: [ByteOrder] -> ShowS
showList :: [ByteOrder] -> ShowS
Show, Int -> ByteOrder
ByteOrder -> Int
ByteOrder -> [ByteOrder]
ByteOrder -> ByteOrder
ByteOrder -> ByteOrder -> [ByteOrder]
ByteOrder -> ByteOrder -> ByteOrder -> [ByteOrder]
(ByteOrder -> ByteOrder)
-> (ByteOrder -> ByteOrder)
-> (Int -> ByteOrder)
-> (ByteOrder -> Int)
-> (ByteOrder -> [ByteOrder])
-> (ByteOrder -> ByteOrder -> [ByteOrder])
-> (ByteOrder -> ByteOrder -> [ByteOrder])
-> (ByteOrder -> ByteOrder -> ByteOrder -> [ByteOrder])
-> Enum ByteOrder
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: ByteOrder -> ByteOrder
succ :: ByteOrder -> ByteOrder
$cpred :: ByteOrder -> ByteOrder
pred :: ByteOrder -> ByteOrder
$ctoEnum :: Int -> ByteOrder
toEnum :: Int -> ByteOrder
$cfromEnum :: ByteOrder -> Int
fromEnum :: ByteOrder -> Int
$cenumFrom :: ByteOrder -> [ByteOrder]
enumFrom :: ByteOrder -> [ByteOrder]
$cenumFromThen :: ByteOrder -> ByteOrder -> [ByteOrder]
enumFromThen :: ByteOrder -> ByteOrder -> [ByteOrder]
$cenumFromTo :: ByteOrder -> ByteOrder -> [ByteOrder]
enumFromTo :: ByteOrder -> ByteOrder -> [ByteOrder]
$cenumFromThenTo :: ByteOrder -> ByteOrder -> ByteOrder -> [ByteOrder]
enumFromThenTo :: ByteOrder -> ByteOrder -> ByteOrder -> [ByteOrder]
Enum, ByteOrder
ByteOrder -> ByteOrder -> Bounded ByteOrder
forall a. a -> a -> Bounded a
$cminBound :: ByteOrder
minBound :: ByteOrder
$cmaxBound :: ByteOrder
maxBound :: ByteOrder
Bounded)
mkRadix :: Int -> Either Error Radix
mkRadix :: Int -> Either Error Radix
mkRadix Int
n
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
2 Bool -> Bool -> Bool
&& Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
62 = Radix -> Either Error Radix
forall a b. b -> Either a b
Right (Int -> Radix
Radix Int
n)
| Bool
otherwise = Error -> Either Error Radix
forall a b. a -> Either a b
Left (Int -> Error
InvalidIntegerRadix Int
n)
radixValue :: Radix -> Int
radixValue :: Radix -> Int
radixValue (Radix Int
n) = Int
n
binary :: Radix
binary :: Radix
binary = Int -> Radix
Radix Int
2
decimal :: Radix
decimal :: Radix
decimal = Int -> Radix
Radix Int
10
hexadecimal :: Radix
hexadecimal :: Radix
hexadecimal = Int -> Radix
Radix Int
16
fromText :: Radix -> Text -> Either Error Integer
fromText :: Radix -> Text -> Either Error Integer
fromText (Radix Int
radix) Text
text = let (Bool
negative, Text
digits) = Text -> (Bool, Text)
signed Text
text in Int -> Bool -> Text -> Either Error Integer
parseDigits Int
radix Bool
negative Text
digits
fromTextAuto :: Text -> Either Error Integer
fromTextAuto :: Text -> Either Error Integer
fromTextAuto Text
text =
let (Bool
negative, Text
digits) = Text -> (Bool, Text)
signed Text
text
(Int
radix, Text
remaining) = case Text -> [Char]
T.unpack (Int -> Text -> Text
T.take Int
2 Text
digits) of
[Char
'0', Char
'x'] -> (Int
16, Int -> Text -> Text
T.drop Int
2 Text
digits)
[Char
'0', Char
'X'] -> (Int
16, Int -> Text -> Text
T.drop Int
2 Text
digits)
[Char
'0', Char
'b'] -> (Int
2, Int -> Text -> Text
T.drop Int
2 Text
digits)
[Char
'0', Char
'B'] -> (Int
2, Int -> Text -> Text
T.drop Int
2 Text
digits)
[Char]
_ | Text -> Text -> Bool
T.isPrefixOf Text
"0" Text
digits -> (Int
8, Text
digits)
[Char]
_ -> (Int
10, Text
digits)
in Int -> Bool -> Text -> Either Error Integer
parseDigits Int
radix Bool
negative Text
remaining
toText :: Radix -> Integer -> Text
toText :: Radix -> Integer -> Text
toText (Radix Int
radix) Integer
number =
[Char] -> Text
T.pack ((if Integer
number Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 then [Char]
"-" else [Char]
"") [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Integer -> (Int -> Char) -> Integer -> ShowS
forall a. Integral a => a -> (Int -> Char) -> a -> ShowS
showIntAtBase (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
radix) (Int -> Int -> Char
digitCharacter Int
radix) (Integer -> Integer
forall a. Num a => a -> a
abs Integer
number) [Char]
"")
toUpperText :: Radix -> Integer -> Either Error Text
toUpperText :: Radix -> Integer -> Either Error Text
toUpperText Radix
radix Integer
number
| Radix -> Int
radixValue Radix
radix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
36 = Error -> Either Error Text
forall a b. a -> Either a b
Left (Int -> Error
InvalidIntegerRadix (Int -> Int
forall a. Num a => a -> a
negate (Radix -> Int
radixValue Radix
radix)))
| Bool
otherwise = Text -> Either Error Text
forall a b. b -> Either a b
Right (Text -> Text
T.toUpper (Radix -> Integer -> Text
toText Radix
radix Integer
number))
fromBytes :: ByteOrder -> ByteString -> Integer
fromBytes :: ByteOrder -> ByteString -> Integer
fromBytes ByteOrder
order = (Integer -> Word8 -> Integer) -> Integer -> ByteString -> Integer
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
B.foldl' (\Integer
n Word8
b -> Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
256 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word8 -> Integer
forall a. Integral a => a -> Integer
toInteger Word8
b) Integer
0 (ByteString -> Integer)
-> (ByteString -> ByteString) -> ByteString -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
ordered
where
ordered :: ByteString -> ByteString
ordered = case ByteOrder
order of ByteOrder
LittleEndian -> ByteString -> ByteString
B.reverse; ByteOrder
BigEndian -> ByteString -> ByteString
forall a. a -> a
id
toBytes :: ByteOrder -> Integer -> ByteString
toBytes :: ByteOrder -> Integer -> ByteString
toBytes ByteOrder
order Integer
number =
let little :: [Word8]
little = if Integer
number Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 then [Word8
0] else (Integer -> Maybe (Word8, Integer)) -> Integer -> [Word8]
forall b a. (b -> Maybe (a, b)) -> b -> [a]
unfoldr Integer -> Maybe (Word8, Integer)
forall {a}. Num a => Integer -> Maybe (a, Integer)
step (Integer -> Integer
forall a. Num a => a -> a
abs Integer
number)
in [Word8] -> ByteString
B.pack (case ByteOrder
order of ByteOrder
LittleEndian -> [Word8]
little; ByteOrder
BigEndian -> [Word8] -> [Word8]
forall a. [a] -> [a]
reverse [Word8]
little)
where
step :: Integer -> Maybe (a, Integer)
step Integer
0 = Maybe (a, Integer)
forall a. Maybe a
Nothing
step Integer
n = let (Integer
q, Integer
r) = Integer
n Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Integer
256 in (a, Integer) -> Maybe (a, Integer)
forall a. a -> Maybe a
Just (Integer -> a
forall a. Num a => Integer -> a
fromInteger Integer
r, Integer
q)
modularInverse :: Integer -> Integer -> Either Error Integer
modularInverse :: Integer -> Integer -> Either Error Integer
modularInverse Integer
value Integer
modulus
| Integer -> Integer
forall a. Num a => a -> a
abs Integer
modulus Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
1 = Error -> Either Error Integer
forall a b. a -> Either a b
Left (Integer -> Error
InvalidModulus Integer
modulus)
| Bool
otherwise =
let (Integer
divisor, Integer
coefficient) = Integer -> Integer -> Integer -> Integer -> (Integer, Integer)
forall {t}. Integral t => t -> t -> t -> t -> (t, t)
euclid (Integer -> Integer
forall a. Num a => a -> a
abs Integer
modulus) (Integer
value Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer -> Integer
forall a. Num a => a -> a
abs Integer
modulus) Integer
0 Integer
1
in if Integer
divisor Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1 then Integer -> Either Error Integer
forall a b. b -> Either a b
Right (Integer
coefficient Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer -> Integer
forall a. Num a => a -> a
abs Integer
modulus) else Error -> Either Error Integer
forall a b. a -> Either a b
Left (Integer -> Integer -> Error
NoModularInverse Integer
value Integer
modulus)
where
euclid :: t -> t -> t -> t -> (t, t)
euclid t
a t
0 t
x t
_ = (t
a, t
x)
euclid t
a t
b t
x t
y = let (t
q, t
r) = t
a t -> t -> (t, t)
forall a. Integral a => a -> a -> (a, a)
`quotRem` t
b; next :: t
next = t
x t -> t -> t
forall a. Num a => a -> a -> a
- t
q t -> t -> t
forall a. Num a => a -> a -> a
* t
y in t
next t -> (t, t) -> (t, t)
forall a b. a -> b -> b
`seq` t -> t -> t -> t -> (t, t)
euclid t
b t
r t
y t
next
modularPower :: Integer -> Integer -> Integer -> Either Error Integer
modularPower :: Integer -> Integer -> Integer -> Either Error Integer
modularPower Integer
value Integer
power Integer
modulus
| Integer
modulus Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 = Error -> Either Error Integer
forall a b. a -> Either a b
Left (Integer -> Error
InvalidModulus Integer
modulus)
| Integer
power Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 = Integer -> Either Error Integer
forall a b. b -> Either a b
Right Integer
1
| Integer
power Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = do
Integer
inverse <- Integer -> Integer -> Either Error Integer
modularInverse Integer
value Integer
modulus
Integer -> Either Error Integer
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer -> Integer -> Integer -> Integer
forall {t}. Integral t => Integer -> t -> Integer -> Integer
square Integer
inverse (Integer -> Integer
forall a. Num a => a -> a
negate Integer
power) Integer
1)
| Bool
otherwise = Integer -> Either Error Integer
forall a b. b -> Either a b
Right (Integer -> Integer -> Integer -> Integer
forall {t}. Integral t => Integer -> t -> Integer -> Integer
square (Integer
value Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
magnitude) Integer
power Integer
1)
where
magnitude :: Integer
magnitude = Integer -> Integer
forall a. Num a => a -> a
abs Integer
modulus
square :: Integer -> t -> Integer -> Integer
square Integer
_ t
0 Integer
result = Integer
result
square Integer
factor t
remaining Integer
result =
let productValue :: Integer
productValue = if t -> Bool
forall a. Integral a => a -> Bool
odd t
remaining then (Integer
result Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
factor) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
magnitude else Integer
result
nextFactor :: Integer
nextFactor = (Integer
factor Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
factor) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
magnitude
in Integer
productValue Integer -> Integer -> Integer
forall a b. a -> b -> b
`seq` Integer
nextFactor Integer -> Integer -> Integer
forall a b. a -> b -> b
`seq` Integer -> t -> Integer -> Integer
square Integer
nextFactor (t
remaining t -> t -> t
forall a. Integral a => a -> a -> a
`quot` t
2) Integer
productValue
asciiSpace :: Char -> Bool
asciiSpace :: Char -> Bool
asciiSpace Char
c = Char
c Char -> [Char] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Char
' ', Char
'\t', Char
'\n', Char
'\r', Char
'\f', Char
'\v']
signed :: Text -> (Bool, Text)
signed :: Text -> (Bool, Text)
signed Text
text =
let prefix :: Text
prefix = (Char -> Bool) -> Text -> Text
T.dropWhile Char -> Bool
asciiSpace ((Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\0') Text
text)
in case Text -> Text -> Maybe Text
T.stripPrefix Text
"-" Text
prefix of Just Text
rest -> (Bool
True, Text
rest); Maybe Text
Nothing -> (Bool
False, Text
prefix)
parseDigits :: Int -> Bool -> Text -> Either Error Integer
parseDigits :: Int -> Bool -> Text -> Either Error Integer
parseDigits Int
radix Bool
negative Text
text
| Text -> Bool
T.null Text
digits = Error -> Either Error Integer
forall a b. a -> Either a b
Left (Text -> Error
InvalidIntegerText Text
"missing digits")
| Bool
otherwise = (if Bool
negative then Integer -> Integer
forall a. Num a => a -> a
negate else Integer -> Integer
forall a. a -> a
id) (Integer -> Integer)
-> Either Error Integer -> Either Error Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Either Error Integer -> Char -> Either Error Integer)
-> Either Error Integer -> Text -> Either Error Integer
forall a. (a -> Char -> a) -> a -> Text -> a
T.foldl' Either Error Integer -> Char -> Either Error Integer
consume (Integer -> Either Error Integer
forall a b. b -> Either a b
Right Integer
0) Text
digits
where
digits :: Text
digits = (Char -> Bool) -> Text -> Text
T.filter (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
asciiSpace) Text
text
consume :: Either Error Integer -> Char -> Either Error Integer
consume failure :: Either Error Integer
failure@(Left Error
_) Char
_ = Either Error Integer
failure
consume (Right Integer
accumulated) Char
c = do
Int
digit <- case () of
()
_ | Char -> Bool
isDigit Char
c -> Int -> Either Error Int
forall a b. b -> Either a b
Right (Char -> Int
ord Char
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Char -> Int
ord Char
'0')
()
_ | Char -> Bool
isAsciiUpper Char
c -> Int -> Either Error Int
forall a b. b -> Either a b
Right (Char -> Int
ord Char
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Char -> Int
ord Char
'A' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
10)
()
_ | Char -> Bool
isAsciiLower Char
c -> Int -> Either Error Int
forall a b. b -> Either a b
Right (Char -> Int
ord Char
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Char -> Int
ord Char
'a' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ if Int
radix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
36 then Int
10 else Int
36)
()
_ -> Error -> Either Error Int
forall a b. a -> Either a b
Left (Text -> Error
InvalidIntegerText Text
"invalid digit")
if Int
digit Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
radix then Error -> Either Error Integer
forall a b. a -> Either a b
Left (Text -> Error
InvalidIntegerText Text
"digit exceeds radix") else let next :: Integer
next = Integer
accumulated Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
radix Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
digit in Integer
next Integer -> Either Error Integer -> Either Error Integer
forall a b. a -> b -> b
`seq` Integer -> Either Error Integer
forall a b. b -> Either a b
Right Integer
next
digitCharacter :: Int -> Int -> Char
digitCharacter :: Int -> Int -> Char
digitCharacter Int
radix Int
digit
| Int
digit Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
10 = Int -> Char
chr (Char -> Int
ord Char
'0' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
digit)
| Int
radix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
36 = Int -> Char
chr (Char -> Int
ord Char
'a' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
digit Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
10)
| Int
digit Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
36 = Int -> Char
chr (Char -> Int
ord Char
'A' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
digit Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
10)
| Bool
otherwise = Int -> Char
chr (Char -> Int
ord Char
'a' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
digit Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
36)