git.iko.soy/typeable/pandoc
on master help sign in
Better algorithm for oneOfStrings.

This goes character by character, not backtracking.

commitfbd3d2b4503c486f7d835697252ce8b1e39eea3e authorJohn MacFarlane <fiddlosopher@gmail.com> dateTue 24 Jul 2012, 22:42 (14 years ago) parent02ef26a
1 file changed+9−2
src/Text/Pandoc/Parsing.hs+9−2
src/Text/Pandoc/Parsing.hs+9−2
@@ -173,9 +173,16 @@ notFollowedBy' p = try $ join $ do a <- try p
173173 return (return ())
174174-- (This version due to Andrew Pimlott on the Haskell mailing list.)
175175
176−-- | Parses one of a list of strings (tried in order).
176+-- | Parses one of a list of strings (tried in order).
177177oneOfStrings :: [String] -> Parsec [Char] st String
178−oneOfStrings listOfStrings = choice $ map (try . string) listOfStrings
178+oneOfStrings [] = fail "no strings"
179+oneOfStrings strs = do
180+ c <- anyChar
181+ let strs' = [xs | (x:xs) <- strs, x == c]
182+ case strs' of
183+ [] -> fail "not found"
184+ z | "" `elem` z -> return [c]
185+ | otherwise -> (c:) `fmap` oneOfStrings strs'
179186
180187-- | Parses a space or tab.
181188spaceChar :: Parsec [Char] st Char