Using Haskell to output a UTF-8-encoded ByteString

后端 未结 3 1525
时光取名叫无心
时光取名叫无心 2020-12-15 17:35

I\'m going out of my mind trying to simply output UTF-8-encoded data to the console.

I\'ve managed to accomplish this using String, but now I\'d like to

相关标签:
3条回答
  • 2020-12-15 17:55

    This is a known ghc bug, marked "wontfix".

    0 讨论(0)
  • 2020-12-15 17:58

    bytestrings are strings of bytes. When they're output, they will be truncated to 8 bits, as it describes in the documentation for Data.ByteString.Char8. You'll need to explicitly convert them to utf8 - via the utf8-string package on Hackage, which contains support for bytestrings.


    However, as of 2011, you should use the text package, for fast, packed unicode output. GHC truncating Unicode character output

    Your example becomes a lot simpler:

    {-# LANGUAGE OverloadedStrings #-}
    
    import qualified Data.Text    as T
    import qualified Data.Text.IO as T
    
    main = T.putStrLn "čušpajž日本語"
    

    Like so:

    $ runhaskell A.hs
    čušpajž日本語
    
    0 讨论(0)
  • 2020-12-15 18:10

    utf8-string supports bytestrings.

    import Prelude hiding (putStr)
    import Data.ByteString.Char8 (putStr)
    import Data.ByteString.UTF8 (fromString)
    
    main :: IO ()
    main = putStr $ fromString "čušpajž日本語"
    
    0 讨论(0)
提交回复
热议问题