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
import "iter"
// encoding and decoding functions must not make any allocations
type TablePushFunc func(tag uint16, value []byte) (n int, err error)
func DecodeTable(data []byte) iter.Seq2[uint16, []byte] {
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
type TablePullFunc func() (tag uint16, value []byte, n int, err error)
length, nn, err := DecodeGBEU[uint64](data[n:])
if err != nil { return }
n += nn
func DecodeTable(data []byte) TablePullFunc {
return func() (tag uint16, value []byte, n int, err error) {
tag, nn, err := DecodeI16[uint16](data[n:])
if err != nil { return tag, value, n, err }
n += nn
value := data[n:n + int(length)]
yield(tag, value)
n += int(length)
}
length, nn, err := DecodeGBEU[uint64](data[n:])
if err != nil { return tag, value, n, err }
n += nn
value = data[n:n + int(length)]
n += int(length)
return tag, value, n, err
}
}