git.iko.soy/typeable/pandoc
on master help sign in
Parsing: Rename takeWhileP -> take1WhileP and clean it up.
db6e9deJohn MacFarlane
src/Text/Pandoc/Parsing.hs+11−9
@@ -21,7 +21,7 @@
21A utility library with parsers used in pandoc readers.
21A utility library with parsers used in pandoc readers.
22-}
22-}
23
23
24−module Text.Pandoc.Parsing ( takeWhileP,
24+module Text.Pandoc.Parsing ( take1WhileP,
25 takeP,
25 takeP,
26 countChar,
26 countChar,
27 textStr,
27 textStr,
@@ -264,19 +264,21 @@ textStr :: Stream s m Char => Text -> ParsecT s u m Text
264textStr t = string (T.unpack t) $> t
264textStr t = string (T.unpack t) $> t
265
265
266-- | Parse characters while a predicate is true.
266-- | Parse characters while a predicate is true.
267−takeWhileP :: Monad m
267+take1WhileP :: Monad m
268 => (Char -> Bool)
268 => (Char -> Bool)
269 -> ParserT Text st m Text
269 -> ParserT Text st m Text
270−takeWhileP f = do
270+take1WhileP f = do
271+ -- needed to persuade parsec that this won't match an empty string:
271− -- faster than 'many (satisfy f)'
272+ c <- satisfy f
272 inp <- getInput
273 inp <- getInput
273 pos <- getPosition
274 pos <- getPosition
274− let (xs, rest) = T.span f inp
275+ let (t, rest) = T.span f inp
275− -- needed to persuade parsec that this won't match an empty string:
276− anyChar
277 setInput rest
276 setInput rest
277+ setPosition $
278+ if f '\t' || f '\n'
278− setPosition $ updatePosString pos $ T.unpack xs
279+ then updatePosString pos $ T.unpack t
280+ else incSourceColumn pos (T.length t)
279− return xs
281+ return $ T.singleton c <> t
280
282
281-- Parse n characters of input (or the rest of the input if
283-- Parse n characters of input (or the rest of the input if
282-- there aren't n characters).
284-- there aren't n characters).