Add copyCons for copying small byte sequences with extra byte in front

This commit is contained in:
Andrew Martin 2022-02-01 00:32:18 -05:00
parent def7f05d8b
commit 4f03a98100
2 changed files with 18 additions and 1 deletions

View file

@ -78,7 +78,7 @@ test-suite test
HexWord64 HexWord64
Word16Tree Word16Tree
build-depends: build-depends:
, QuickCheck >=2.13.1 && <2.14 , QuickCheck >=2.13.1 && <2.15
, base >=4.12.0.0 && <5 , base >=4.12.0.0 && <5
, bytebuild , bytebuild
, byteslice , byteslice

View file

@ -22,6 +22,7 @@ module Data.Bytes.Builder
-- * Materialized Byte Sequences -- * Materialized Byte Sequences
, bytes , bytes
, copy , copy
, copyCons
, copy2 , copy2
, insert , insert
, byteArray , byteArray
@ -400,6 +401,22 @@ copy (Bytes (ByteArray src# ) (I# soff# ) (I# slen# )) = Builder
where where
!(I# newSz) = max (I# slen#) 4080 !(I# newSz) = max (I# slen#) 4080
-- | Variant of 'copy' that additionally pastes an extra byte in
-- front of the bytes.
copyCons :: Word8 -> Bytes -> Builder
copyCons (W8# w0) (Bytes (ByteArray src# ) (I# soff# ) (I# slen# )) = Builder
(\buf0 off0 len0 cs0 s0 -> case len0 <# (slen# +# 1#) of
1# -> case Exts.newByteArray# newSz s0 of
(# s1, buf1 #) -> case Exts.copyByteArray# src# soff# buf1 1# slen# s1 of
s2 -> case Exts.writeWord8Array# buf1 0# w0 s2 of
s3 -> (# s3, buf1, slen# +# 1#, newSz -# (slen# +# 1#), Mutable buf0 off0 cs0 #)
_ -> let !s1 = Exts.copyByteArray# src# soff# buf0 (off0 +# 1#) slen# s0
!s2 = Exts.writeWord8Array# buf0 off0 w0 s1
in (# s2, buf0, off0 +# (slen# +# 1#), len0 -# (slen# +# 1#), cs0 #)
)
where
!(I# newSz) = max ((I# slen#) + 1) 4080
cstring# :: Addr# -> Builder cstring# :: Addr# -> Builder
{-# inline cstring# #-} {-# inline cstring# #-}
cstring# x = cstring (Exts.Ptr x) cstring# x = cstring (Exts.Ptr x)