Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support utf-8 in unstructured headers #83

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Data/IMF.hs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ import Data.IMF.Syntax
( CI, ci, mk, original
, (<<>>), foldMany, foldMany1Sep
, char, fromChar, isAtext, isQtext, isVchar, isWsp
, optionalCFWS, word, wsp, vchar, optionalFWS, crlf
, optionalCFWS, word, wsp, vcharUtf8, optionalFWS, crlf
, domainLiteral, dotAtom, dotAtomText, localPart, quotedString
)
import {-# SOURCE #-} Data.IMF.Text (readMailbox)
Expand Down Expand Up @@ -899,9 +899,10 @@ field = (,)
<* char8 ':' <* many wsp
<*> unstructured <* crlf

-- | UTF-8 unstructured text
unstructured :: Parser B.ByteString
unstructured =
foldMany (optionalFWS <<>> (B.singleton <$> vchar))
foldMany (optionalFWS <<>> fmap B.pack vcharUtf8)
<<>> A.takeWhile isWsp


Expand Down
26 changes: 26 additions & 0 deletions src/Data/IMF/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ module Data.IMF.Syntax
, CharParsing(..)
, SM

, vcharUtf8

-- ** Helpers for building parsers
, isAtext
, isQtext
Expand Down Expand Up @@ -146,6 +148,30 @@ isVchar c =
vchar :: CharParsing f s a => (f s) a
vchar = satisfy isVchar

vcharUtf8 :: AT.Parser B.ByteString [Word8]
vcharUtf8 =
pure <$> satisfy isVchar
<|> utf8NonAscii

-- | Parse a multi-byte UTF-8 character, returning the list of
-- bytes.
utf8NonAscii :: A.Parser [Word8]
utf8NonAscii =
list2 <$> fromTo 0xC2 0xDF <*> utf8_tail
<|> list3 <$> A.word8 0xE0 <*> fromTo 0xA0 0xBF <*> utf8_tail
<|> list3 <$> fromTo 0xE1 0xEC <*> utf8_tail <*> utf8_tail
<|> list3 <$> A.word8 0xED <*> fromTo 0x80 0x9F <*> utf8_tail
<|> list3 <$> fromTo 0xEE 0xEF <*> utf8_tail <*> utf8_tail
<|> list4 <$> A.word8 0xF0 <*> fromTo 0x90 0xBF <*> utf8_tail <*> utf8_tail
<|> list4 <$> fromTo 0xF1 0xF3 <*> utf8_tail <*> utf8_tail <*> utf8_tail
<|> list4 <$> fromTo 0xF1 0xF3 <*> utf8_tail <*> utf8_tail <*> utf8_tail
where
fromTo lo hi = A.satisfy (\c -> c >= lo && c <= hi)
utf8_tail = fromTo 0x80 0xBF
list2 c1 c2 = [c1, c2]
list3 c1 c2 c3 = [c1, c2, c3]
list4 c1 c2 c3 c4 = [c1, c2, c3, c4]

dquote :: CharParsing f s a => (f s) a
dquote = char '"'

Expand Down