git.iko.soy/typeable/attoparsec
on master help sign in
Add takeCount function.
commitebe88ee4596adcfed20b79f7b9c4886b911974aa authorBryan O'Sullivan <bos@serpentine.com> dateTue 30 Dec 2008, 23:07 (18 years ago) parent73a1e0e
2 files changed+13−2
src/Data/ParserCombinators/Attoparsec/Char8.hs+3−2 src/Data/ParserCombinators/Attoparsec/Internal.hs+10
src/Data/ParserCombinators/Attoparsec/Char8.hs+3−2
@@ -66,6 +66,7 @@ module Data.ParserCombinators.Attoparsec.Char8
6666 , takeWhile1
6767 , takeTill
6868 , takeAll
69+ , takeCount
6970 , skipWhile
7071 , skipSpace
7172 , notEmpty
@@ -86,8 +87,8 @@ import qualified Data.ParserCombinators.Attoparsec.Internal as I
8687import Data.ParserCombinators.Attoparsec.Internal
8788 (Parser, ParseError, (<?>), parse, parseAt, parseTest, try, manyTill, eof,
8889 skipMany, skipMany1, count, lookAhead, peek, sepBy, sepBy1, string,
89− eitherP, getInput, getConsumed, takeAll, notEmpty, match, endOfLine,
90− setInput, many, many1)
90+ eitherP, getInput, getConsumed, takeAll, takeCount, notEmpty, match,
91+ endOfLine, setInput, many, many1)
9192import Data.ByteString.Lex.Lazy.Double (readDouble)
9293import Prelude hiding (takeWhile)
9394
src/Data/ParserCombinators/Attoparsec/Internal.hs+10
@@ -59,6 +59,7 @@ module Data.ParserCombinators.Attoparsec.Internal
5959 , takeWhile1
6060 , takeTill
6161 , takeAll
62+ , takeCount
6263 , skipWhile
6364 , notEmpty
6465 , match
@@ -271,6 +272,15 @@ takeAll = Parser $ \(S sb lb n) ->
271272 let bs = sb +: lb
272273 in Right (bs, mkState LB.empty (n + LB.length bs))
273274
275+takeCount :: Int64 -> Parser LB.ByteString
276+takeCount k =
277+ Parser $ \(S sb lb n) ->
278+ let bs = sb +: lb
279+ (h,t) = LB.splitAt k bs
280+ in if LB.length h == k
281+ then Right (h, mkState t (n + k))
282+ else Left (bs, [show k ++ " bytes"])
283+
274284-- | Consume characters while the predicate is true.
275285takeWhile :: (Word8 -> Bool) -> Parser LB.ByteString
276286takeWhile p =