Add replicate

This commit is contained in:
Andrew Martin 2020-06-01 11:28:35 -04:00
parent 616f724846
commit 5c2dd94acf
3 changed files with 20 additions and 1 deletions

View file

@ -5,6 +5,10 @@ Note: Prior to version 0.3.4.0, this library was named
`small-bytearray-builder` is now just a compatibility shim
to ease the migration process.
## 0.3.6.0 -- 2020-??-??
* Add `replicate`.
## 0.3.5.0 -- 2020-05-01
* Add `wordLEB128` and `word64LEB128`.

View file

@ -1,6 +1,6 @@
cabal-version: 2.2
name: bytebuild
version: 0.3.5.0
version: 0.3.6.0
synopsis: Serialize to a small byte arrays
description:
This is similar to the builder facilities provided by

View file

@ -118,10 +118,14 @@ module Data.Bytes.Builder
-- * Encode Floating-Point Types
-- ** Human-Readable
, doubleDec
-- * Replication
, replicate
-- * Control
, flush
) where
import Prelude hiding (replicate)
import Control.Exception (SomeException,toException)
import Control.Monad.ST (ST,runST)
import Control.Monad.IO.Class (MonadIO,liftIO)
@ -1130,6 +1134,17 @@ backwardsWordLoop arr off0 x0 = go off0 x0 where
go (off + 1) y
else pure off
-- | Replicate a byte the given number of times.
replicate ::
Int -- ^ Number of times to replicate the byte
-> Word8 -- ^ Byte to replicate
-> Builder
replicate !len !w = fromEffect len
(\marr off -> do
PM.setByteArray marr off len w
pure (off + len)
)
-- Based on C code from https://stackoverflow.com/a/5558614
-- For numbers less than 1073741829, this gives a correct answer.
approxDiv10 :: Word -> Word