haskell / attoparsec

A fast Haskell library for parsing ByteStrings
http://hackage.haskell.org/package/attoparsec
Other
512 stars 93 forks source link

octal parsing #164

Open joeyh opened 4 years ago

joeyh commented 4 years ago

Data.Attoparsec.ByteString.Char8 has decimal and hexadecimal, but no octal. Since octals are used for file permissions, it would be good to have a ready made parser for them. In my case I'm parsing git ls-tree output, which includes file permissions.

It's easy enough to adapt the decimal and hexadecimal code into the following. I mostly submitted this because now, having modified your code, I have to include the 3 clause BSD license in my program in order to properly document your copyright, and would rather send it upstream. ;)

-- | Parse and decode an unsigned octal number.
--
-- This parser does not accept a leading @\"0o\"@ string.
octal :: Integral a => A.Parser a
octal = B8.foldl' step 0 `fmap` I.takeWhile1 isOctDigit
  where
        isOctDigit w = w >= 48 && w <= 55
        step a w = a * 8 + fromIntegral (w - 48)