tape: Improve table decoding
This commit is contained in:
parent
f50b2ca0cd
commit
568431f4c3
@ -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
|
func DecodeTable(data []byte) TablePullFunc {
|
||||||
for {
|
return func() (tag uint16, value []byte, n int, err error) {
|
||||||
tag, nn, err := DecodeI16[uint16](data[n:])
|
tag, nn, err := DecodeI16[uint16](data[n:])
|
||||||
if err != nil { return }
|
if err != nil { return tag, value, n, err }
|
||||||
n += nn
|
n += nn
|
||||||
|
|
||||||
length, nn, err := DecodeGBEU[uint64](data[n:])
|
length, nn, err := DecodeGBEU[uint64](data[n:])
|
||||||
if err != nil { return }
|
if err != nil { return tag, value, n, err }
|
||||||
n += nn
|
n += nn
|
||||||
|
|
||||||
value := data[n:n + int(length)]
|
value = data[n:n + int(length)]
|
||||||
yield(tag, value)
|
|
||||||
n += int(length)
|
n += int(length)
|
||||||
}
|
return tag, value, n, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user