git.iko.soy/typeable/pandoc
on master help sign in
Parsing: Rename takeWhileP -> take1WhileP and clean it up.

(It doesn't match the empty sequence.)

commitdb6e9de091d02a5d36893aa1ef9708afb85da588 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 14 Nov 2019, 07:49 (7 years ago) parenta60eb60
1 file changed+11−9
src/Text/Pandoc/Parsing.hs+11−9
src/Text/Pandoc/Parsing.hs+11−9
@@ -21,7 +21,7 @@
2121A utility library with parsers used in pandoc readers.
2222-}
2323
24−module Text.Pandoc.Parsing ( takeWhileP,
24+module Text.Pandoc.Parsing ( take1WhileP,
2525 takeP,
2626 countChar,
2727 textStr,
@@ -264,19 +264,21 @@ textStr :: Stream s m Char => Text -> ParsecT s u m Text
264264textStr t = string (T.unpack t) $> t
265265
266266-- | Parse characters while a predicate is true.
267−takeWhileP :: Monad m
267+take1WhileP :: Monad m
268268 => (Char -> Bool)
269269 -> ParserT Text st m Text
270−takeWhileP f = do
271− -- faster than 'many (satisfy f)'
270+take1WhileP f = do
271+ -- needed to persuade parsec that this won't match an empty string:
272+ c <- satisfy f
272273 inp <- getInput
273274 pos <- getPosition
274− let (xs, rest) = T.span f inp
275− -- needed to persuade parsec that this won't match an empty string:
276− anyChar
275+ let (t, rest) = T.span f inp
277276 setInput rest
278− setPosition $ updatePosString pos $ T.unpack xs
279− return xs
277+ setPosition $
278+ if f '\t' || f '\n'
279+ then updatePosString pos $ T.unpack t
280+ else incSourceColumn pos (T.length t)
281+ return $ T.singleton c <> t
280282
281283-- Parse n characters of input (or the rest of the input if
282284-- there aren't n characters).