Commit 52b8199f authored by Lennart Kolmodin's avatar Lennart Kolmodin
Browse files

Merge remote-tracking branch 'qnikst/bytestring-short'

parents cf1d5225 9fa6234c
......@@ -7,7 +7,7 @@
-- Module : Data.Binary.Builder
-- Copyright : Lennart Kolmodin, Ross Paterson
-- License : BSD3-style (see LICENSE)
--
--
-- Maintainer : Lennart Kolmodin <kolmodin@gmail.com>
-- Stability : experimental
-- Portability : portable to Hugs and GHC
......@@ -28,6 +28,9 @@ module Data.Binary.Builder (
, append
, fromByteString -- :: S.ByteString -> Builder
, fromLazyByteString -- :: L.ByteString -> Builder
#if MIN_VERSION_bytestring(0,10,4)
, fromShortByteString -- :: T.ByteString -> Builder
#endif
-- * Flushing the buffer state
, flush
......
......@@ -33,7 +33,9 @@ module Data.Binary.Builder.Base (
, append
, fromByteString -- :: S.ByteString -> Builder
, fromLazyByteString -- :: L.ByteString -> Builder
#if MIN_VERSION_bytestring(0,10,4)
, fromShortByteString -- :: T.ByteString -> Builder
#endif
-- * Flushing the buffer state
, flush
......@@ -64,6 +66,10 @@ module Data.Binary.Builder.Base (
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
#if MIN_VERSION_bytestring(0,10,4)
import qualified Data.ByteString.Short as T
import qualified Data.ByteString.Short.Internal as T
#endif
#if MIN_VERSION_base(4,9,0)
import Data.Semigroup
#else
......@@ -170,6 +176,17 @@ fromLazyByteString :: L.ByteString -> Builder
fromLazyByteString bss = flush `append` mapBuilder (bss `L.append`)
{-# INLINE fromLazyByteString #-}
#if MIN_VERSION_bytestring(0,10,4)
-- | /O(n)./ A builder taking 'T.ShortByteString' and copy it to a Builder,
-- satisfying
--
-- * @'toLazyByteString' ('fromShortByteString' bs) = 'L.fromChunks' ['T.fromShort' bs]
fromShortByteString :: T.ShortByteString -> Builder
fromShortByteString sbs = writeN (T.length sbs) $ \ptr ->
T.copyToPtr sbs 0 ptr (T.length sbs)
{-# INLINE fromShortByteString #-}
#endif
------------------------------------------------------------------------
-- Our internal buffer type
......
......@@ -73,6 +73,9 @@ import Data.List (unfoldr, foldl')
-- And needed for the instances:
import qualified Data.ByteString as B
#if MIN_VERSION_bytestring(0,10,4)
import qualified Data.ByteString.Short as BS
#endif
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.IntMap as IntMap
......@@ -553,6 +556,14 @@ instance Binary ByteString where
putLazyByteString bs
get = get >>= getLazyByteString
#if MIN_VERSION_bytestring(0,10,4)
instance Binary BS.ShortByteString where
put bs = do put (BS.length bs)
putShortByteString bs
get = get >>= fmap BS.toShort . getByteString
#endif
------------------------------------------------------------------------
-- Maps and Sets
......
......@@ -34,6 +34,9 @@ module Data.Binary.Put (
, putWord8
, putByteString
, putLazyByteString
#if MIN_VERSION_bytestring(0,10,4)
, putShortByteString
#endif
-- * Big-endian primitives
, putWord16be
......@@ -60,6 +63,9 @@ import qualified Data.Binary.Builder as B
import Data.Word
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
#if MIN_VERSION_bytestring(0,10,4)
import Data.ByteString.Short
#endif
import Control.Applicative
import Prelude -- Silence AMP warning.
......@@ -160,6 +166,13 @@ putLazyByteString :: L.ByteString -> Put
putLazyByteString = tell . B.fromLazyByteString
{-# INLINE putLazyByteString #-}
#if MIN_VERSION_bytestring(0,10,4)
-- | Write 'ShortByteString' to the buffer
putShortByteString :: ShortByteString -> Put
putShortByteString = tell . B.fromShortByteString
{-# INLINE putShortByteString #-}
#endif
-- | Write a Word16 in big endian format
putWord16be :: Word16 -> Put
putWord16be = tell . B.putWord16be
......
......@@ -7,6 +7,9 @@ import Test.QuickCheck
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
#if MIN_VERSION_bytestring(0,10,4)
import qualified Data.ByteString.Short as S
#endif
instance Arbitrary L.ByteString where
arbitrary = fmap L.fromChunks arbitrary
......@@ -14,6 +17,11 @@ instance Arbitrary L.ByteString where
instance Arbitrary B.ByteString where
arbitrary = B.pack `fmap` arbitrary
#if MIN_VERSION_bytestring(0,10,4)
instance Arbitrary S.ShortByteString where
arbitrary = S.toShort `fmap` arbitrary
#endif
instance (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e,
Arbitrary f) =>
Arbitrary (a,b,c,d,e,f) where
......
......@@ -20,6 +20,9 @@ import Control.Monad (unless, liftM2)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Internal as L
#if MIN_VERSION_bytestring(0,10,4)
import Data.ByteString.Short (ShortByteString)
#endif
import Data.Int
import Data.Ratio
import System.IO.Unsafe
......@@ -559,6 +562,9 @@ tests =
, ("B.ByteString", p (test :: T B.ByteString ))
, ("L.ByteString", p (test :: T L.ByteString ))
#if MIN_VERSION_bytestring(0,10,4)
, ("ShortByteString", p (test :: T ShortByteString ))
#endif
]
, testGroup "Invariants" $ map (uncurry testProperty)
......@@ -566,6 +572,10 @@ tests =
, ("[B.ByteString] invariant", p (prop_invariant :: B [B.ByteString] ))
, ("L.ByteString invariant", p (prop_invariant :: B L.ByteString ))
, ("[L.ByteString] invariant", p (prop_invariant :: B [L.ByteString] ))
#if MIN_VERSION_bytestring(0,10,4)
, ("ShortByteString invariant", p (prop_invariant :: B ShortByteString ))
, ("[ShortByteString] invariant", p (prop_invariant :: B [ShortByteString] ))
#endif
]
#ifdef HAS_FIXED_CONSTRUCTOR
, testGroup "Fixed"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment