From 3688e024e82fe520ddee610a57e6b811221917b5 Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Mon, 9 Dec 2019 13:25:51 -0500 Subject: [PATCH] Add runOnto --- CHANGELOG.md | 1 + src/Data/ByteArray/Builder.hs | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70e42de..ea9eb56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ array over and over. * Add `consLength`. * Add `putManyConsLength`, useful for chunked HTTP encoding. +* Add `runOnto` ## 0.3.1.0 -- 2019-11-20 diff --git a/src/Data/ByteArray/Builder.hs b/src/Data/ByteArray/Builder.hs index b42000b..ee9f0da 100644 --- a/src/Data/ByteArray/Builder.hs +++ b/src/Data/ByteArray/Builder.hs @@ -13,6 +13,7 @@ module Data.ByteArray.Builder , fromBounded -- * Evaluation , run + , runOnto , putMany , putManyConsLength -- * Materialized Byte Sequences @@ -139,12 +140,21 @@ run :: Int -- ^ Size of initial chunk (use 4080 if uncertain) -> Builder -- ^ Builder -> Chunks -run hint@(I# hint# ) (Builder f) = runST $ do +run !hint bldr = runOnto hint bldr ChunksNil + +-- | Run a builder. The resulting chunks are consed onto the +-- beginning of an existing sequence of chunks. +runOnto :: + Int -- ^ Size of initial chunk (use 4080 if uncertain) + -> Builder -- ^ Builder + -> Chunks + -> Chunks +runOnto hint@(I# hint# ) (Builder f) cs0 = runST $ do MutableByteArray buf0 <- PM.newByteArray hint cs <- ST $ \s0 -> case f buf0 0# hint# Initial s0 of (# s1, bufX, offX, _, csX #) -> (# s1, Mutable bufX offX csX #) - reverseCommitsOntoChunks ChunksNil cs + reverseCommitsOntoChunks cs0 cs -- | Run a builder against lots of elements. This fills the same -- underlying buffer over and over again. Do not let the argument to