Skip to content

Commit 7be9150

Browse files
committed
Add length, bytes, byteArray to Data.Builder.Catenable.Bytes
1 parent 41bdb8d commit 7be9150

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
* Add `length`, `shortText`, `char`, `word32Dec`, `word64Dec`, `int32Dec`, `int64Dec`
66
to `Data.Builder.Catenable.Text`.
7-
7+
* Add `length`, `bytes`, and `byteArray` to `Data.Builder.Catenable.Bytes`.
88

99
## 0.1.2.0 -- 2022-08-09
1010

src/Data/Builder/Catenable/Bytes.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ module Data.Builder.Catenable.Bytes
1010
, pattern (:>)
1111
-- * Run
1212
, run
13+
-- * Properties
14+
, length
15+
-- * Create
16+
, bytes
17+
, byteArray
1318
) where
1419

20+
import Prelude hiding (length)
21+
1522
import Control.Monad.ST (ST,runST)
1623
import Data.Bytes (Bytes)
1724
import Data.Bytes.Chunks (Chunks(ChunksNil))
25+
import Data.Primitive (ByteArray)
1826

27+
import qualified Data.Bytes as Bytes
1928
import qualified Data.Bytes.Builder as BB
2029
import qualified Data.Bytes.Builder.Unsafe as BBU
2130

@@ -42,6 +51,14 @@ pattern (:<) x y = Cons x y
4251
pattern (:>) :: Builder -> Bytes -> Builder
4352
pattern (:>) x y = Snoc x y
4453

54+
-- | Number of bytes in the sequence.
55+
length :: Builder -> Int
56+
length b0 = case b0 of
57+
Empty -> 0
58+
Cons x b1 -> Bytes.length x + length b1
59+
Snoc b1 x -> Bytes.length x + length b1
60+
Append x y -> length x + length y
61+
4562
run :: Builder -> Chunks
4663
{-# noinline run #-}
4764
run b = runST $ do
@@ -62,4 +79,8 @@ pushCatenable !bldr0 b = case b of
6279
bldr1 <- pushCatenable bldr0 x
6380
pushCatenable bldr1 y
6481

82+
bytes :: Bytes -> Builder
83+
bytes !b = Cons b Empty
6584

85+
byteArray :: ByteArray -> Builder
86+
byteArray !b = Cons (Bytes.fromByteArray b) Empty

0 commit comments

Comments
 (0)