From 5a0179d0a05fcb0f6fc0946a4c2ad2423a5fd5e3 Mon Sep 17 00:00:00 2001 From: fiddlosopher Date: Thu, 22 Nov 2007 18:17:29 +0000 Subject: 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 --- src/Text/Pandoc/Shared.hs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs index e0e93c189..5fb1306af 100644 --- a/src/Text/Pandoc/Shared.hs +++ b/src/Text/Pandoc/Shared.hs @@ -379,18 +379,21 @@ charsInBalanced' open close = try $ do char close return $ concat raw +-- Auxiliary functions for romanNumeral: + +lowercaseRomanDigits = ['i','v','x','l','c','d','m'] +uppercaseRomanDigits = map toUpper lowercaseRomanDigits + -- | Parses a roman numeral (uppercase or lowercase), returns number. romanNumeral :: Bool -- ^ Uppercase if true -> GenParser Char st Int romanNumeral upperCase = do - let charAnyCase c = char (if upperCase then toUpper c else c) - let one = charAnyCase 'i' - let five = charAnyCase 'v' - let ten = charAnyCase 'x' - let fifty = charAnyCase 'l' - let hundred = charAnyCase 'c' - let fivehundred = charAnyCase 'd' - let thousand = charAnyCase 'm' + let romanDigits = if upperCase + then uppercaseRomanDigits + else lowercaseRomanDigits + lookAhead $ oneOf romanDigits + let [one, five, ten, fifty, hundred, fivehundred, thousand] = + map char romanDigits thousands <- many thousand >>= (return . (1000 *) . length) ninehundreds <- option 0 $ try $ hundred >> thousand >> return 900 fivehundreds <- many fivehundred >>= (return . (500 *) . length) -- cgit v1.2.3