]> Cypherpunks repositories - gostls13.git/commitdiff
image/gif: simplify blockReader.Read.
authorRob Pike <r@golang.org>
Tue, 24 May 2011 01:02:44 +0000 (11:02 +1000)
committerRob Pike <r@golang.org>
Tue, 24 May 2011 01:02:44 +0000 (11:02 +1000)
Inverting the tests avoids recursion and simplifies the flow.

R=nigeltao
CC=golang-dev
https://golang.org/cl/4551057

src/pkg/image/gif/reader.go

index 5dd404036ce717b1c7a035b3bd60a00531db807a..4de18c323b0a406787fbde5cf3a59c814f5367c9 100644 (file)
@@ -94,28 +94,26 @@ type blockReader struct {
        tmp   [256]byte
 }
 
-func (b *blockReader) Read(p []byte) (n int, err os.Error) {
+func (b *blockReader) Read(p []byte) (int, os.Error) {
        if len(p) == 0 {
-               return
-       }
-       if len(b.slice) > 0 {
-               n = copy(p, b.slice)
-               b.slice = b.slice[n:]
-               return
-       }
-       var blockLen uint8
-       blockLen, err = b.r.ReadByte()
-       if err != nil {
-               return
+               return 0, nil
        }
-       if blockLen == 0 {
-               return 0, os.EOF
-       }
-       b.slice = b.tmp[0:blockLen]
-       if _, err = io.ReadFull(b.r, b.slice); err != nil {
-               return
+       if len(b.slice) == 0 {
+               blockLen, err := b.r.ReadByte()
+               if err != nil {
+                       return 0, err
+               }
+               if blockLen == 0 {
+                       return 0, os.EOF
+               }
+               b.slice = b.tmp[0:blockLen]
+               if _, err = io.ReadFull(b.r, b.slice); err != nil {
+                       return 0, err
+               }
        }
-       return b.Read(p)
+       n := copy(p, b.slice)
+       b.slice = b.slice[n:]
+       return n, nil
 }
 
 // decode reads a GIF image from r and stores the result in d.