Bryan O'Sullivan avatar Bryan O'Sullivan committed fa4ee94

Simplify ensure, and hide it

Comments (0)

Files changed (3)

Data/Attoparsec.hs

     -- * State observation and manipulation functions
     , I.endOfInput
     , I.atEnd
-    , I.ensure
     ) where
 
 import Data.Attoparsec.Combinator

Data/Attoparsec/Char8.hs

     -- * State observation and manipulation functions
     , I.endOfInput
     , I.atEnd
-    , I.ensure
     ) where
 
 import Control.Applicative ((*>), (<$>), (<|>))

Data/Attoparsec/Internal.hs

     where msg = "Failed reading: " ++ err
 {-# INLINE failDesc #-}
 
--- | Succeed only if at least @n@ bytes of input are available.
-ensure :: Int -> Parser ()
+-- | If at least @n@ bytes of input are available, return the current
+-- input, otherwise fail.
+ensure :: Int -> Parser B.ByteString
 ensure n = Parser $ \i0 a0 m0 kf ks ->
     if B.length (unI i0) >= n
-    then ks i0 a0 m0 ()
+    then ks i0 a0 m0 (unI i0)
     else runParser (demandInput >> ensure n) i0 a0 m0 kf ks
 
 -- | Ask for input.  If we receive any, pass it to a success
 -- continuation, otherwise to a failure continuation.
---prompt :: S -> (S -> Result r) -> (S -> Result r) -> Result r
 prompt :: Input -> Added -> More
        -> (Input -> Added -> More -> Result r)
        -> (Input -> Added -> More -> Result r)
 -- >    where isDigit w = w >= 48 && w <= 57
 satisfy :: (Word8 -> Bool) -> Parser Word8
 satisfy p = do
-  ensure 1
-  s <- get
+  s <- ensure 1
   let w = B.unsafeHead s
   if p w
     then put (B.unsafeTail s) >> return w
 -- >    where isDigit w = w >= 48 && w <= 57
 skip :: (Word8 -> Bool) -> Parser ()
 skip p = do
-  ensure 1
-  s <- get
+  s <- ensure 1
   if p (B.unsafeHead s)
     then put (B.unsafeTail s)
     else fail "skip"
 -- parser returns the transformed byte that was parsed.
 satisfyWith :: (Word8 -> a) -> (a -> Bool) -> Parser a
 satisfyWith f p = do
-  ensure 1
-  s <- get
+  s <- ensure 1
   let c = f (B.unsafeHead s)
   if p c
     then put (B.unsafeTail s) >> return c
 -- returns 'True'.
 takeWith :: Int -> (B.ByteString -> Bool) -> Parser B.ByteString
 takeWith n p = do
-  ensure n
-  s <- get
+  s <- ensure n
   let (h,t) = B.splitAt n s
   if p h
     then put t >> return h
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.