Skip to content

Commit 7ac8bc9

Browse files
committed
grow buffer doc + fmt
1 parent a602069 commit 7ac8bc9

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

buffer.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,22 @@ func (b *buffer) fill(need int) (err error) {
5555
return
5656
}
5757

58+
// grow the buffer to the given size
5859
// credit for this code snippet goes to Maxim Khitrov
5960
// https://groups.google.com/forum/#!topic/golang-nuts/ETbw1ECDgRs
6061
func (b *buffer) grow(size int) {
62+
// If append would be too expensive, alloc a new slice
6163
if size > 2*cap(b.buf) {
6264
newBuf := make([]byte, size)
6365
copy(newBuf, b.buf)
6466
b.buf = newBuf
6567
return
66-
} else {
67-
for cap(b.buf) < size {
68-
b.buf = append(b.buf[:cap(b.buf)], 0)
69-
}
70-
b.buf = b.buf[:cap(b.buf)]
7168
}
69+
70+
for cap(b.buf) < size {
71+
b.buf = append(b.buf[:cap(b.buf)], 0)
72+
}
73+
b.buf = b.buf[:cap(b.buf)]
7274
}
7375

7476
// returns next N bytes from buffer.

0 commit comments

Comments
 (0)