2

好的,所以我需要解析 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 的可怕黑客攻击。有没有更惯用的快速方法?

4

1 回答 1

2

Data.Attoparsec.ByteString.Char8.hexadecimal实现为:

hexadecimal :: (Integral a, Bits a) => Parser a
hexadecimal = B8.foldl' step 0 `fmap` I.takeWhile1 isHexDigit
  where
    isHexDigit w = (w >= 48 && w <= 57) ||
                   (w >= 97 && w <= 102) ||
                   (w >= 65 && w <= 70)
    step a w | w >= 48 && w <= 57  = (a `shiftL` 4) .|. fromIntegral (w - 48)
             | w >= 97             = (a `shiftL` 4) .|. fromIntegral (w - 87)
             | otherwise           = (a `shiftL` 4) .|. fromIntegral (w - 55)

您可以使用几乎相同的方法,只是您需要检查 的结果take,因为您的某些字符可能不是有效的十六进制字符。您可以使用(Maybe a -> Word8 -> Maybe a)将两者放在同一个函数中,但为简单起见,我使用了上面的函数:

fixedHexadecimal :: (Integral a, Bits a) => Int -> Parser a
fixedHexadecimal n = do
    bytes <- A.take n
    if B8.all isHexDigit bytes 
      then B8.foldl' step 0 bytes
      else fail "fixedHexadecimal"

  where isHexDigit = -- see above
        step       = -- see above
于 2015-04-23T07:01:24.687 回答