Changed Entities.hs to use Data.Map rather than
an association list, for a slight performance boost. git-svn-id: https://pandoc.googlecode.com/svn/trunk@532 788f1e2b-df1e-0410-8736-df70ead52e1b
This commit is contained in:
parent
5a9b7c229d
commit
1266b189a1
1 changed files with 7 additions and 9 deletions
|
@ -39,15 +39,15 @@ module Text.Pandoc.Entities (
|
|||
import Data.Char ( chr, ord )
|
||||
import Text.ParserCombinators.Parsec
|
||||
import Data.Maybe ( fromMaybe )
|
||||
import qualified Data.Map as Map
|
||||
|
||||
-- | Returns a string containing an entity reference for the character.
|
||||
charToEntity :: Char -> String
|
||||
charToEntity char =
|
||||
let matches = filter (\(entity, character) -> (character == char))
|
||||
entityTable in
|
||||
if (length matches) == 0
|
||||
let matches = Map.filter (== char) entityTable in
|
||||
if Map.null matches
|
||||
then charToNumericalEntity char
|
||||
else fst (head matches)
|
||||
else head $ Map.keys matches
|
||||
|
||||
-- | Returns a string containing a numerical entity reference for the char.
|
||||
charToNumericalEntity :: Char -> String
|
||||
|
@ -64,9 +64,7 @@ namedEntity = try $ do
|
|||
body <- many1 alphaNum
|
||||
end <- char ';'
|
||||
let entity = "&" ++ body ++ ";"
|
||||
return $ case (lookup entity entityTable) of
|
||||
Just ch -> ch
|
||||
Nothing -> '?'
|
||||
return $ Map.findWithDefault '?' entity entityTable
|
||||
|
||||
-- | Parse SGML hexadecimal entity.
|
||||
hexEntity :: GenParser Char st Char
|
||||
|
@ -114,8 +112,8 @@ decodeEntities str =
|
|||
Left err -> error $ "\nError: " ++ show err
|
||||
Right result -> result
|
||||
|
||||
entityTable :: [(String, Char)]
|
||||
entityTable = [
|
||||
entityTable :: Map.Map String Char
|
||||
entityTable = Map.fromList [
|
||||
(""", chr 34),
|
||||
("&", chr 38),
|
||||
("<", chr 60),
|
||||
|
|
Loading…
Reference in a new issue