XML character dereferencer

Just in case you ever need one:

xmlCharDeref :: String -> String
xmlCharDeref [] = []
xmlCharDeref ('&':'#':'x':r) = let
        (digits, remainder) = span (/= ';') r
        c = chr (read ("0x" ++ digits))
    in
        c : xmlCharDeref (tail remainder)
xmlCharDeref ('&':'#':r) = let
        (digits, remainder) = span (/= ';') r
        c = chr (read digits)
    in
        c : xmlCharDeref (tail remainder)
xmlCharDeref (c:r) = c : xmlCharDeref r

In ghci:

*Foo> xmlCharDeref "hello there"
"hello there"
*Foo> xmlCharDeref "hello there"
"hello there"
*Foo> xmlCharDeref "hello2there"
"hello2there"

Chris Done

How about:

newtype XMLString = XMLString String

xmlCharDeref :: String -> XMLString

Now there’s no way you can accidentally encode twice. :-)

Magnus

@Chris: Except of course that the type would be

xmlCharDeref :: XMLString -> String

In any case it’s hardly worth doing for the particular case where I had use for this function.

Leave a comment