Add runOnto

This commit is contained in:
Andrew Martin 2019-12-09 13:25:51 -05:00
parent e8de684ae2
commit 3688e024e8
2 changed files with 13 additions and 2 deletions

View file

@ -6,6 +6,7 @@
array over and over. array over and over.
* Add `consLength`. * Add `consLength`.
* Add `putManyConsLength`, useful for chunked HTTP encoding. * Add `putManyConsLength`, useful for chunked HTTP encoding.
* Add `runOnto`
## 0.3.1.0 -- 2019-11-20 ## 0.3.1.0 -- 2019-11-20

View file

@ -13,6 +13,7 @@ module Data.ByteArray.Builder
, fromBounded , fromBounded
-- * Evaluation -- * Evaluation
, run , run
, runOnto
, putMany , putMany
, putManyConsLength , putManyConsLength
-- * Materialized Byte Sequences -- * Materialized Byte Sequences
@ -139,12 +140,21 @@ run ::
Int -- ^ Size of initial chunk (use 4080 if uncertain) Int -- ^ Size of initial chunk (use 4080 if uncertain)
-> Builder -- ^ Builder -> Builder -- ^ Builder
-> Chunks -> 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 MutableByteArray buf0 <- PM.newByteArray hint
cs <- ST $ \s0 -> case f buf0 0# hint# Initial s0 of cs <- ST $ \s0 -> case f buf0 0# hint# Initial s0 of
(# s1, bufX, offX, _, csX #) -> (# s1, bufX, offX, _, csX #) ->
(# s1, Mutable 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 -- | Run a builder against lots of elements. This fills the same
-- underlying buffer over and over again. Do not let the argument to -- underlying buffer over and over again. Do not let the argument to