aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>2007-11-22 18:17:29 +0000
committerfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>2007-11-22 18:17:29 +0000
commit5a0179d0a05fcb0f6fc0946a4c2ad2423a5fd5e3 (patch)
treea9f4336c4eeacb53562ac08b0a9a414abba3c4a4 /src
parentf957c6ef320e799ce47bde1424b9c2d936e05b9a (diff)
downloadpandoc-5a0179d0a05fcb0f6fc0946a4c2ad2423a5fd5e3.tar.gz
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
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Shared.hs19
1 files changed, 11 insertions, 8 deletions
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)