我如何解码编码字符串? [英] How can I decode an encoded-word String?

查看:128
本文介绍了我如何解码编码字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,此问题与相同以前未答复的问题。

这也与这个PHP问题相同,但我正在寻找相当于haskell的。

RFC 2047 定义了标准为编码词编码,并提供了一个例子:

  =?iso-8859-1?q?this = 20is = 20some = 20text?= 

是否有标准的haskell库用于处理将其解码为正确的文字表示法?

使用parsec和RFC Spec编写定制解析器应该不会太困难,但这似乎是其他语言中常见的解决的问题,我无法找到一个Haskell等价物,我宁愿不在这里重新发明轮子。 mime 包可以看看 decodeWord Codec.MIME.Decode

  ghci> import Codec.MIME.Decode 
ghci> decodeWord=?iso-8859-1?q?this = 20is = 20some = 20text?=
Just(this is some text,)

阅读源代码 iso-8859-1 us-支持ascii



还有 decodeWords ,它使用 decodeWord 函数来转换整个字符串:

  ghci> decodeWordsFoo =?iso-8859-1?q?this = 20is = 20some = 20text?= Bar
Foothis是一些textBar


Note that this question is the same as this previously unanswered question.
It is also the same as this PHP question, but I'm looking for the haskell equivalent.

RFC 2047 defines the standard for "encoded-word" encodings and provides an example of:

=?iso-8859-1?q?this=20is=20some=20text?=

Is there a standard haskell library for dealing with decoding this into it's correct Text representation?

This shouldn't be too hard to write a custom parser using parsec and the RFC Spec, but this seems like a common, solved problem in other languages that I cannot find a Haskell equivalent for, and I'd rather not re-invent the wheel here.

解决方案

In the mime package have look at decodeWord in the module Codec.MIME.Decode:

ghci> import Codec.MIME.Decode
ghci> decodeWord "=?iso-8859-1?q?this=20is=20some=20text?="
Just ("this is some text","")

From reading the source code both iso-8859-1 and us-ascii are supported.

There is also the decodeWords which uses the decodeWord function to translate a entire String:

ghci> decodeWords "Foo=?iso-8859-1?q?this=20is=20some=20text?=Bar"
"Foothis is some textBar"

这篇关于我如何解码编码字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆