tape: Improve table decoding

This commit is contained in:
Sasha Koshka 2025-05-18 16:08:47 -04:00
parent f50b2ca0cd
commit 568431f4c3

View File

@ -1,27 +1,24 @@
package tape package tape
import "iter"
// encoding and decoding functions must not make any allocations // encoding and decoding functions must not make any allocations
type TablePushFunc func(tag uint16, value []byte) (n int, err error) type TablePushFunc func(tag uint16, value []byte) (n int, err error)
func DecodeTable(data []byte) iter.Seq2[uint16, []byte] { type TablePullFunc func() (tag uint16, value []byte, n int, err error)
return func(yield func(tag uint16, value []byte) bool) {
n := 0
for {
tag, nn, err := DecodeI16[uint16](data[n:])
if err != nil { return }
n += nn
length, nn, err := DecodeGBEU[uint64](data[n:]) func DecodeTable(data []byte) TablePullFunc {
if err != nil { return } return func() (tag uint16, value []byte, n int, err error) {
n += nn tag, nn, err := DecodeI16[uint16](data[n:])
if err != nil { return tag, value, n, err }
n += nn
value := data[n:n + int(length)] length, nn, err := DecodeGBEU[uint64](data[n:])
yield(tag, value) if err != nil { return tag, value, n, err }
n += int(length) n += nn
}
value = data[n:n + int(length)]
n += int(length)
return tag, value, n, err
} }
} }