@@ -61,13 +61,12 @@ module Data.ParserCombinators.Attoparsec.Internal
6161 , match
6262 ) where
6363
64−import Control.Applicative
65− (Alternative(..), Applicative(..), (<$>), (<*), (*>))
64+import Control.Applicative (Alternative(..), Applicative(..), (<$>))
6665import Control.Monad (MonadPlus(..), ap, liftM2)
6766import Control.Monad.Fix (MonadFix(..))
6867import qualified Data.ByteString as SB
6968import qualified Data.ByteString.Lazy as LB
70−import qualified Data.ByteString.Lazy.Internal as LB
69+import qualified Data.ByteString.Lazy.Internal as I
7170import Data.Int (Int64)
7271import Data.Word (Word8)
7372import Prelude hiding (takeWhile)
@@ -133,14 +132,14 @@ instance Alternative Parser where
133132
134133mkState :: LB.ByteString -> Int64 -> S
135134mkState s = case s of
136− LB.Empty -> S SB.empty s
137− LB.Chunk x xs -> S x xs
135+ I.Empty -> S SB.empty s
136+ I.Chunk x xs -> S x xs
138137
139138-- | Turn our chunked representation back into a normal lazy
140139-- ByteString.
141140(+:) :: SB.ByteString -> LB.ByteString -> LB.ByteString
142141sb +: lb | SB.null sb = lb
143− | otherwise = LB.Chunk sb lb
142+ | otherwise = I.Chunk sb lb
144143{-# INLINE (+:) #-}
145144
146145infix 0 <?>
@@ -157,8 +156,8 @@ p <?> msg =
157156nextChunk :: Parser ()
158157nextChunk = Parser $ \(S _ lb n) ->
159158 case lb of
160− LB.Chunk sb' lb' -> Right ((), S sb' lb' n)
161− LB.Empty -> Left (lb, [])
159+ I.Chunk sb' lb' -> Right ((), S sb' lb' n)
160+ I.Empty -> Left (lb, [])
162161
163162-- | Get remaining input.
164163getInput :: Parser LB.ByteString
@@ -245,37 +244,28 @@ takeAll = Parser $ \(S sb lb n) ->
245244 let bs = sb +: lb
246245 in Right (bs, mkState LB.empty (n + LB.length bs))
247246
248−oneChunk :: SB.ByteString -> LB.ByteString
249−oneChunk s = LB.Chunk s LB.Empty
250−
251−length64 :: SB.ByteString -> Int64
252−length64 = fromIntegral . SB.length
253−
254247-- | Consume characters while the predicate is true.
255248takeWhile :: (Word8 -> Bool) -> Parser LB.ByteString
256−takeWhile p = Parser $ \s@(S sb lb n) ->
257− let (h, t) = SB.span p sb
258− in if SB.null t
259− then unParser ((h +:) <$> (nextChunk *> takeWhile p)) s
260− else Right (oneChunk h, S t lb (n + length64 h))
249+takeWhile p =
250+ Parser $ \(S sb lb n) ->
251+ case LB.span p (sb +: lb) of
252+ (h,t) -> Right (h, mkState t (n + LB.length h))
261253{-# INLINE takeWhile #-}
262254
263255takeTill :: (Word8 -> Bool) -> Parser LB.ByteString
264−takeTill p = takeWhile (not . p) <* satisfy p
256+takeTill p =
257+ Parser $ \(S sb lb n) ->
258+ case LB.span (not . p) (sb +: lb) of
259+ (h,t) | LB.null t -> Left (h, [])
260+ | otherwise -> Right (h, mkState t (n + LB.length h))
265261{-# INLINE takeTill #-}
266262
267263takeWhile1 :: (Word8 -> Bool) -> Parser LB.ByteString
268−takeWhile1 p = Parser $ \s@(S sb lb n) ->
269− let (h, t) = SB.span p sb
270− in if SB.null t
271− then case unParser (nextChunk *> takeWhile p) s of
272− Left err -> Left err
273− Right (xs, s') ->
274− let bs = h +: xs
275− in if LB.null bs
276− then Left (sb +: lb, [])
277− else Right (bs, s')
278− else Right (oneChunk h, S t lb (n + length64 h))
264+takeWhile1 p =
265+ Parser $ \(S sb lb n) ->
266+ case LB.span p (sb +: lb) of
267+ (h,t) | LB.null h -> Left (t, [])
268+ | otherwise -> Right (h, mkState t (n + LB.length h))
279269{-# INLINE takeWhile1 #-}
280270
281271-- | Skip over characters while the predicate is true.
@@ -338,7 +328,9 @@ parseAt p bs n =
338328 Left (bs', msg) -> (bs', Left $ showError msg)
339329 Right (a, S sb lb n') -> (sb +: lb, Right (a, n'))
340330 where
331+ showError [""] = "Parser error\n"
341332 showError [msg] = "Parser error, expected:\n" ++ msg ++ "\n"
333+ showError [] = "Parser error\n"
342334 showError msgs = "Parser error, expected one of:\n" ++ unlines msgs
343335
344336-- | Run a parser.