[commit: packages/binary] master: Support ShortByteStrings. (9fa6234)

git at git.haskell.org git at git.haskell.org
Tue Feb 2 21:05:05 UTC 2016


Repository : ssh://git@git.haskell.org/binary

On branch  : master
Link       : http://git.haskell.org/packages/binary.git/commitdiff/9fa6234c1f7b91d6c32a689bdcf9b40718fa2525

>---------------------------------------------------------------

commit 9fa6234c1f7b91d6c32a689bdcf9b40718fa2525
Author: Alexander Vershilov <alexander.vershilov at gmail.com>
Date:   Sat Jan 23 03:21:13 2016 +0300

    Support ShortByteStrings.
    
    Implement Binary instance and builders for ShortByteString.


>---------------------------------------------------------------

9fa6234c1f7b91d6c32a689bdcf9b40718fa2525
 src/Data/Binary/Builder.hs      |  3 +++
 src/Data/Binary/Builder/Base.hs | 19 ++++++++++++++++++-
 src/Data/Binary/Class.hs        | 11 +++++++++++
 src/Data/Binary/Put.hs          | 13 +++++++++++++
 tests/Arbitrary.hs              |  8 ++++++++
 tests/QC.hs                     | 10 ++++++++++
 6 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/src/Data/Binary/Builder.hs b/src/Data/Binary/Builder.hs
index 7af1a4b..88e38ed 100644
--- a/src/Data/Binary/Builder.hs
+++ b/src/Data/Binary/Builder.hs
@@ -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
diff --git a/src/Data/Binary/Builder/Base.hs b/src/Data/Binary/Builder/Base.hs
index 62d286e..169937a 100644
--- a/src/Data/Binary/Builder/Base.hs
+++ b/src/Data/Binary/Builder/Base.hs
@@ -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
diff --git a/src/Data/Binary/Class.hs b/src/Data/Binary/Class.hs
index ebac8b0..f3c2d70 100644
--- a/src/Data/Binary/Class.hs
+++ b/src/Data/Binary/Class.hs
@@ -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
 
diff --git a/src/Data/Binary/Put.hs b/src/Data/Binary/Put.hs
index a05bfc7..1858312 100644
--- a/src/Data/Binary/Put.hs
+++ b/src/Data/Binary/Put.hs
@@ -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
diff --git a/tests/Arbitrary.hs b/tests/Arbitrary.hs
index dcb9d44..3d6281f 100644
--- a/tests/Arbitrary.hs
+++ b/tests/Arbitrary.hs
@@ -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
diff --git a/tests/QC.hs b/tests/QC.hs
index 94348ff..be11864 100644
--- a/tests/QC.hs
+++ b/tests/QC.hs
@@ -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"



More information about the ghc-commits mailing list