Skip to content
Open
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
2 changes: 1 addition & 1 deletion csv-conduit.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ library
, mtl
, mmorph
, primitive
, resourcet >= 1.1.2.1
, resourcet >= 1.1.2.1 && < 1.2

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also noticed that csv-conduit does not build with resourcet-1.2, so I added this upper bound.


if impl(ghc >= 7.2.1)
cpp-options: -DGENERICS
Expand Down
11 changes: 9 additions & 2 deletions src/Data/CSV/Conduit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import Data.Conduit.Binary (sinkFile, sinkIOHandle,
sourceFile)
import qualified Data.Conduit.List as C
import qualified Data.Map as M
import Data.Maybe
import Data.String
import Data.Text (Text)
import qualified Data.Text as T
Expand Down Expand Up @@ -145,7 +146,10 @@ instance CSV ByteString (Row ByteString) where
let
sep = B.pack [c2w (csvSep s)]
wrapField !f = case csvQuoteChar s of
Just !x -> (x `B8.cons` escape x f) `B8.snoc` x
Just !x ->
if B8.elem x f
then (x `B8.cons` escape x f) `B8.snoc` x
else f
_ -> f
escape c str = B8.intercalate (B8.pack [c,c]) $ B8.split c str
in B.intercalate sep . map wrapField $ r
Expand All @@ -161,7 +165,10 @@ instance CSV Text (Row Text) where
let
sep = T.pack [csvSep s]
wrapField !f = case csvQuoteChar s of
Just !x -> x `T.cons` escape x f `T.snoc` x
Just !x ->
if isJust (T.findIndex (==x) f)
then x `T.cons` escape x f `T.snoc` x
else f
_ -> f
escape c str = T.intercalate (T.pack [c,c]) $ T.split (== c) str
in T.intercalate sep . map wrapField $ r
Expand Down
8 changes: 4 additions & 4 deletions test/test.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
`Col1`,`Col2`,`Col3`,`Sum`
`A`,`2`,`3`,`5`
`B`,`3`,`4`,`7`
`Field using the quote char ``this is the in-quoted value```,`4`,`5`,`9`
Col1,Col2,Col3,Sum
A,2,3,5
B,3,4,7
`Field using the quote char ``this is the in-quoted value```,4,5,9