codec/h264/h26dec/read.go: added fieldReader type to provide sticky error and specific methods for bool and int reading

This commit is contained in:
Saxon 2019-07-27 13:35:05 +09:30
parent 46c709404f
commit 404e6493b6
1 changed files with 40 additions and 0 deletions

View File

@ -228,3 +228,43 @@ func readFlags(br *bits.BitReader, flags []flag) error {
}
return nil
}
// fieldReader provides methods for reading bool and int fields from a
// bits.BitReader with a sticky error that may be checked after a series of
// parsing read calls.
type fieldReader struct {
e error
br *bits.BitReader
}
// newFieldReader returns a new fieldReader.
func newFieldReader(br *bits.BitReader) fieldReader {
return fieldReader{br: br}
}
// readBool returns a bool from reading one bit from br. If we have an error
// already, we do not continue with the read.
func (r fieldReader) readBool() bool {
if r.e != nil {
return false
}
var b uint64
b, r.e = r.br.ReadBits(1)
return b == 1
}
// readBitsInt returns an int from reading n bits from br. If we have an error
// already, we do not continue with the read.
func (r fieldReader) readBitsInt(n int) int {
if r.e != nil {
return 0
}
var b uint64
b, r.e = r.br.ReadBits(n)
return int(b)
}
// err returns the fieldReader's error e.
func (r fieldReader) err() error {
return r.e
}