Skip to content

Commit

Permalink
Conditionally use the new Data.ByteString.Lazy.toStrict and fromStrict
Browse files Browse the repository at this point in the history
  • Loading branch information
basvandijk authored and tibbe committed Feb 24, 2013
1 parent 09c2b93 commit 15ca3fe
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions Data/Csv/Conversion.hs
Expand Up @@ -62,6 +62,21 @@ import GHC.Generics
import qualified Data.IntMap as IM
#endif

------------------------------------------------------------------------
-- bytestring compatibility

toStrict :: L.ByteString -> B.ByteString
fromStrict :: B.ByteString -> L.ByteString
#if MIN_VERSION_bytestring(0,10,0)
toStrict = L.toStrict
fromStrict = L.fromStrict
#else
toStrict = B.concat . L.toChunks
fromStrict = L.fromChunks . (:[])
#endif
{-# INLINE toStrict #-}
{-# INLINE fromStrict #-}

------------------------------------------------------------------------
-- Type conversion

Expand Down Expand Up @@ -553,11 +568,11 @@ instance ToField B.ByteString where
{-# INLINE toField #-}

instance FromField L.ByteString where
parseField s = pure (L.fromChunks [s])
parseField = pure . fromStrict
{-# INLINE parseField #-}

instance ToField L.ByteString where
toField = toField . B.concat . L.toChunks
toField = toStrict
{-# INLINE toField #-}

-- | Assumes UTF-8 encoding.
Expand All @@ -572,12 +587,12 @@ instance ToField T.Text where

-- | Assumes UTF-8 encoding.
instance FromField LT.Text where
parseField = either (fail . show) (pure . LT.fromChunks . (:[])) . T.decodeUtf8'
parseField = either (fail . show) (pure . LT.fromStrict) . T.decodeUtf8'
{-# INLINE parseField #-}

-- | Uses UTF-8 encoding.
instance ToField LT.Text where
toField = toField . B.concat . L.toChunks . LT.encodeUtf8
toField = toField . toStrict . LT.encodeUtf8
{-# INLINE toField #-}

-- | Assumes UTF-8 encoding.
Expand Down

0 comments on commit 15ca3fe

Please sign in to comment.