{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns, MagicHash, UnboxedTuples, MultiWayIf #-}
{-# OPTIONS_GHC -O2 -fno-warn-name-shadowing #-}
-- We always optimise this, otherwise performance of a non-optimised
-- compiler is severely affected. This module used to live in the `ghc`
-- package but has been moved to `ghc-boot` because the definition
-- of the package database (needed in both ghc and in ghc-pkg) lives in
-- `ghc-boot` and uses ShortText, which in turn depends on this module.

-- -----------------------------------------------------------------------------
--
-- (c) The University of Glasgow, 1997-2006
--
-- Character encodings
--
-- -----------------------------------------------------------------------------

module GHC.Utils.Encoding (
        -- * UTF-8
        module GHC.Utils.Encoding.UTF8,

        -- * Z-encoding
        UserString,
        EncodedString,
        zEncodeString,
        zDecodeString,

        -- * Base62-encoding
        toBase62,
        toBase62Padded
  ) where

import Prelude

import Foreign
import Data.Char
import qualified Data.Char as Char
import Numeric

import GHC.Utils.Encoding.UTF8

-- -----------------------------------------------------------------------------
-- Note [Z-Encoding]
-- ~~~~~~~~~~~~~~~~~

{-
This is the main name-encoding and decoding function.  It encodes any
string into a string that is acceptable as a C name.  This is done
right before we emit a symbol name into the compiled C or asm code.
Z-encoding of strings is cached in the FastString interface, so we
never encode the same string more than once.

The basic encoding scheme is this.

* Tuples (,,,) are coded as Z3T

* Alphabetic characters (upper and lower) and digits
        all translate to themselves;
        except 'Z', which translates to 'ZZ'
        and    'z', which translates to 'zz'
  We need both so that we can preserve the variable/tycon distinction

* Most other printable characters translate to 'zx' or 'Zx' for some
        alphabetic character x

* The others translate as 'znnnU' where 'nnn' is the decimal number
        of the character

        Before          After
        --------------------------
        Trak            Trak
        foo_wib         foozuwib
        >               zg
        >1              zg1
        foo#            foozh
        foo##           foozhzh
        foo##1          foozhzh1
        fooZ            fooZZ
        :+              ZCzp
        ()              Z0T     0-tuple
        (,,,,)          Z5T     5-tuple
        (# #)           Z1H     unboxed 1-tuple (note the space)
        (#,,,,#)        Z5H     unboxed 5-tuple
                (NB: There is no Z1T nor Z0H.)
-}

type UserString = String        -- As the user typed it
type EncodedString = String     -- Encoded form


zEncodeString :: UserString -> EncodedString
zEncodeString :: UserString -> UserString
zEncodeString UserString
cs = case UserString -> Maybe UserString
maybe_tuple UserString
cs of
                Just UserString
n  -> UserString
n            -- Tuples go to Z2T etc
                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   -- True for chars that don't need encoding
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'

-- If a digit is at the start of a symbol then we need to encode it.
-- Otherwise package names like 9pH-0.1 give linker errors.
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]     -- Common case first

-- Constructors
encode_ch Char
'('  = UserString
"ZL"   -- Needed for things like (,), and (->)
encode_ch Char
')'  = UserString
"ZR"   -- For symmetry with (
encode_ch Char
'['  = UserString
"ZM"
encode_ch Char
']'  = UserString
"ZN"
encode_ch Char
':'  = UserString
"ZC"
encode_ch Char
'Z'  = UserString
"ZZ"

-- Variables
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"
  -- ToDo: we could improve the encoding here in various ways.
  -- eg. strings of unicode characters come out as 'z1234Uz5678U', we
  -- could remove the 'U' in the middle (the 'z' works as a separator).

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  = {-pprTrace "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  = {-pprTrace "decode_lower" (char ch)-} Char
ch

-- Characters not having a specific code are coded as z224U (in hex)
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
        -- NB. recurse back to zDecodeString after decoding the tuple, because
        -- the tuple might be embedded in a longer name.
    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)

{-
Tuples are encoded as
        Z3T or Z3H
for 3-tuples or unboxed 3-tuples respectively.  No other encoding starts
        Z<digit>

* "(# #)" is the tycon for an unboxed 1-tuple (not 0-tuple)
  There are no unboxed 0-tuples.

* "()" is the tycon for a boxed 0-tuple.
  There are no boxed 1-tuples.
-}

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)


{-
************************************************************************
*                                                                      *
                        Base 62
*                                                                      *
************************************************************************

Note [Base 62 encoding 128-bit integers]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Instead of base-62 encoding a single 128-bit integer
(ceil(21.49) characters), we'll base-62 a pair of 64-bit integers
(2 * ceil(10.75) characters).  Luckily for us, it's the same number of
characters!
-}

--------------------------------------------------------------------------
-- Base 62

-- The base-62 code is based off of 'locators'
-- ((c) Operational Dynamics Consulting, BSD3 licensed)

-- | Size of a 64-bit word when written as a base-62 string
word64Base62Len :: Int
word64Base62Len :: Int
word64Base62Len = Int
11

-- | Converts a 64-bit word into a base-62 string
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 -- 11 == ceil(64 / lg 62)
    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!"