简体   繁体   English

GHC截断Unicode字符输出

[英]GHC truncating Unicode character output

I can't get GHCi or GHC to print unicode codepoint 221A (sqrt symbol: √). 我不能让GHCi或GHC打印unicode代码点221A(sqrt符号:√)。

I don't think it's my shell, because I can get ruby to do it: 我不认为这是我的shell,因为我可以得到ruby:

irb> puts "\u221A"
√

GHC/GHCi is another issue: GHC / GHCi是另一个问题:

ghci> putStrLn "\8730"

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

So what am I doing wrong? 那么我做错了什么?

(GHC v6.l0.3) (GHC v6.l0.3)

GHC's behavior with unicode changed in GHC 6.12.1 to "do the right thing" with Unicode strings. GHC的unicode行为在GHC 6.12.1中改为使用Unicode字符串“做正确的事”。 Prior versions truncate to 8 bit characters on IO (forcing the use of an encoding library). 先前版本在IO上截断为8位字符(强制使用编码库)。

That is, '\\8730' is 0x221a, while '\\SUB' is 0x1a -- the high byte is gone. 也就是说,'\\ 8730'是0x221a,而'\\ SUB'是0x1a - 高字节消失了。

Here with GHC 7: 这里有GHC 7:

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

But I get your result with GHC 6.8. 但是我用GHC 6.8得到你的结果。 Like this: 像这样:

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

as the unicode bits are being truncated to 8 bits. 因为unicode位被截断为8位。

GHC 7 + IO works as expected: GHC 7 + IO按预期工作:

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

Can you upgrade to GHC 7 (in the Haskell Platform ) to get full Unicode support? 您可以升级到GHC 7(在Haskell平台中 )以获得完整的Unicode支持吗? If this is not possible, you can use one of the encoding libraries, such as utf8-string 如果无法做到这一点,您可以使用其中一个编码库,例如utf8-string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM