[commit: packages/bytestring] ghc-head: Add a builder primitive for ShortByteString (3bb3b57)

git at git.haskell.org git
Fri Oct 4 08:27:41 UTC 2013


Repository : ssh://git at git.haskell.org/bytestring

On branch  : ghc-head
Link       : http://git.haskell.org/packages/bytestring.git/commitdiff/3bb3b5738b2d9001f7e123f844c61a3d690662c1

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

commit 3bb3b5738b2d9001f7e123f844c61a3d690662c1
Author: Duncan Coutts <duncan at community.haskell.org>
Date:   Tue Sep 17 12:29:37 2013 +0100

    Add a builder primitive for ShortByteString


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

3bb3b5738b2d9001f7e123f844c61a3d690662c1
 Data/ByteString/Builder.hs          |    1 +
 Data/ByteString/Builder/Internal.hs |   32 ++++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/Data/ByteString/Builder.hs b/Data/ByteString/Builder.hs
index d491c2a..6ee44d0 100644
--- a/Data/ByteString/Builder.hs
+++ b/Data/ByteString/Builder.hs
@@ -197,6 +197,7 @@ module Data.ByteString.Builder
       -- ** Binary encodings
     , byteString
     , lazyByteString
+    , shortByteString
     , int8
     , word8
 
diff --git a/Data/ByteString/Builder/Internal.hs b/Data/ByteString/Builder/Internal.hs
index b07355b..55bd550 100644
--- a/Data/ByteString/Builder/Internal.hs
+++ b/Data/ByteString/Builder/Internal.hs
@@ -83,6 +83,8 @@ module Data.ByteString.Builder.Internal (
   , lazyByteStringInsert
   , lazyByteStringThreshold
   
+  , shortByteString
+
   , lazyByteStringC
 
   , maximalCopySize
@@ -123,6 +125,7 @@ import Data.Monoid
 import qualified Data.ByteString               as S
 import qualified Data.ByteString.Internal      as S
 import qualified Data.ByteString.Lazy.Internal as L
+import qualified Data.ByteString.Short.Internal as Sh
 
 #if __GLASGOW_HASKELL__ >= 611
 import GHC.IO.Buffer (Buffer(..), newByteBuffer)
@@ -637,6 +640,35 @@ byteStringInsert =
       | otherwise =
           return $ insertChunks op (fromIntegral $ S.length bs) (L.Chunk bs) k
 
+-- Short bytestrings
+------------------------------------------------------------------------------
+
+-- | Construct a 'Builder' that copies the 'SH.ShortByteString'.
+--
+{-# INLINE shortByteString #-}
+shortByteString :: Sh.ShortByteString -> Builder
+shortByteString = \sbs -> builder $ shortByteStringCopyStep sbs
+
+-- | Copy the bytes from a 'SH.ShortByteString' into the output stream.
+{-# INLINE shortByteStringCopyStep #-}
+shortByteStringCopyStep :: Sh.ShortByteString  -- ^ Input 'SH.ShortByteString'.
+                        -> BuildStep a -> BuildStep a
+shortByteStringCopyStep !sbs k =
+    go 0 (Sh.length sbs)
+  where
+    go !ip !ipe !(BufferRange op ope)
+      | inpRemaining <= outRemaining = do
+          Sh.copyToPtr sbs ip op inpRemaining
+          let !br' = BufferRange (op `plusPtr` inpRemaining) ope
+          k br'
+      | otherwise = do
+          Sh.copyToPtr sbs ip op outRemaining
+          let !ip' = ip + outRemaining
+          return $ bufferFull 1 ope (go ip' ipe)
+      where
+        outRemaining = ope `minusPtr` op
+        inpRemaining = ipe - ip
+
 
 -- Lazy bytestrings
 ------------------------------------------------------------------------------




More information about the ghc-commits mailing list