git.iko.soy/typeable/pandoc
on master help sign in
UTF8: Strip off BOM if present.

Closes #743.

commit3465ba2f61872566e221235fba58001bbc2a3b67 authorJohn MacFarlane <fiddlosopher@gmail.com> dateFri 8 Feb 2013, 09:45 (14 years ago) parentec5dc43
1 file changed+9−2
src/Text/Pandoc/UTF8.hs+9−2
src/Text/Pandoc/UTF8.hs+9−2
@@ -86,10 +86,17 @@ hGetContents = fmap toStringLazy . BL.hGetContents
8686-- >> hSetNewlineMode h universalNewlineMode
8787-- >> IO.hGetContents h
8888
89+-- | Drop BOM (byte order marker) if present at beginning of string.
90+-- Note that Data.Text converts the BOM to code point FEFF, zero-width
91+-- no-break space, so if the string begins with this we strip it off.
92+dropBOM :: String -> String
93+dropBOM ('\xFEFF':xs) = xs
94+dropBOM xs = xs
95+
8996-- | Convert UTF8-encoded ByteString to String, also
9097-- removing '\r' characters.
9198toString :: B.ByteString -> String
92−toString = filter (/='\r') . T.unpack . T.decodeUtf8
99+toString = filter (/='\r') . dropBOM . T.unpack . T.decodeUtf8
93100
94101fromString :: String -> B.ByteString
95102fromString = T.encodeUtf8 . T.pack
@@ -97,7 +104,7 @@ fromString = T.encodeUtf8 . T.pack
97104-- | Convert UTF8-encoded ByteString to String, also
98105-- removing '\r' characters.
99106toStringLazy :: BL.ByteString -> String
100−toStringLazy = filter (/='\r') . TL.unpack . TL.decodeUtf8
107+toStringLazy = filter (/='\r') . dropBOM . TL.unpack . TL.decodeUtf8
101108
102109fromStringLazy :: String -> BL.ByteString
103110fromStringLazy = TL.encodeUtf8 . TL.pack