@@ -3,6 +3,7 @@ module BitWriter exposing
33 , bit
44 , bits
55 , empty
6+ , getOffset
67 , run
78 )
89
@@ -13,7 +14,7 @@ import Bytes.Encode as BE
1314
1415empty : BitWriter
1516empty =
16− BitWriter { running = Nothing, collected = BE.sequence [] }
17+ BitWriter { running = Nothing, collected = BE.sequence [], offset = 0 }
1718
1819
1920run : BitWriter -> Bytes
@@ -33,17 +34,31 @@ type BitWriter
3334 { value : Int
3435 , length : Int
3536 }
37+ , offset : Int
3638 , collected : BE.Encoder
3739 }
3840
3941
42+getOffset : BitWriter -> Int
43+getOffset (BitWriter { offset }) =
44+ offset
45+
46+
4047{-| If the given int is longer than 1 bit the behaviour is undefined
4148-}
4249bit : Int -> BitWriter -> BitWriter
43−bit b (BitWriter { running, collected }) =
50+bit b (BitWriter { running, collected, offset }) =
51+ let
52+ newOffset =
53+ offset + 1
54+ in
4455 case running of
4556 Nothing ->
46− BitWriter { running = Just { value = b, length = 1 }, collected = collected }
57+ BitWriter
58+ { running = Just { value = b, length = 1 }
59+ , collected = collected
60+ , offset = newOffset
61+ }
4762
4863 Just { value, length } ->
4964 let
@@ -51,10 +66,18 @@ bit b (BitWriter { running, collected }) =
5166 Bitwise.or (Bitwise.shiftLeftBy length b) value
5267 in
5368 if length == 7 then
54− BitWriter { running = Nothing, collected = BE.sequence [ collected, BE.unsignedInt8 newValue ] }
69+ BitWriter
70+ { running = Nothing
71+ , collected = BE.sequence [ collected, BE.unsignedInt8 newValue ]
72+ , offset = newOffset
73+ }
5574
5675 else
57− BitWriter { running = Just { value = newValue, length = length + 1 }, collected = collected }
76+ BitWriter
77+ { running = Just { value = newValue, length = length + 1 }
78+ , collected = collected
79+ , offset = newOffset
80+ }
5881
5982
6083