将 ByteString 部分解码为文本
我需要将各种编码的字节字符串解码为文本,但字节字符串可能是不完整的片段。理想情况下,我需要一个具有如下签名的函数:
decodeFragment :: Encoding -> ByteString -> (Text, ByteString)
它返回成功解码的文本以及未形成完整 unicode 字符的任何剩余字节(这样当我获取下一个片段时我可以重新使用这些字节) 。
这种函数是否已经存在于某些 Haskell 库中,还是我需要自己推出?现在,我什至可以开始使用不支持 UTF-8 以外的编码的东西。
I need to decode ByteStrings from various encodings into Text, but the ByteStrings might be incomplete fragments. Ideally, I would need a function with signature of something like:
decodeFragment :: Encoding -> ByteString -> (Text, ByteString)
which returnes the succesfully decoded Text as well as any remaining bytes that didn't form a complete unicode character (so I can re-use those bytes when I get the next fragment).
Does this sort of function already exist in some Haskell library, or do I need to roll my own? For now, I could even get started with something that doesn't support encodings beyond UTF-8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
棘手。通常, encoding 是我对文本编码和解码的首选建议,但我不相信它提供了您所需要的确切东西。它很接近,因为它提供了
您可以迭代以获得
m String
的功能。捕获由decodeChar
引发的错误将告诉您是否已到达片段的末尾。粗略地看一下 Hackage 上的其他一些编码包表明,它们要么需要相同的方法,要么需要一个补丁来公开与它们内部使用的上述函数类似的函数。Tricky. Usually, encoding is my go-to suggestion for encoding and decoding text, but I don't believe it offers the exact thing you're asking for. It comes close, in that it offers
which you can iterate to get a
m String
. Catching the errors thrown bydecodeChar
will tell you if you've come to the end of a fragment. A cursory look at some of the other encoding packages on Hackage suggests that they will either require the same approach or will require a patch to expose the function analogous to the above one that they use internally.