1
0
mirror of https://github.com/golang/go synced 2024-11-25 01:47:56 -07:00

gob: slightly simpler decodeUint

R=r
CC=golang-dev
https://golang.org/cl/5089048
This commit is contained in:
Robert Griesemer 2011-09-21 14:47:00 -07:00
parent 91a48115bb
commit 3e02fff007

View File

@ -70,13 +70,12 @@ func decodeUintReader(r io.Reader, buf []byte) (x uint64, width int, err os.Erro
if b <= 0x7f { if b <= 0x7f {
return uint64(b), width, nil return uint64(b), width, nil
} }
nb := -int(int8(b)) n := -int(int8(b))
if nb > uint64Size { if n > uint64Size {
err = errBadUint err = errBadUint
return return
} }
var n int width, err = io.ReadFull(r, buf[0:n])
n, err = io.ReadFull(r, buf[0:nb])
if err != nil { if err != nil {
if err == os.EOF { if err == os.EOF {
err = io.ErrUnexpectedEOF err = io.ErrUnexpectedEOF
@ -84,11 +83,10 @@ func decodeUintReader(r io.Reader, buf []byte) (x uint64, width int, err os.Erro
return return
} }
// Could check that the high byte is zero but it's not worth it. // Could check that the high byte is zero but it's not worth it.
for i := 0; i < n; i++ { for _, b := range buf[0:width] {
x <<= 8 x = x<<8 | uint64(b)
x |= uint64(buf[i])
width++
} }
width++ // +1 for length byte
return return
} }
@ -102,19 +100,18 @@ func (state *decoderState) decodeUint() (x uint64) {
if b <= 0x7f { if b <= 0x7f {
return uint64(b) return uint64(b)
} }
nb := -int(int8(b)) n := -int(int8(b))
if nb > uint64Size { if n > uint64Size {
error(errBadUint) error(errBadUint)
} }
n, err := state.b.Read(state.buf[0:nb]) width, err := state.b.Read(state.buf[0:n])
if err != nil { if err != nil {
error(err) error(err)
} }
// Don't need to check error; it's safe to loop regardless. // Don't need to check error; it's safe to loop regardless.
// Could check that the high byte is zero but it's not worth it. // Could check that the high byte is zero but it's not worth it.
for i := 0; i < n; i++ { for _, b := range state.buf[0:width] {
x <<= 8 x = x<<8 | uint64(b)
x |= uint64(state.buf[i])
} }
return x return x
} }