好的,所以我需要解析 n 位十六进制,我遇到了无法停止标准 attoparsec hex parser 的问题hexadecimal
。
我的第一个想法是这样的:
nHex n = take n *> hexadecimal
但这不起作用,因为它会删除 4 位数字,然后解析字符串 xD 的其余部分
下一个可行的想法是:
hex :: (Num a, Eq a) => Int -> Parser a
hex n = fst . head . readHex <$> count n (satisfy isHexDigit)
但该代码的问题在于 attoparsec 库它警告不要返回字符列表以考虑速度问题,这个十六进制解析器是我整个程序的基础
尝试提高速度的下一个想法是:
parseFragments :: (Bits a, Integral a) => Int -> Parser a
parseFragments n = do
fourChars <- B.take n
let hexDigits = parseOnly hexadecimal fourChars
case hexDigits of
Left err -> fail err
Right x -> return x
但这感觉就像使用 parseOnly 的可怕黑客攻击。有没有更惯用的快速方法?