Decoupling byte-level encoding
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 20/100
- Issue 类型
- 功能
- 描述清晰度
- 需要澄清
- 活跃度
- 停滞
- 技术栈
- haskell
调研方向
首先阅读所引用的 GHC.Encoding.UTF8 和 Data.Text.Internal.Encoding.Utf8 实现,然后比较 Encoding.Mixed.Error 和 Codec.Web.JSON.Parse.String 中链接的 JSON 解析器代码。只有在包边界、API、依赖方案和实现计划达成一致后,该提案才算完整;此 issue 将这些决定留待确定。
由索引模型根据 Issue 内容生成。
描述
When writing a JSON parser (GaloisInc/json#17) I needed some way to decode UTF-8 and to my dismay I found all existing solutions do not fit my expectations:
GHC.Encoding.UTF8andGHC.IO.EncodingareIO-based and I don't want that in a parser;Data.Text.Internal.Encoding.Utf8, while pure, appears to both returnRejectas an error and has a rather complex interface;Data.Text.Encoding.*andData.Text.Lazy.Encoding.*are already parsers themselves, too high-level for this task;utf-string'sCodec.Binary.UTF8.Stringconsumes and returns lists, so it isn't parser-compatible.
I decided to handroll the UTF-8 decoding, which allowed me to categorize the errors (see Encoding.Mixed.Error) and resulted in a lot of code on the parser side that has little to do with consuming bytes per se (see Codec.Web.JSON.Parse.String).
However the code I wrote can instead be generalized to:
-- Assume Error is Encoding.Mixed.Error.Error
data UTF8 a = UTF8_1 a
| Part_2 (Word8 -> UTF8_2 a)
| Part_3_1 (Word8 -> Part_3_1 a)
| Part_4_1 (Word8 -> Part_4_1 a)
| Error_1 Error
data UTF8_2 a = UTF8_2 a
| Error_2 Error
data Part_3_1 a = Part_3_2 (Word8 -> UTF8_3 a)
| Error_3_1 Error
data UTF8_3 a = UTF8_3 a
| Error_3_2 Error
data Part_4_1 a = Part_4_2 (Word8 -> Part_4_2 a)
| Error_4_1 Error
data Part_4_2 a = Part_4_3 (Word8 -> UTF8_4 a)
| Error_4_2 Error
data UTF8_4 a = UTF8_4 a
| Error_4_3 Error
newtype Conv1 a = Conv1 (Word8 -> a)
newtype Conv2 a = Conv2 (Word8 -> Word8 -> a)
newtype Conv3 a = Conv3 (Word8 -> Word8 -> Word8 -> a)
newtype Conv4 a = Conv4 (Word8 -> Word8 -> Word8 -> Word8 -> a)
utf8 :: Conv1 a -> Conv2 a -> Conv3 a -> Conv4 a -> Word8 -> UTF8 a
utf8 = -- I'm omitting the implementation, but it's only 50 lines long
Parsing then is simply unwrapping UTF8. This decouples character validation and conversion, the only part of decoding left is ensuring only the maximal subpart of an ill-formed sequence is consumed, which is the responsibility of the parser.
My proposal is creating a separate package with a focus specifically on decoding/encoding UTF-8/UTF-16/UTF-32 on byte-level. Then text can drop some internal modules in favor of a simpler common interface.
This proposal is however naive: I do not know whether GHC can inline these datatypes reliably or, indeed, at all. Based on my cursory reading of the Secrets of the Glasgow Haskell Compiler inliner paper it should, as each of these expressions is trivial.
This doesn't clash with the issue of GHC's many UTF-8 implementations (outlined in GHC.Encoding.UTF8) as all other algorithms are in IO.
Other concerns:
textis a core library, so I assume an extra dependency can't just be added on a whim;- Package named
utfalready exists and is deprecated. I don't know how hard reclaiming deprecated packages is.
- 主要语言
- Haskell
- 星标
- 424
- 派生
- 167
- 平均合并
- 1 天 21 小时
- 30 天内合并 PR
- 6
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
haskell/text 的其他 Issue
-
Relax constraints on decimal / hexadecimal from Data.Text.Read and Data.Text.Lazy.Read可能已有人在做 @Lysxia 于 148 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 64/100
-
难度 3/5 1-2 天 新手友好度 38/100
-
难度 3/5 1-2 天 新手友好度 42/100
-
难度 2/5 1-3 小时 新手友好度 45/100
-
Shrinking is Insanely Slow for `write_read`可能已有人在做 @BebeSparkelSparkel 于 926 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 30/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
haskell/bytestring#730 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 84/100
seal-harness/seal-harness#399 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 65/100
fmidue/codeworld-tasks#124 ·
-
难度 2/5 1-3 小时 新手友好度 62/100
reflex-frp/reflex#563 ·