[Solved]What the range of characters that `putStrLn` support?

It seems that io functions like putStrLn not support putting all unicode characters like:

1.OtherSymbol,like emoji

ghci> putStrLn "🏅"
*** Exception: <stdout>: hPutChar: invalid argument (invalid character)
ghci> putStrLn "🥰"
*** Exception: <stdout>: hPutChar: invalid argument (invalid character)
ghci> import Text.Unicode

2.Some Math Symbol like exist,sum:

ghci> putStrLn "⅀"
*** Exception: <stdout>: hPutChar: invalid argument (invalid character)
ghci> putStrLn "∃"
*** Exception: <stdout>: hPutChar: invalid argument (invalid character)

3.Modifier Letter,like small modifier letter ʲ

ghci> putStrLn "ʲ"
*** Exception: <stdout>: hPutChar: invalid argument (invalid character)
ghci> putStrLn "ʰ"
*** Exception: <stdout>: hPutChar: invalid argument (invalid character)
ghci> putStrLn "ᵂ"
*** Exception: <stdout>: hPutChar: invalid argument (invalid character)
ghci> putStrLn "ꓽ"
*** Exception: <stdout>: hPutChar: invalid argument (invalid character)

4.Some Other Letter,like jeem:

ghci> putStrLn "ج"
*** Exception: <stdout>: hPutChar: invalid argument (invalid character)

5.Titlecase Letter,like:

ghci> putStrLn "Dž"
*** Exception: <stdout>: hPutChar: invalid argument (invalid character)

…others I haven’t test

I have searched for base's documents,but haven’t found the answers.Does someone have some ideas or reference to offer?

1 Like

You’re most probably looking for this section in System.IO. Either change your default locale or use hSetEncoding stdout utf8 before writing to stdout.

2 Likes

:smiling_face_with_three_hearts:Thanks!Sorry for I just search around the putStrLn that miss this documents.

Hi Wendao, with questions like this it’s always useful to know what platform you’re using. hPutChar: invalid argument gets many hits at StackOverflow, and are mostly reported from Windows users. (Are you using some sort of shell or terminal emulator?) So if @BurningWitness’s link doesn’t help, there’s:

And some more links from them.

1 Like

I don’t know how to express my appreciation.:smiling_face_with_three_hearts::smiling_face_with_three_hearts::smiling_face_with_three_hearts:Thank you very much.

1 Like

Clearly a bug to me:

The output handle in windows API doesn’t care what encoding is there. It just expects wchar_t:

But Win32 package doesn’t seem to have it:

If it did, we could probably re-implement putStrLn and friends properly via OsString. Currently, GHC has a rather odd posix layer emulation for windows, making lots of things awkward. I’m not sure what the status of the windows IO manager is… you could try that too via the RTS flag --io-manager=native I believe.

With OsString you’d use a quasiquoter that assumes e.g. UTF-8 on unix and UTF-16 on windows. Whether the console will show something reasonable is a different matter.

2 Likes