在 Latin1 编码的 Data.ByteString 和 Data.Text 之间转换

发布于 2024-12-06 12:15:59 字数 377 浏览 5 评论 0原文

由于 latin-1(又名 ISO-8859-1)字符集作为最低 256 个代码点嵌入到 Unicode 字符集中,因此我希望转换很简单,但我没有看到任何 latin-1 Data.Text.Encoding 仅包含常见 UTF 编码的转换函数。

在以 latin-1 表示形式编码的 Data.ByteString 值和 Data.Text 值之间进行转换的推荐和/或有效方法是什么?

Since the latin-1 (aka ISO-8859-1) character set is embedded in the Unicode character set as its lowest 256 code-points, I'd expect the conversion to be trivial, but I didn't see any latin-1 encoding conversion functions in Data.Text.Encoding which contains only conversion functions for the common UTF encodings.

What's the recommended and/or efficient way to convert between Data.ByteString values encoded in latin-1 representation and Data.Text values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离鸿 2024-12-13 12:15:59

答案就在您链接的页面顶部:

要访问更大的编码系列,请使用 text-icu 包:http://hackage.haskell.org/package/text-icu

一个快速的 GHCi 示例:

λ> import Data.Text.ICU.Convert
λ> conv <- open "ISO-8859-1" Nothing
λ> Data.Text.IO.putStrLn $ toUnicode conv $ Data.ByteString.pack [198, 216, 197]
ÆØÅ
λ> Data.ByteString.unpack $ fromUnicode conv $ Data.Text.pack "ÆØÅ"
[198,216,197]

但是,正如您所指出的,在 latin-1 的特定情况下,代码点与 Unicode 一致,因此你可以使用从 Data.ByteString.Char8 中进行 pack/unpack 以执行从 latin-1 到 String 的简单映射,然后您可以使用 Data.Text 中相应的 pack/unpack 将其转换为 Text

The answer is right at the top of the page you linked:

To gain access to a much larger family of encodings, use the text-icu package: http://hackage.haskell.org/package/text-icu

A quick GHCi example:

λ> import Data.Text.ICU.Convert
λ> conv <- open "ISO-8859-1" Nothing
λ> Data.Text.IO.putStrLn $ toUnicode conv $ Data.ByteString.pack [198, 216, 197]
ÆØÅ
λ> Data.ByteString.unpack $ fromUnicode conv $ Data.Text.pack "ÆØÅ"
[198,216,197]

However, as you pointed out, in the specific case of latin-1, the code points coincide with Unicode, so you can use pack/unpack from Data.ByteString.Char8 to perform the trivial mapping from latin-1 from/to String, which you can then convert to Text using the corresponding pack/unpack from Data.Text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文