@@ -3,7 +3,7 @@
33-- Module : Data.Attoparsec.Internal
44-- Copyright : Bryan O'Sullivan 2007-2010
55-- License : BSD3
6−--
6+--
77-- Maintainer : bos@serpentine.com
88-- Stability : experimental
99-- Portability : unknown
@@ -50,8 +50,13 @@ module Data.Attoparsec.Internal
5050 , takeWhile1
5151 , takeTill
5252
53+ -- ** Consume all remaining input
54+ , takeByteString
55+ , takeLazyByteString
56+
5357 -- * State observation and manipulation functions
5458 , endOfInput
59+ , atEnd
5560 , ensure
5661
5762 -- * Utilities
@@ -73,6 +78,7 @@ import qualified Data.ByteString as B8
7378import qualified Data.ByteString.Char8 as B
7479import qualified Data.ByteString.Internal as B
7580import qualified Data.ByteString.Unsafe as B
81+import qualified Data.ByteString.Lazy as L
7682
7783-- | The result of a parse.
7884data Result r = Fail B.ByteString [String] String
@@ -183,7 +189,7 @@ apP d e = do
183189instance Applicative Parser where
184190 pure = returnP
185191 (<*>) = apP
186−
192+
187193 -- These definitions are equal to the defaults, but this
188194 -- way the optimizer doesn't have to work so hard to figure
189195 -- that out.
@@ -406,6 +412,26 @@ takeWhile p = (B.concat . reverse) `fmap` go []
406412 else return (h:acc)
407413 else return acc
408414
415+takeRest :: Parser [B.ByteString]
416+takeRest = go []
417+ where
418+ go acc = do
419+ input <- wantInput
420+ if input
421+ then do
422+ s <- get
423+ put B.empty
424+ go (s:acc)
425+ else return (reverse acc)
426+
427+-- | Consume all remaining input and return it as a single string.
428+takeByteString :: Parser B.ByteString
429+takeByteString = B.concat `fmap` takeRest
430+
431+-- | Consume all remaining input and return it as a single string.
432+takeLazyByteString :: Parser L.ByteString
433+takeLazyByteString = L.fromChunks `fmap` takeRest
434+
409435-- | A stateful scanner. The predicate consumes and transforms a
410436-- state argument, and each transformed state is passed to successive
411437-- invocations of the predicate on each byte of the input until one
@@ -510,7 +536,13 @@ endOfInput = Parser $ \i0 a0 m0 kf ks ->
510536 "endOfInput"
511537 in runParser demandInput i0 a0 m0 kf' ks'
512538 else kf i0 a0 m0 [] "endOfInput"
513−
539+
540+-- | Return an indication of whether the end of input has been
541+-- reached.
542+atEnd :: Parser Bool
543+atEnd = not <$> wantInput
544+{-# INLINE atEnd #-}
545+
514546-- | Match either a single newline character @\'\\n\'@, or a carriage
515547-- return followed by a newline character @\"\\r\\n\"@.
516548endOfLine :: Parser ()