git.iko.soy/typeable/pandoc
on master help sign in
Improved efficiency of romanNumeral parser (in Text.Pandoc.Shared)

for a big speed boost in (non-strict) markdown parsing. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1089 788f1e2b-df1e-0410-8736-df70ead52e1b

commit5a0179d0a05fcb0f6fc0946a4c2ad2423a5fd5e3 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateThu 22 Nov 2007, 18:17 (19 years ago) parentf957c6e
1 file changed+11−8
src/Text/Pandoc/Shared.hs+11−8
src/Text/Pandoc/Shared.hs+11−8
@@ -379,18 +379,21 @@ charsInBalanced' open close = try $ do
379379 char close
380380 return $ concat raw
381381
382+-- Auxiliary functions for romanNumeral:
383+
384+lowercaseRomanDigits = ['i','v','x','l','c','d','m']
385+uppercaseRomanDigits = map toUpper lowercaseRomanDigits
386+
382387-- | Parses a roman numeral (uppercase or lowercase), returns number.
383388romanNumeral :: Bool -- ^ Uppercase if true
384389 -> GenParser Char st Int
385390romanNumeral upperCase = do
386− let charAnyCase c = char (if upperCase then toUpper c else c)
387− let one = charAnyCase 'i'
388− let five = charAnyCase 'v'
389− let ten = charAnyCase 'x'
390− let fifty = charAnyCase 'l'
391− let hundred = charAnyCase 'c'
392− let fivehundred = charAnyCase 'd'
393− let thousand = charAnyCase 'm'
391+ let romanDigits = if upperCase
392+ then uppercaseRomanDigits
393+ else lowercaseRomanDigits
394+ lookAhead $ oneOf romanDigits
395+ let [one, five, ten, fifty, hundred, fivehundred, thousand] =
396+ map char romanDigits
394397 thousands <- many thousand >>= (return . (1000 *) . length)
395398 ninehundreds <- option 0 $ try $ hundred >> thousand >> return 900
396399 fivehundreds <- many fivehundred >>= (return . (500 *) . length)