{-# LANGUAGE ViewPatterns #-}
import Prelude hiding (lookup)
import Data.Maybe
import Data.Function
import System.IO.Unsafe
import Control.Applicative
import Data.Char
import qualified Data.HashMap.Strict as M
import Data.HashMap.Strict ((!?))

splitOn :: Eq a => a -> [a] -> [[a]]
splitOn x l  = let (before, after) = span (/= x) l
                in before:splitOn' x after
               where splitOn' x (match:after) = splitOn x after
                     splitOn' x [] = []

nameMap :: M.HashMap String Char
{-# NOINLINE nameMap #-}
nameMap = unsafePerformIO getUCDMap

lookup = (nameMap !?)

dsTrans :: String -> String
dsTrans = map $ \c ->
    lookup ("mathematical double-struck capital " ++ [c]) <|>
    lookup ("double-struck capital " ++ [c]) &
    fromMaybe c

getUCDMap = do
    ucd <- lines <$> readFile "UnicodeData.txt"
    return $ M.fromList $ (\s -> let (num:name:rest) = splitOn ';' s in (toLower <$> name, chr . read $ "0x" ++ num)) <$> ucd
