diff options
author | Robert Griesemer <gri@golang.org> | 2011-09-21 14:47:00 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2011-09-21 14:47:00 -0700 |
commit | dc1f1ff3ceccd9307cf5145e9e26d6315486a3b8 (patch) | |
tree | 024284061d2d87935c07d189467f4d823cccf4eb | |
parent | 11f1a49fc15fcff9e3f479a21c8145b750a58e91 (diff) | |
download | go-dc1f1ff3ceccd9307cf5145e9e26d6315486a3b8.tar.gz |
gob: slightly simpler decodeUint
R=r
CC=golang-dev
http://codereview.appspot.com/5089048
-rw-r--r-- | src/pkg/gob/decode.go | 25 |
1 files changed, 11 insertions, 14 deletions
diff --git a/src/pkg/gob/decode.go b/src/pkg/gob/decode.go index 60c0e10ce..9d8d90587 100644 --- a/src/pkg/gob/decode.go +++ b/src/pkg/gob/decode.go @@ -70,13 +70,12 @@ func decodeUintReader(r io.Reader, buf []byte) (x uint64, width int, err os.Erro if b <= 0x7f { return uint64(b), width, nil } - nb := -int(int8(b)) - if nb > uint64Size { + n := -int(int8(b)) + if n > uint64Size { err = errBadUint return } - var n int - n, err = io.ReadFull(r, buf[0:nb]) + width, err = io.ReadFull(r, buf[0:n]) if err != nil { if err == os.EOF { err = io.ErrUnexpectedEOF @@ -84,11 +83,10 @@ func decodeUintReader(r io.Reader, buf []byte) (x uint64, width int, err os.Erro return } // Could check that the high byte is zero but it's not worth it. - for i := 0; i < n; i++ { - x <<= 8 - x |= uint64(buf[i]) - width++ + for _, b := range buf[0:width] { + x = x<<8 | uint64(b) } + width++ // +1 for length byte return } @@ -102,19 +100,18 @@ func (state *decoderState) decodeUint() (x uint64) { if b <= 0x7f { return uint64(b) } - nb := -int(int8(b)) - if nb > uint64Size { + n := -int(int8(b)) + if n > uint64Size { error(errBadUint) } - n, err := state.b.Read(state.buf[0:nb]) + width, err := state.b.Read(state.buf[0:n]) if err != nil { error(err) } // 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. - for i := 0; i < n; i++ { - x <<= 8 - x |= uint64(state.buf[i]) + for _, b := range state.buf[0:width] { + x = x<<8 | uint64(b) } return x } |