Parcourir la source

grow buffer doc + fmt

Julien Schmidt il y a 12 ans
Parent
commit
7ac8bc9feb
1 fichiers modifiés avec 7 ajouts et 5 suppressions
  1. 7 5
      buffer.go

+ 7 - 5
buffer.go

@@ -55,20 +55,22 @@ func (b *buffer) fill(need int) (err error) {
 	return
 	return
 }
 }
 
 
+// grow the buffer to the given size
 // credit for this code snippet goes to Maxim Khitrov
 // credit for this code snippet goes to Maxim Khitrov
 // https://groups.google.com/forum/#!topic/golang-nuts/ETbw1ECDgRs
 // https://groups.google.com/forum/#!topic/golang-nuts/ETbw1ECDgRs
 func (b *buffer) grow(size int) {
 func (b *buffer) grow(size int) {
+	// If append would be too expensive, alloc a new slice
 	if size > 2*cap(b.buf) {
 	if size > 2*cap(b.buf) {
 		newBuf := make([]byte, size)
 		newBuf := make([]byte, size)
 		copy(newBuf, b.buf)
 		copy(newBuf, b.buf)
 		b.buf = newBuf
 		b.buf = newBuf
 		return
 		return
-	} else {
-		for cap(b.buf) < size {
-			b.buf = append(b.buf[:cap(b.buf)], 0)
-		}
-		b.buf = b.buf[:cap(b.buf)]
 	}
 	}
+
+	for cap(b.buf) < size {
+		b.buf = append(b.buf[:cap(b.buf)], 0)
+	}
+	b.buf = b.buf[:cap(b.buf)]
 }
 }
 
 
 // returns next N bytes from buffer.
 // returns next N bytes from buffer.