Bryan O'Sullivan avatar Bryan O'Sullivan committed 5ea21c9

Add takeCount function.

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 =
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.