module DDC.Core.Salt.Name.Sanitize
( sanitizeName
, sanitizeGlobal
, sanitizeLocal)
where
import Data.Maybe
sanitizeName :: String -> String
sanitizeName str
= let hasSymbols = any isJust $ map convertSymbol str
in if hasSymbols
then "_sym_" ++ concatMap rewriteChar str
else str
sanitizeGlobal :: String -> String
sanitizeGlobal = sanitizeName
sanitizeLocal :: String -> String
sanitizeLocal str
= "_" ++ sanitizeGlobal str
rewriteChar :: Char -> String
rewriteChar c
| Just str <- convertSymbol c = "Z" ++ str
| 'Z' <- c = "ZZ"
| otherwise = [c]
convertSymbol :: Char -> Maybe String
convertSymbol c
= case c of
'!' -> Just "Bg"
'@' -> Just "At"
'#' -> Just "Hs"
'$' -> Just "Dl"
'%' -> Just "Pc"
'^' -> Just "Ht"
'&' -> Just "An"
'*' -> Just "St"
'~' -> Just "Tl"
'-' -> Just "Ms"
'+' -> Just "Ps"
'=' -> Just "Eq"
'|' -> Just "Pp"
'\\' -> Just "Bs"
'/' -> Just "Fs"
':' -> Just "Cl"
'.' -> Just "Dt"
'?' -> Just "Qm"
'<' -> Just "Lt"
'>' -> Just "Gt"
'[' -> Just "Br"
']' -> Just "Kt"
'\'' -> Just "Pm"
'`' -> Just "Bt"
_ -> Nothing