UCS4⇒UTF8変換

http://d.hatena.ne.jp/ha-tan/20070102

ここにあるのを参考にさせてもらった (流用した)
ただこれはおそらく

Cinnamon/Ucs.hs の ucs4CharToUtf8Chars で
  UCS-4 range (hex.)    UTF-8 octet sequence (binary)
  0000 0000-0000 007F   0xxxxxxx

の部分の変換がおかしい気がする

という事でHaskellはじめたばっかりで全然効率的ではない方法で直してみる

ucs4CharToUtf8Chars :: Char -> [Char]
ucs4CharToUtf8Chars c                            ----   v--fix By INA
  | c' <= 0x0000007f = map chr $ marks 0x00 $ masks 1 0x7f  
  | c' <= 0x000007ff = map chr $ marks 0xc0 $ masks 2 0x3f
  | c' <= 0x0000ffff = map chr $ marks 0xe0 $ masks 3 0x3f
  | c' <= 0x1fffffff = map chr $ marks 0xf0 $ masks 4 0x3f
  | c' <= 0x3fffffff = map chr $ marks 0xf8 $ masks 5 0x3f
  | c' <= 0x7fffffff = map chr $ marks 0xfc $ masks 6 0x3f
  | otherwise        = error "ucs4CharToUtf8Char: out of range."
  where
    c' :: Int
    c' = ord c

    mask :: Int -> Int -> Int
    mask m n = (c' `shiftR` (n * 6)) .&. m

    masks :: Int  -> Int -> [Int]                   ---fix add Int parameter
    masks n m= map (mask m) $ reverse $ [0 .. n - 1]

    marks :: Int -> [Int] -> [Int]
    marks m0 = zipWith (\ m c -> m .|. c) (m0 : repeat 0x80)