Skip to content

Commit

Permalink
fix: handle case when input for field parser is a single opening doub…
Browse files Browse the repository at this point in the history
…le-quote
  • Loading branch information
gzh committed Feb 27, 2023
1 parent 91d3839 commit 8feec3e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
25 changes: 14 additions & 11 deletions src/Data/Csv/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,20 @@ field !delim = do
escapedField :: AL.Parser S.ByteString
escapedField = do
_ <- dquote
-- The scan state is 'True' if the previous character was a double
-- quote. We need to drop a trailing double quote left by scan.
s <- S.init <$> (A.scan False $ \s c -> if c == doubleQuote
then Just (not s)
else if s then Nothing
else Just False)
if doubleQuote `S.elem` s
then case Z.parse unescape s of
Right r -> return r
Left err -> fail err
else return s
-- The scan state is 'True' if the previous character was a double quote.
s' <- A.scan False $ \s c -> if c == doubleQuote
then Just (not s)
else if s then Nothing
else Just False
-- We need to drop a trailing double quote left by scan.
if S.null s'
then fail "trailing double quote"
else let s = S.init s'
in if doubleQuote `S.elem` s
then case Z.parse unescape s of
Right r -> return r
Left err -> fail err
else return s

unescapedField :: Word8 -> AL.Parser S.ByteString
unescapedField !delim = A.takeWhile (\ c -> c /= doubleQuote &&
Expand Down
2 changes: 1 addition & 1 deletion tests/UnitTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ positionalTests =
testCase "escapedMalformed1" $
"\"x,\"y" `decodeFailsWith` "endOfInput",
testCase "escapedMalformed0" $
"baz,\"" `decodeFailsWith` "endOfInput"
"baz,\"" `decodeFailsWith` "Failed reading: trailing double quote"
]

nameBasedTests :: [TF.Test]
Expand Down

0 comments on commit 8feec3e

Please sign in to comment.