Commits

Bryan O'Sullivan committed 5ea21c9

Add takeCount function.

  • Participants
  • Parent commits 5349211

Comments (0)

Files changed (2)

src/Data/ParserCombinators/Attoparsec/Char8.hs

     , takeWhile1
     , takeTill
     , takeAll
+    , takeCount
     , skipWhile
     , skipSpace
     , notEmpty
 import Data.ParserCombinators.Attoparsec.Internal
     (Parser, ParseError, (<?>), parse, parseAt, parseTest, try, manyTill, eof,
      skipMany, skipMany1, count, lookAhead, peek, sepBy, sepBy1, string,
-     eitherP, getInput, getConsumed, takeAll, notEmpty, match, endOfLine,
-     setInput, many, many1)
+     eitherP, getInput, getConsumed, takeAll, takeCount, notEmpty, match,
+     endOfLine, setInput, many, many1)
 import Data.ByteString.Lex.Lazy.Double (readDouble)
 import Prelude hiding (takeWhile)
 

src/Data/ParserCombinators/Attoparsec/Internal.hs

     , takeWhile1
     , takeTill
     , takeAll
+    , takeCount
     , skipWhile
     , notEmpty
     , match
           let bs = sb +: lb
           in Right (bs, mkState LB.empty (n + LB.length bs))
 
+takeCount :: Int64 -> Parser LB.ByteString
+takeCount k =
+  Parser $ \(S sb lb n) ->
+      let bs = sb +: lb
+          (h,t) = LB.splitAt k bs
+      in if LB.length h == k
+         then Right (h, mkState t (n + k))
+         else Left (bs, [show k ++ " bytes"])
+
 -- | Consume characters while the predicate is true.
 takeWhile :: (Word8 -> Bool) -> Parser LB.ByteString
 takeWhile p =