简体   繁体   中英

GHC truncating Unicode character output

I can't get GHCi or GHC to print unicode codepoint 221A (sqrt symbol: √).

I don't think it's my shell, because I can get ruby to do it:

irb> puts "\u221A"
√

GHC/GHCi is another issue:

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's behavior with unicode changed in GHC 6.12.1 to "do the right thing" with Unicode strings. Prior versions truncate to 8 bit characters on IO (forcing the use of an encoding library).

That is, '\\8730' is 0x221a, while '\\SUB' is 0x1a -- the high byte is gone.

Here with GHC 7:

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

But I get your result with 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.

GHC 7 + IO works as expected:

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? If this is not possible, you can use one of the encoding libraries, such as utf8-string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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