{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns, MagicHash, UnboxedTuples, MultiWayIf #-}
{-# OPTIONS_GHC -O2 -fno-warn-name-shadowing #-}
module GHC.Utils.Encoding (
module GHC.Utils.Encoding.UTF8,
UserString,
EncodedString,
zEncodeString,
zDecodeString,
toBase62,
toBase62Padded
) where
import Prelude
import Foreign
import Data.Char
import qualified Data.Char as Char
import Numeric
import GHC.Utils.Encoding.UTF8
type UserString = String
type EncodedString = String
zEncodeString :: UserString -> EncodedString
zEncodeString :: UserString -> UserString
zEncodeString UserString
cs = case UserString -> Maybe UserString
maybe_tuple UserString
cs of
Just UserString
n -> UserString
n
Maybe UserString
Nothing -> UserString -> UserString
go UserString
cs
where
go :: UserString -> UserString
go [] = []
go (Char
c:UserString
cs) = Char -> UserString
encode_digit_ch Char
c forall a. [a] -> [a] -> [a]
++ UserString -> UserString
go' UserString
cs
go' :: UserString -> UserString
go' [] = []
go' (Char
c:UserString
cs) = Char -> UserString
encode_ch Char
c forall a. [a] -> [a] -> [a]
++ UserString -> UserString
go' UserString
cs
unencodedChar :: Char -> Bool
unencodedChar :: Char -> Bool
unencodedChar Char
'Z' = Bool
False
unencodedChar Char
'z' = Bool
False
unencodedChar Char
c = Char
c forall a. Ord a => a -> a -> Bool
>= Char
'a' Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'z'
Bool -> Bool -> Bool
|| Char
c forall a. Ord a => a -> a -> Bool
>= Char
'A' Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'Z'
Bool -> Bool -> Bool
|| Char
c forall a. Ord a => a -> a -> Bool
>= Char
'0' Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'9'
encode_digit_ch :: Char -> EncodedString
encode_digit_ch :: Char -> UserString
encode_digit_ch Char
c | Char
c forall a. Ord a => a -> a -> Bool
>= Char
'0' Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'9' = Char -> UserString
encode_as_unicode_char Char
c
encode_digit_ch Char
c | Bool
otherwise = Char -> UserString
encode_ch Char
c
encode_ch :: Char -> EncodedString
encode_ch :: Char -> UserString
encode_ch Char
c | Char -> Bool
unencodedChar Char
c = [Char
c]
encode_ch Char
'(' = UserString
"ZL"
encode_ch Char
')' = UserString
"ZR"
encode_ch Char
'[' = UserString
"ZM"
encode_ch Char
']' = UserString
"ZN"
encode_ch Char
':' = UserString
"ZC"
encode_ch Char
'Z' = UserString
"ZZ"
encode_ch Char
'z' = UserString
"zz"
encode_ch Char
'&' = UserString
"za"
encode_ch Char
'|' = UserString
"zb"
encode_ch Char
'^' = UserString
"zc"
encode_ch Char
'$' = UserString
"zd"
encode_ch Char
'=' = UserString
"ze"
encode_ch Char
'>' = UserString
"zg"
encode_ch Char
'#' = UserString
"zh"
encode_ch Char
'.' = UserString
"zi"
encode_ch Char
'<' = UserString
"zl"
encode_ch Char
'-' = UserString
"zm"
encode_ch Char
'!' = UserString
"zn"
encode_ch Char
'+' = UserString
"zp"
encode_ch Char
'\'' = UserString
"zq"
encode_ch Char
'\\' = UserString
"zr"
encode_ch Char
'/' = UserString
"zs"
encode_ch Char
'*' = UserString
"zt"
encode_ch Char
'_' = UserString
"zu"
encode_ch Char
'%' = UserString
"zv"
encode_ch Char
c = Char -> UserString
encode_as_unicode_char Char
c
encode_as_unicode_char :: Char -> EncodedString
encode_as_unicode_char :: Char -> UserString
encode_as_unicode_char Char
c = Char
'z' forall a. a -> [a] -> [a]
: case UserString
hex_str of
Char
hd : UserString
_
| Char -> Bool
isDigit Char
hd -> UserString
hex_str
UserString
_ -> Char
'0' forall a. a -> [a] -> [a]
: UserString
hex_str
where hex_str :: UserString
hex_str = forall a. (Integral a, Show a) => a -> UserString -> UserString
showHex (Char -> Int
ord Char
c) UserString
"U"
zDecodeString :: EncodedString -> UserString
zDecodeString :: UserString -> UserString
zDecodeString [] = []
zDecodeString (Char
'Z' : Char
d : UserString
rest)
| Char -> Bool
isDigit Char
d = Char -> UserString -> UserString
decode_tuple Char
d UserString
rest
| Bool
otherwise = Char -> Char
decode_upper Char
d forall a. a -> [a] -> [a]
: UserString -> UserString
zDecodeString UserString
rest
zDecodeString (Char
'z' : Char
d : UserString
rest)
| Char -> Bool
isDigit Char
d = Char -> UserString -> UserString
decode_num_esc Char
d UserString
rest
| Bool
otherwise = Char -> Char
decode_lower Char
d forall a. a -> [a] -> [a]
: UserString -> UserString
zDecodeString UserString
rest
zDecodeString (Char
c : UserString
rest) = Char
c forall a. a -> [a] -> [a]
: UserString -> UserString
zDecodeString UserString
rest
decode_upper, decode_lower :: Char -> Char
decode_upper :: Char -> Char
decode_upper Char
'L' = Char
'('
decode_upper Char
'R' = Char
')'
decode_upper Char
'M' = Char
'['
decode_upper Char
'N' = Char
']'
decode_upper Char
'C' = Char
':'
decode_upper Char
'Z' = Char
'Z'
decode_upper Char
ch = Char
ch
decode_lower :: Char -> Char
decode_lower Char
'z' = Char
'z'
decode_lower Char
'a' = Char
'&'
decode_lower Char
'b' = Char
'|'
decode_lower Char
'c' = Char
'^'
decode_lower Char
'd' = Char
'$'
decode_lower Char
'e' = Char
'='
decode_lower Char
'g' = Char
'>'
decode_lower Char
'h' = Char
'#'
decode_lower Char
'i' = Char
'.'
decode_lower Char
'l' = Char
'<'
decode_lower Char
'm' = Char
'-'
decode_lower Char
'n' = Char
'!'
decode_lower Char
'p' = Char
'+'
decode_lower Char
'q' = Char
'\''
decode_lower Char
'r' = Char
'\\'
decode_lower Char
's' = Char
'/'
decode_lower Char
't' = Char
'*'
decode_lower Char
'u' = Char
'_'
decode_lower Char
'v' = Char
'%'
decode_lower Char
ch = Char
ch
decode_num_esc :: Char -> EncodedString -> UserString
decode_num_esc :: Char -> UserString -> UserString
decode_num_esc Char
d UserString
rest
= Int -> UserString -> UserString
go (Char -> Int
digitToInt Char
d) UserString
rest
where
go :: Int -> UserString -> UserString
go Int
n (Char
c : UserString
rest) | Char -> Bool
isHexDigit Char
c = Int -> UserString -> UserString
go (Int
16forall a. Num a => a -> a -> a
*Int
n forall a. Num a => a -> a -> a
+ Char -> Int
digitToInt Char
c) UserString
rest
go Int
n (Char
'U' : UserString
rest) = Int -> Char
chr Int
n forall a. a -> [a] -> [a]
: UserString -> UserString
zDecodeString UserString
rest
go Int
n UserString
other = forall a. HasCallStack => UserString -> a
error (UserString
"decode_num_esc: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> UserString
show Int
n forall a. [a] -> [a] -> [a]
++ Char
' 'forall a. a -> [a] -> [a]
:UserString
other)
decode_tuple :: Char -> EncodedString -> UserString
decode_tuple :: Char -> UserString -> UserString
decode_tuple Char
d UserString
rest
= Int -> UserString -> UserString
go (Char -> Int
digitToInt Char
d) UserString
rest
where
go :: Int -> UserString -> UserString
go Int
n (Char
c : UserString
rest) | Char -> Bool
isDigit Char
c = Int -> UserString -> UserString
go (Int
10forall a. Num a => a -> a -> a
*Int
n forall a. Num a => a -> a -> a
+ Char -> Int
digitToInt Char
c) UserString
rest
go Int
0 (Char
'T':UserString
rest) = UserString
"()" forall a. [a] -> [a] -> [a]
++ UserString -> UserString
zDecodeString UserString
rest
go Int
n (Char
'T':UserString
rest) = Char
'(' forall a. a -> [a] -> [a]
: forall a. Int -> a -> [a]
replicate (Int
nforall a. Num a => a -> a -> a
-Int
1) Char
',' forall a. [a] -> [a] -> [a]
++ UserString
")" forall a. [a] -> [a] -> [a]
++ UserString -> UserString
zDecodeString UserString
rest
go Int
1 (Char
'H':UserString
rest) = UserString
"(# #)" forall a. [a] -> [a] -> [a]
++ UserString -> UserString
zDecodeString UserString
rest
go Int
n (Char
'H':UserString
rest) = Char
'(' forall a. a -> [a] -> [a]
: Char
'#' forall a. a -> [a] -> [a]
: forall a. Int -> a -> [a]
replicate (Int
nforall a. Num a => a -> a -> a
-Int
1) Char
',' forall a. [a] -> [a] -> [a]
++ UserString
"#)" forall a. [a] -> [a] -> [a]
++ UserString -> UserString
zDecodeString UserString
rest
go Int
n UserString
other = forall a. HasCallStack => UserString -> a
error (UserString
"decode_tuple: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> UserString
show Int
n forall a. [a] -> [a] -> [a]
++ Char
' 'forall a. a -> [a] -> [a]
:UserString
other)
maybe_tuple :: UserString -> Maybe EncodedString
maybe_tuple :: UserString -> Maybe UserString
maybe_tuple UserString
"(# #)" = forall a. a -> Maybe a
Just(UserString
"Z1H")
maybe_tuple (Char
'(' : Char
'#' : UserString
cs) = case Int -> UserString -> (Int, UserString)
count_commas (Int
0::Int) UserString
cs of
(Int
n, Char
'#' : Char
')' : UserString
_) -> forall a. a -> Maybe a
Just (Char
'Z' forall a. a -> [a] -> [a]
: forall a. Show a => a -> UserString -> UserString
shows (Int
nforall a. Num a => a -> a -> a
+Int
1) UserString
"H")
(Int, UserString)
_ -> forall a. Maybe a
Nothing
maybe_tuple UserString
"()" = forall a. a -> Maybe a
Just(UserString
"Z0T")
maybe_tuple (Char
'(' : UserString
cs) = case Int -> UserString -> (Int, UserString)
count_commas (Int
0::Int) UserString
cs of
(Int
n, Char
')' : UserString
_) -> forall a. a -> Maybe a
Just (Char
'Z' forall a. a -> [a] -> [a]
: forall a. Show a => a -> UserString -> UserString
shows (Int
nforall a. Num a => a -> a -> a
+Int
1) UserString
"T")
(Int, UserString)
_ -> forall a. Maybe a
Nothing
maybe_tuple UserString
_ = forall a. Maybe a
Nothing
count_commas :: Int -> String -> (Int, String)
count_commas :: Int -> UserString -> (Int, UserString)
count_commas Int
n (Char
',' : UserString
cs) = Int -> UserString -> (Int, UserString)
count_commas (Int
nforall a. Num a => a -> a -> a
+Int
1) UserString
cs
count_commas Int
n UserString
cs = (Int
n,UserString
cs)
word64Base62Len :: Int
word64Base62Len :: Int
word64Base62Len = Int
11
toBase62Padded :: Word64 -> String
toBase62Padded :: Word64 -> UserString
toBase62Padded Word64
w = UserString
pad forall a. [a] -> [a] -> [a]
++ UserString
str
where
pad :: UserString
pad = forall a. Int -> a -> [a]
replicate Int
len Char
'0'
len :: Int
len = Int
word64Base62Len forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. Foldable t => t a -> Int
length UserString
str
str :: UserString
str = Word64 -> UserString
toBase62 Word64
w
toBase62 :: Word64 -> String
toBase62 :: Word64 -> UserString
toBase62 Word64
w = forall a.
(Integral a, Show a) =>
a -> (Int -> Char) -> a -> UserString -> UserString
showIntAtBase Word64
62 Int -> Char
represent Word64
w UserString
""
where
represent :: Int -> Char
represent :: Int -> Char
represent Int
x
| Int
x forall a. Ord a => a -> a -> Bool
< Int
10 = Int -> Char
Char.chr (Int
48 forall a. Num a => a -> a -> a
+ Int
x)
| Int
x forall a. Ord a => a -> a -> Bool
< Int
36 = Int -> Char
Char.chr (Int
65 forall a. Num a => a -> a -> a
+ Int
x forall a. Num a => a -> a -> a
- Int
10)
| Int
x forall a. Ord a => a -> a -> Bool
< Int
62 = Int -> Char
Char.chr (Int
97 forall a. Num a => a -> a -> a
+ Int
x forall a. Num a => a -> a -> a
- Int
36)
| Bool
otherwise = forall a. HasCallStack => UserString -> a
error UserString
"represent (base 62): impossible!"