Permalink
Browse files

Use predicate on delimiter character.

Now all tests are passing.
  • Loading branch information...
Shimuuar committed Feb 28, 2013
1 parent 0a60b79 commit 24f375ea05c87b474b9f36d6ec0fc290c6ec0a3c
Showing with 17 additions and 17 deletions.
  1. +9 −9 Data/Csv/Parser.hs
  2. +8 −8 tests/UnitTests.hs
View
@@ -56,7 +56,7 @@ import Data.Csv.Util ((<$!>), blankLine)
-- > }
data DecodeOptions = DecodeOptions
{ -- | Field delimiter.
- decDelimiter :: {-# UNPACK #-} !Word8
+ decDelimiter :: Word8 -> Bool
-- | Runs of consecutive delimiters are regarded as a single
-- delimiter. This is useful e.g. when parsing white space
@@ -67,22 +67,22 @@ data DecodeOptions = DecodeOptions
-- end of each record (but not at the begining and end of each
-- field).
, decTrimRecordSpace :: !Bool
- } deriving (Eq, Show)
+ }
-- TODO: Document default values in defaultDecodeOptions
-- | Decoding options for parsing CSV files.
defaultDecodeOptions :: DecodeOptions
defaultDecodeOptions = DecodeOptions
- { decDelimiter = 44 -- comma
+ { decDelimiter = (==44) -- comma
, decMergeDelimiters = False
, decTrimRecordSpace = False
}
-- | Decoding options for parsing space-delimited files.
spaceDecodeOptions :: DecodeOptions
spaceDecodeOptions = DecodeOptions
- { decDelimiter = 32 -- space
+ { decDelimiter = \c -> c == space || c == tab
, decMergeDelimiters = True
, decTrimRecordSpace = True
}
@@ -164,8 +164,8 @@ record !opts
| otherwise = parser
where
delim = decDelimiter opts
- delimiter | decMergeDelimiters opts = A.skipMany1 (A.word8 delim)
- | otherwise = () <$ A.word8 delim
+ delimiter | decMergeDelimiters opts = A.skipMany1 (A.satisfy delim)
+ | otherwise = () <$ A.satisfy delim
parser = do fs <- field opts `sepBy1'` delimiter
return $! V.fromList fs
{-# INLINE record #-}
@@ -199,9 +199,9 @@ escapedField = do
unescapedField :: DecodeOptions -> AL.Parser S.ByteString
unescapedField !opt = A.takeWhile (\ c -> c /= doubleQuote &&
- c /= newline &&
- c /= delim &&
- c /= cr)
+ c /= newline &&
+ c /= cr &&
+ not (delim c))
where
delim = decDelimiter opt
View
@@ -22,6 +22,8 @@ import Test.Framework.Providers.QuickCheck2 as TF
import Data.Csv hiding (record)
import qualified Data.Csv.Streaming as S
+import Data.Csv.Parser (spaceDecodeOptions)
+
------------------------------------------------------------------------
-- Parse tests
@@ -143,10 +145,12 @@ positionalTests =
, ("rfc4180", rfc4180Input, rfc4180Output)
]
decodeWithTests =
- [ ("tab-delim", defDec { decDelimiter = 9 }, "1\t2", [["1", "2"]])
+ [ ("tab-delim", defDec { decDelimiter = (==9) }, "1\t2", [["1", "2"]])
, ("mixed-space", spaceDec, " 88 c \t 0.4 ", [["88", "c", "0.4"]])
, ("multiline-space", spaceDec, " 11 22 \n 11 22", [ ["11","22"]
, ["11","22"]])
+ , ("blankLine-space", spaceDec, "1 2\n\n3 4\n", [ ["1","2"]
+ , ["3","4"]])
]
encodeTest (name, input, expected) =
@@ -159,13 +163,9 @@ positionalTests =
testCase name $ input `decodesStreamingAs` expected
streamingDecodeWithTest (name, opts, input, expected) =
testCase name $ decodesWithStreamingAs opts input expected
- defEnc = defaultEncodeOptions
- defDec = defaultDecodeOptions
- spaceDec = defaultDecodeOptions -- TODO: Use spaceDecodeOptions
- { decDelimiter = 32 -- space
- , decMergeDelimiters = True
- , decTrimRecordSpace = True
- }
+ defEnc = defaultEncodeOptions
+ defDec = defaultDecodeOptions
+ spaceDec = spaceDecodeOptions
nameBasedTests :: [TF.Test]
nameBasedTests =

0 comments on commit 24f375e

Please sign in to comment.