【问题标题】:Write surrogate pairs to file using Haskell使用 Haskell 将代理对写入文件
【发布时间】:2018-08-31 12:38:44
【问题描述】:

这是我的代码:

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 的解决方法)来实现这一目标。

【问题讨论】:

  • 这与编码有关:在 UTF-16 中,这根本不是一个 有效 字符:它确实 not 映射到一个字符.
  • 您可以使用“二进制写入器”来写入指定的字节。但作为文本没有意义。
  • 如果要写入无效文本的数据,请不要将其写入文本。改为二进制。

标签: haskell unicode surrogate-pairs


【解决方案1】:

当然,只要写你想要的字节:

import Data.ByteString as BS
main = BS.writeFile "surrogate.txt" (pack [0xd8, 0x00])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多