这是我的代码:
import qualified System.IO as IO
writeSurrogate :: IO ()
writeSurrogate = do
IO.writeFile "/home/sibi/surrogate.txt" ['\xD800']
执行上面的代码会出现错误:
text-tests: /home/sibi/surrogate.txt: commitBuffer: invalid argument (invalid character)
原因是GHC本身已将其阻止,因为它们是替代代码点:https://github.com/ghc/ghc/blob/21f0f56164f50844c2150c62f950983b2376f8b6/libraries/base/GHC/IO/Encoding/Failure.hs#L114
我想编写一些需要该数据的测试文件。现在,我正在使用Python实现我想要的-但是我很想知道是否有一种方法(使用Haskell的变通方法)来实现这一目标。
答案 0 :(得分:4)
当然,只需写入所需的字节:
import Data.ByteString as BS
main = BS.writeFile "surrogate.txt" (pack [0xd8, 0x00])