GHC截断Unicode字符输出

时间:2011-04-13 20:58:29

标签: unicode haskell ghc

我无法让GHCi或GHC打印unicode代码点221A(sqrt symbol:√)。

我不认为这是我的外壳,因为我可以得到红宝石:

irb> puts "\u221A"
√

GHC / GHCi是另一个问题:

ghci> putStrLn "\8730"

ghci> withFile "temp.out" WriteMode $ flip hPutStrLn "\8730"
ghci> readFile "temp.out"
"\SUB\n"

那么我做错了什么?

(GHC v6.l0.3)

1 个答案:

答案 0 :(得分:10)

GHC使用unicode changed in GHC 6.12.1使用Unicode字符串“做正确的事”的行为。以前的版本在IO上截断为8位字符(强制使用编码库)。

也就是说,'\ 8730'是0x221a,而'\ SUB'是0x1a - 高字节消失了。

这里有GHC 7:

Prelude> print "√\n"
"\8730\n"
Prelude> putStr "√\n"
√
Prelude> putStr "\8730√\n"
√√

但是我用GHC 6.8得到你的结果。像这样:

Prelude> writeFile "/tmp/x" "√\n"
Prelude> readFile "/tmp/x"
"\SUB\n"

因为unicode位被截断为8位。

GHC 7 + IO按预期工作:

Prelude> writeFile "/tmp/x" "\8730√\n"
Prelude> readFile "/tmp/x"
"\8730\8730\n"
Prelude> s <- readFile "/tmp/x"
Prelude> putStr s
√√

您可以升级到GHC 7(在Haskell Platform中)以获得完整的Unicode支持吗?如果无法做到这一点,您可以使用其中一个编码库,例如utf8-string

相关问题