{-# LANGUAGE BangPatterns, MagicHash, UnboxedTuples #-}
{-# OPTIONS_GHC -O2 #-}
module Encoding (
utf8DecodeChar#,
utf8PrevChar,
utf8CharStart,
utf8DecodeChar,
utf8DecodeByteString,
utf8DecodeStringLazy,
utf8EncodeChar,
utf8EncodeString,
utf8EncodedLength,
countUTF8Chars,
zEncodeString,
zDecodeString,
toBase62,
toBase62Padded
) where
import GhcPrelude
import Foreign
import Foreign.ForeignPtr.Unsafe
import Data.Char
import qualified Data.Char as Char
import Numeric
import GHC.IO
import Data.ByteString (ByteString)
import qualified Data.ByteString.Internal as BS
import GHC.Exts
{-# INLINE utf8DecodeChar# #-}
utf8DecodeChar# :: Addr# -> (# Char#, Int# #)
utf8DecodeChar# :: Addr# -> (# Char#, Int# #)
utf8DecodeChar# a# :: Addr#
a# =
let !ch0 :: Int#
ch0 = Word# -> Int#
word2Int# (Addr# -> Int# -> Word#
indexWord8OffAddr# Addr#
a# 0#) in
case () of
_ | Int# -> Bool
isTrue# (Int#
ch0 Int# -> Int# -> Int#
<=# 0x7F#) -> (# Int# -> Char#
chr# Int#
ch0, 1# #)
| Int# -> Bool
isTrue# ((Int#
ch0 Int# -> Int# -> Int#
>=# 0xC0#) Int# -> Int# -> Int#
`andI#` (Int#
ch0 Int# -> Int# -> Int#
<=# 0xDF#)) ->
let !ch1 :: Int#
ch1 = Word# -> Int#
word2Int# (Addr# -> Int# -> Word#
indexWord8OffAddr# Addr#
a# 1#) in
if Int# -> Bool
isTrue# ((Int#
ch1 Int# -> Int# -> Int#
<# 0x80#) Int# -> Int# -> Int#
`orI#` (Int#
ch1 Int# -> Int# -> Int#
>=# 0xC0#)) then Int# -> (# Char#, Int# #)
fail 1# else
(# Int# -> Char#
chr# (((Int#
ch0 Int# -> Int# -> Int#
-# 0xC0#) Int# -> Int# -> Int#
`uncheckedIShiftL#` 6#) Int# -> Int# -> Int#
+#
(Int#
ch1 Int# -> Int# -> Int#
-# 0x80#)),
2# #)
| Int# -> Bool
isTrue# ((Int#
ch0 Int# -> Int# -> Int#
>=# 0xE0#) Int# -> Int# -> Int#
`andI#` (Int#
ch0 Int# -> Int# -> Int#
<=# 0xEF#)) ->
let !ch1 :: Int#
ch1 = Word# -> Int#
word2Int# (Addr# -> Int# -> Word#
indexWord8OffAddr# Addr#
a# 1#) in
if Int# -> Bool
isTrue# ((Int#
ch1 Int# -> Int# -> Int#
<# 0x80#) Int# -> Int# -> Int#
`orI#` (Int#
ch1 Int# -> Int# -> Int#
>=# 0xC0#)) then Int# -> (# Char#, Int# #)
fail 1# else
let !ch2 :: Int#
ch2 = Word# -> Int#
word2Int# (Addr# -> Int# -> Word#
indexWord8OffAddr# Addr#
a# 2#) in
if Int# -> Bool
isTrue# ((Int#
ch2 Int# -> Int# -> Int#
<# 0x80#) Int# -> Int# -> Int#
`orI#` (Int#
ch2 Int# -> Int# -> Int#
>=# 0xC0#)) then Int# -> (# Char#, Int# #)
fail 2# else
(# Int# -> Char#
chr# (((Int#
ch0 Int# -> Int# -> Int#
-# 0xE0#) Int# -> Int# -> Int#
`uncheckedIShiftL#` 12#) Int# -> Int# -> Int#
+#
((Int#
ch1 Int# -> Int# -> Int#
-# 0x80#) Int# -> Int# -> Int#
`uncheckedIShiftL#` 6#) Int# -> Int# -> Int#
+#
(Int#
ch2 Int# -> Int# -> Int#
-# 0x80#)),
3# #)
| Int# -> Bool
isTrue# ((Int#
ch0 Int# -> Int# -> Int#
>=# 0xF0#) Int# -> Int# -> Int#
`andI#` (Int#
ch0 Int# -> Int# -> Int#
<=# 0xF8#)) ->
let !ch1 :: Int#
ch1 = Word# -> Int#
word2Int# (Addr# -> Int# -> Word#
indexWord8OffAddr# Addr#
a# 1#) in
if Int# -> Bool
isTrue# ((Int#
ch1 Int# -> Int# -> Int#
<# 0x80#) Int# -> Int# -> Int#
`orI#` (Int#
ch1 Int# -> Int# -> Int#
>=# 0xC0#)) then Int# -> (# Char#, Int# #)
fail 1# else
let !ch2 :: Int#
ch2 = Word# -> Int#
word2Int# (Addr# -> Int# -> Word#
indexWord8OffAddr# Addr#
a# 2#) in
if Int# -> Bool
isTrue# ((Int#
ch2 Int# -> Int# -> Int#
<# 0x80#) Int# -> Int# -> Int#
`orI#` (Int#
ch2 Int# -> Int# -> Int#
>=# 0xC0#)) then Int# -> (# Char#, Int# #)
fail 2# else
let !ch3 :: Int#
ch3 = Word# -> Int#
word2Int# (Addr# -> Int# -> Word#
indexWord8OffAddr# Addr#
a# 3#) in
if Int# -> Bool
isTrue# ((Int#
ch3 Int# -> Int# -> Int#
<# 0x80#) Int# -> Int# -> Int#
`orI#` (Int#
ch3 Int# -> Int# -> Int#
>=# 0xC0#)) then Int# -> (# Char#, Int# #)
fail 3# else
(# Int# -> Char#
chr# (((Int#
ch0 Int# -> Int# -> Int#
-# 0xF0#) Int# -> Int# -> Int#
`uncheckedIShiftL#` 18#) Int# -> Int# -> Int#
+#
((Int#
ch1 Int# -> Int# -> Int#
-# 0x80#) Int# -> Int# -> Int#
`uncheckedIShiftL#` 12#) Int# -> Int# -> Int#
+#
((Int#
ch2 Int# -> Int# -> Int#
-# 0x80#) Int# -> Int# -> Int#
`uncheckedIShiftL#` 6#) Int# -> Int# -> Int#
+#
(Int#
ch3 Int# -> Int# -> Int#
-# 0x80#)),
4# #)
| Bool
otherwise -> Int# -> (# Char#, Int# #)
fail 1#
where
fail :: Int# -> (# Char#, Int# #)
fail :: Int# -> (# Char#, Int# #)
fail nBytes# :: Int#
nBytes# = (# '\0'#, Int#
nBytes# #)
utf8DecodeChar :: Ptr Word8 -> (Char, Int)
utf8DecodeChar :: Ptr Word8 -> (Char, Int)
utf8DecodeChar (Ptr a# :: Addr#
a#) =
case Addr# -> (# Char#, Int# #)
utf8DecodeChar# Addr#
a# of (# c# :: Char#
c#, nBytes# :: Int#
nBytes# #) -> ( Char# -> Char
C# Char#
c#, Int# -> Int
I# Int#
nBytes# )
utf8PrevChar :: Ptr Word8 -> IO (Ptr Word8)
utf8PrevChar :: Ptr Word8 -> IO (Ptr Word8)
utf8PrevChar p :: Ptr Word8
p = Ptr Word8 -> IO (Ptr Word8)
utf8CharStart (Ptr Word8
p Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` (-1))
utf8CharStart :: Ptr Word8 -> IO (Ptr Word8)
utf8CharStart :: Ptr Word8 -> IO (Ptr Word8)
utf8CharStart p :: Ptr Word8
p = Ptr Word8 -> IO (Ptr Word8)
forall b. (Storable b, Ord b, Num b) => Ptr b -> IO (Ptr b)
go Ptr Word8
p
where go :: Ptr b -> IO (Ptr b)
go p :: Ptr b
p = do b
w <- Ptr b -> IO b
forall a. Storable a => Ptr a -> IO a
peek Ptr b
p
if b
w b -> b -> Bool
forall a. Ord a => a -> a -> Bool
>= 0x80 Bool -> Bool -> Bool
&& b
w b -> b -> Bool
forall a. Ord a => a -> a -> Bool
< 0xC0
then Ptr b -> IO (Ptr b)
go (Ptr b
p Ptr b -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` (-1))
else Ptr b -> IO (Ptr b)
forall (m :: * -> *) a. Monad m => a -> m a
return Ptr b
p
utf8DecodeByteString :: ByteString -> [Char]
utf8DecodeByteString :: ByteString -> [Char]
utf8DecodeByteString (BS.PS ptr :: ForeignPtr Word8
ptr offset :: Int
offset len :: Int
len)
= ForeignPtr Word8 -> Int -> Int -> [Char]
utf8DecodeStringLazy ForeignPtr Word8
ptr Int
offset Int
len
utf8DecodeStringLazy :: ForeignPtr Word8 -> Int -> Int -> [Char]
utf8DecodeStringLazy :: ForeignPtr Word8 -> Int -> Int -> [Char]
utf8DecodeStringLazy fptr :: ForeignPtr Word8
fptr offset :: Int
offset len :: Int
len
= IO [Char] -> [Char]
forall a. IO a -> a
unsafeDupablePerformIO (IO [Char] -> [Char]) -> IO [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ Ptr Any -> IO [Char]
forall a. Ptr a -> IO [Char]
unpack Ptr Any
forall b. Ptr b
start
where
!start :: Ptr b
start = ForeignPtr Word8 -> Ptr Word8
forall a. ForeignPtr a -> Ptr a
unsafeForeignPtrToPtr ForeignPtr Word8
fptr Ptr Word8 -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
offset
!end :: Ptr b
end = Ptr Any
forall b. Ptr b
start Ptr Any -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
len
unpack :: Ptr a -> IO [Char]
unpack p :: Ptr a
p
| Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
>= Ptr a
forall b. Ptr b
end = ForeignPtr Word8 -> IO ()
forall a. ForeignPtr a -> IO ()
touchForeignPtr ForeignPtr Word8
fptr IO () -> IO [Char] -> IO [Char]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Char] -> IO [Char]
forall (m :: * -> *) a. Monad m => a -> m a
return []
| Bool
otherwise =
case Addr# -> (# Char#, Int# #)
utf8DecodeChar# (Ptr a -> Addr#
forall a. Ptr a -> Addr#
unPtr Ptr a
p) of
(# c# :: Char#
c#, nBytes# :: Int#
nBytes# #) -> do
[Char]
rest <- IO [Char] -> IO [Char]
forall a. IO a -> IO a
unsafeDupableInterleaveIO (IO [Char] -> IO [Char]) -> IO [Char] -> IO [Char]
forall a b. (a -> b) -> a -> b
$ Ptr a -> IO [Char]
unpack (Ptr a
p Ptr a -> Int# -> Ptr a
forall a. Ptr a -> Int# -> Ptr a
`plusPtr#` Int#
nBytes#)
[Char] -> IO [Char]
forall (m :: * -> *) a. Monad m => a -> m a
return (Char# -> Char
C# Char#
c# Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char]
rest)
countUTF8Chars :: Ptr Word8 -> Int -> IO Int
countUTF8Chars :: Ptr Word8 -> Int -> IO Int
countUTF8Chars ptr :: Ptr Word8
ptr len :: Int
len = Ptr Word8 -> Int -> IO Int
forall (m :: * -> *) t a. (Monad m, Num t) => Ptr a -> t -> m t
go Ptr Word8
ptr 0
where
!end :: Ptr b
end = Ptr Word8
ptr Ptr Word8 -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
len
go :: Ptr a -> t -> m t
go p :: Ptr a
p !t
n
| Ptr a
p Ptr a -> Ptr a -> Bool
forall a. Ord a => a -> a -> Bool
>= Ptr a
forall b. Ptr b
end = t -> m t
forall (m :: * -> *) a. Monad m => a -> m a
return t
n
| Bool
otherwise = do
case Addr# -> (# Char#, Int# #)
utf8DecodeChar# (Ptr a -> Addr#
forall a. Ptr a -> Addr#
unPtr Ptr a
p) of
(# _, nBytes# :: Int#
nBytes# #) -> Ptr a -> t -> m t
go (Ptr a
p Ptr a -> Int# -> Ptr a
forall a. Ptr a -> Int# -> Ptr a
`plusPtr#` Int#
nBytes#) (t
nt -> t -> t
forall a. Num a => a -> a -> a
+1)
unPtr :: Ptr a -> Addr#
unPtr :: Ptr a -> Addr#
unPtr (Ptr a :: Addr#
a) = Addr#
a
plusPtr# :: Ptr a -> Int# -> Ptr a
plusPtr# :: Ptr a -> Int# -> Ptr a
plusPtr# ptr :: Ptr a
ptr nBytes# :: Int#
nBytes# = Ptr a
ptr Ptr a -> Int -> Ptr a
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` (Int# -> Int
I# Int#
nBytes#)
utf8EncodeChar :: Char -> Ptr Word8 -> IO (Ptr Word8)
utf8EncodeChar :: Char -> Ptr Word8 -> IO (Ptr Word8)
utf8EncodeChar c :: Char
c ptr :: Ptr Word8
ptr =
let x :: Int
x = Char -> Int
ord Char
c in
case () of
_ | Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 Bool -> Bool -> Bool
&& Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0x007f -> do
Ptr Word8 -> Word8 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word8
ptr (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x)
Ptr Word8 -> IO (Ptr Word8)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr Word8
ptr Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` 1)
| Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0x07ff -> do
Ptr Word8 -> Word8 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word8
ptr (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (0xC0 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. ((Int
x Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 6) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x1F)))
Ptr Word8 -> Int -> Word8 -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr Word8
ptr 1 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (0x80 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
x Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x3F)))
Ptr Word8 -> IO (Ptr Word8)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr Word8
ptr Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` 2)
| Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0xffff -> do
Ptr Word8 -> Word8 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word8
ptr (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (0xE0 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
x Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 12) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x0F))
Ptr Word8 -> Int -> Word8 -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr Word8
ptr 1 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (0x80 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
x Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 6) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x3F))
Ptr Word8 -> Int -> Word8 -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr Word8
ptr 2 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (0x80 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
x Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x3F)))
Ptr Word8 -> IO (Ptr Word8)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr Word8
ptr Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` 3)
| Bool
otherwise -> do
Ptr Word8 -> Word8 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word8
ptr (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (0xF0 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
x Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 18)))
Ptr Word8 -> Int -> Word8 -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr Word8
ptr 1 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (0x80 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. ((Int
x Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 12) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x3F)))
Ptr Word8 -> Int -> Word8 -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr Word8
ptr 2 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (0x80 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. ((Int
x Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 6) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x3F)))
Ptr Word8 -> Int -> Word8 -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr Word8
ptr 3 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (0x80 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
x Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x3F)))
Ptr Word8 -> IO (Ptr Word8)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr Word8
ptr Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` 4)
utf8EncodeString :: Ptr Word8 -> String -> IO ()
utf8EncodeString :: Ptr Word8 -> [Char] -> IO ()
utf8EncodeString ptr :: Ptr Word8
ptr str :: [Char]
str = Ptr Word8 -> [Char] -> IO ()
go Ptr Word8
ptr [Char]
str
where go :: Ptr Word8 -> [Char] -> IO ()
go !Ptr Word8
_ [] = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
go ptr :: Ptr Word8
ptr (c :: Char
c:cs :: [Char]
cs) = do
Ptr Word8
ptr' <- Char -> Ptr Word8 -> IO (Ptr Word8)
utf8EncodeChar Char
c Ptr Word8
ptr
Ptr Word8 -> [Char] -> IO ()
go Ptr Word8
ptr' [Char]
cs
utf8EncodedLength :: String -> Int
utf8EncodedLength :: [Char] -> Int
utf8EncodedLength str :: [Char]
str = Int -> [Char] -> Int
forall a. Num a => a -> [Char] -> a
go 0 [Char]
str
where go :: a -> [Char] -> a
go !a
n [] = a
n
go n :: a
n (c :: Char
c:cs :: [Char]
cs)
| Char -> Int
ord Char
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 Bool -> Bool -> Bool
&& Char -> Int
ord Char
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0x007f = a -> [Char] -> a
go (a
na -> a -> a
forall a. Num a => a -> a -> a
+1) [Char]
cs
| Char -> Int
ord Char
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0x07ff = a -> [Char] -> a
go (a
na -> a -> a
forall a. Num a => a -> a -> a
+2) [Char]
cs
| Char -> Int
ord Char
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0xffff = a -> [Char] -> a
go (a
na -> a -> a
forall a. Num a => a -> a -> a
+3) [Char]
cs
| Bool
otherwise = a -> [Char] -> a
go (a
na -> a -> a
forall a. Num a => a -> a -> a
+4) [Char]
cs
type UserString = String
type EncodedString = String
zEncodeString :: UserString -> EncodedString
zEncodeString :: [Char] -> [Char]
zEncodeString cs :: [Char]
cs = case [Char] -> Maybe [Char]
maybe_tuple [Char]
cs of
Just n :: [Char]
n -> [Char]
n
Nothing -> [Char] -> [Char]
go [Char]
cs
where
go :: [Char] -> [Char]
go [] = []
go (c :: Char
c:cs :: [Char]
cs) = Char -> [Char]
encode_digit_ch Char
c [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
go' [Char]
cs
go' :: [Char] -> [Char]
go' [] = []
go' (c :: Char
c:cs :: [Char]
cs) = Char -> [Char]
encode_ch Char
c [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
go' [Char]
cs
unencodedChar :: Char -> Bool
unencodedChar :: Char -> Bool
unencodedChar 'Z' = Bool
False
unencodedChar 'z' = Bool
False
unencodedChar c :: Char
c = Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= 'a' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= 'z'
Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= 'A' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= 'Z'
Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= '0' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= '9'
encode_digit_ch :: Char -> EncodedString
encode_digit_ch :: Char -> [Char]
encode_digit_ch c :: Char
c | Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= '0' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= '9' = Char -> [Char]
encode_as_unicode_char Char
c
encode_digit_ch c :: Char
c | Bool
otherwise = Char -> [Char]
encode_ch Char
c
encode_ch :: Char -> EncodedString
encode_ch :: Char -> [Char]
encode_ch c :: Char
c | Char -> Bool
unencodedChar Char
c = [Char
c]
encode_ch '(' = "ZL"
encode_ch ')' = "ZR"
encode_ch '[' = "ZM"
encode_ch ']' = "ZN"
encode_ch ':' = "ZC"
encode_ch 'Z' = "ZZ"
encode_ch 'z' = "zz"
encode_ch '&' = "za"
encode_ch '|' = "zb"
encode_ch '^' = "zc"
encode_ch '$' = "zd"
encode_ch '=' = "ze"
encode_ch '>' = "zg"
encode_ch '#' = "zh"
encode_ch '.' = "zi"
encode_ch '<' = "zl"
encode_ch '-' = "zm"
encode_ch '!' = "zn"
encode_ch '+' = "zp"
encode_ch '\'' = "zq"
encode_ch '\\' = "zr"
encode_ch '/' = "zs"
encode_ch '*' = "zt"
encode_ch '_' = "zu"
encode_ch '%' = "zv"
encode_ch c :: Char
c = Char -> [Char]
encode_as_unicode_char Char
c
encode_as_unicode_char :: Char -> EncodedString
encode_as_unicode_char :: Char -> [Char]
encode_as_unicode_char c :: Char
c = 'z' Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: if Char -> Bool
isDigit ([Char] -> Char
forall a. [a] -> a
head [Char]
hex_str) then [Char]
hex_str
else '0'Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
:[Char]
hex_str
where hex_str :: [Char]
hex_str = Int -> [Char] -> [Char]
forall a. (Integral a, Show a) => a -> [Char] -> [Char]
showHex (Char -> Int
ord Char
c) "U"
zDecodeString :: EncodedString -> UserString
zDecodeString :: [Char] -> [Char]
zDecodeString [] = []
zDecodeString ('Z' : d :: Char
d : rest :: [Char]
rest)
| Char -> Bool
isDigit Char
d = Char -> [Char] -> [Char]
decode_tuple Char
d [Char]
rest
| Bool
otherwise = Char -> Char
decode_upper Char
d Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char] -> [Char]
zDecodeString [Char]
rest
zDecodeString ('z' : d :: Char
d : rest :: [Char]
rest)
| Char -> Bool
isDigit Char
d = Char -> [Char] -> [Char]
decode_num_esc Char
d [Char]
rest
| Bool
otherwise = Char -> Char
decode_lower Char
d Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char] -> [Char]
zDecodeString [Char]
rest
zDecodeString (c :: Char
c : rest :: [Char]
rest) = Char
c Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char] -> [Char]
zDecodeString [Char]
rest
decode_upper, decode_lower :: Char -> Char
decode_upper :: Char -> Char
decode_upper 'L' = '('
decode_upper 'R' = ')'
decode_upper 'M' = '['
decode_upper 'N' = ']'
decode_upper 'C' = ':'
decode_upper 'Z' = 'Z'
decode_upper ch :: Char
ch = Char
ch
decode_lower :: Char -> Char
decode_lower 'z' = 'z'
decode_lower 'a' = '&'
decode_lower 'b' = '|'
decode_lower 'c' = '^'
decode_lower 'd' = '$'
decode_lower 'e' = '='
decode_lower 'g' = '>'
decode_lower 'h' = '#'
decode_lower 'i' = '.'
decode_lower 'l' = '<'
decode_lower 'm' = '-'
decode_lower 'n' = '!'
decode_lower 'p' = '+'
decode_lower 'q' = '\''
decode_lower 'r' = '\\'
decode_lower 's' = '/'
decode_lower 't' = '*'
decode_lower 'u' = '_'
decode_lower 'v' = '%'
decode_lower ch :: Char
ch = Char
ch
decode_num_esc :: Char -> EncodedString -> UserString
decode_num_esc :: Char -> [Char] -> [Char]
decode_num_esc d :: Char
d rest :: [Char]
rest
= Int -> [Char] -> [Char]
go (Char -> Int
digitToInt Char
d) [Char]
rest
where
go :: Int -> [Char] -> [Char]
go n :: Int
n (c :: Char
c : rest :: [Char]
rest) | Char -> Bool
isHexDigit Char
c = Int -> [Char] -> [Char]
go (16Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Char -> Int
digitToInt Char
c) [Char]
rest
go n :: Int
n ('U' : rest :: [Char]
rest) = Int -> Char
chr Int
n Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: [Char] -> [Char]
zDecodeString [Char]
rest
go n :: Int
n other :: [Char]
other = [Char] -> [Char]
forall a. HasCallStack => [Char] -> a
error ("decode_num_esc: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ' 'Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
:[Char]
other)
decode_tuple :: Char -> EncodedString -> UserString
decode_tuple :: Char -> [Char] -> [Char]
decode_tuple d :: Char
d rest :: [Char]
rest
= Int -> [Char] -> [Char]
go (Char -> Int
digitToInt Char
d) [Char]
rest
where
go :: Int -> [Char] -> [Char]
go n :: Int
n (c :: Char
c : rest :: [Char]
rest) | Char -> Bool
isDigit Char
c = Int -> [Char] -> [Char]
go (10Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Char -> Int
digitToInt Char
c) [Char]
rest
go 0 ('T':rest :: [Char]
rest) = "()" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
zDecodeString [Char]
rest
go n :: Int
n ('T':rest :: [Char]
rest) = '(' Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Int -> Char -> [Char]
forall a. Int -> a -> [a]
replicate (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) ',' [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ")" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
zDecodeString [Char]
rest
go 1 ('H':rest :: [Char]
rest) = "(# #)" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
zDecodeString [Char]
rest
go n :: Int
n ('H':rest :: [Char]
rest) = '(' Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: '#' Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Int -> Char -> [Char]
forall a. Int -> a -> [a]
replicate (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) ',' [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ "#)" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
zDecodeString [Char]
rest
go n :: Int
n other :: [Char]
other = [Char] -> [Char]
forall a. HasCallStack => [Char] -> a
error ("decode_tuple: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ' 'Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
:[Char]
other)
maybe_tuple :: UserString -> Maybe EncodedString
maybe_tuple :: [Char] -> Maybe [Char]
maybe_tuple "(# #)" = [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just("Z1H")
maybe_tuple ('(' : '#' : cs :: [Char]
cs) = case Int -> [Char] -> (Int, [Char])
count_commas (0::Int) [Char]
cs of
(n :: Int
n, '#' : ')' : _) -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just ('Z' Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Int -> [Char] -> [Char]
forall a. Show a => a -> [Char] -> [Char]
shows (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+1) "H")
_ -> Maybe [Char]
forall a. Maybe a
Nothing
maybe_tuple "()" = [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just("Z0T")
maybe_tuple ('(' : cs :: [Char]
cs) = case Int -> [Char] -> (Int, [Char])
count_commas (0::Int) [Char]
cs of
(n :: Int
n, ')' : _) -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just ('Z' Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Int -> [Char] -> [Char]
forall a. Show a => a -> [Char] -> [Char]
shows (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+1) "T")
_ -> Maybe [Char]
forall a. Maybe a
Nothing
maybe_tuple _ = Maybe [Char]
forall a. Maybe a
Nothing
count_commas :: Int -> String -> (Int, String)
count_commas :: Int -> [Char] -> (Int, [Char])
count_commas n :: Int
n (',' : cs :: [Char]
cs) = Int -> [Char] -> (Int, [Char])
count_commas (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+1) [Char]
cs
count_commas n :: Int
n cs :: [Char]
cs = (Int
n,[Char]
cs)
word64Base62Len :: Int
word64Base62Len :: Int
word64Base62Len = 11
toBase62Padded :: Word64 -> String
toBase62Padded :: Word64 -> [Char]
toBase62Padded w :: Word64
w = [Char]
pad [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
str
where
pad :: [Char]
pad = Int -> Char -> [Char]
forall a. Int -> a -> [a]
replicate Int
len '0'
len :: Int
len = Int
word64Base62Len Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Char] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Char]
str
str :: [Char]
str = Word64 -> [Char]
toBase62 Word64
w
toBase62 :: Word64 -> String
toBase62 :: Word64 -> [Char]
toBase62 w :: Word64
w = Word64 -> (Int -> Char) -> Word64 -> [Char] -> [Char]
forall a.
(Integral a, Show a) =>
a -> (Int -> Char) -> a -> [Char] -> [Char]
showIntAtBase 62 Int -> Char
represent Word64
w ""
where
represent :: Int -> Char
represent :: Int -> Char
represent x :: Int
x
| Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 10 = Int -> Char
Char.chr (48 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
x)
| Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 36 = Int -> Char
Char.chr (65 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
- 10)
| Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 62 = Int -> Char
Char.chr (97 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
- 36)
| Bool
otherwise = [Char] -> Char
forall a. HasCallStack => [Char] -> a
error "represent (base 62): impossible!"