Commit 5f4aa5d7 authored by Alex Gaynor's avatar Alex Gaynor Committed by Ian Lance Taylor

bufio: simplify bufio.Reader.ReadBytes to avoid an extra loop over a slice

Change-Id: Icb1c3eb30147180ba5949a25c65b48307b14c1ca
GitHub-Last-Rev: 937ae8641321139b9165ce7d57abeac5a67dc24d
GitHub-Pull-Request: golang/go#34704
Reviewed-on: https://go-review.googlesource.com/c/go/+/199157
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent c1b7f508
...@@ -432,6 +432,7 @@ func (b *Reader) ReadBytes(delim byte) ([]byte, error) { ...@@ -432,6 +432,7 @@ func (b *Reader) ReadBytes(delim byte) ([]byte, error) {
var frag []byte var frag []byte
var full [][]byte var full [][]byte
var err error var err error
n := 0
for { for {
var e error var e error
frag, e = b.ReadSlice(delim) frag, e = b.ReadSlice(delim)
...@@ -447,18 +448,15 @@ func (b *Reader) ReadBytes(delim byte) ([]byte, error) { ...@@ -447,18 +448,15 @@ func (b *Reader) ReadBytes(delim byte) ([]byte, error) {
buf := make([]byte, len(frag)) buf := make([]byte, len(frag))
copy(buf, frag) copy(buf, frag)
full = append(full, buf) full = append(full, buf)
n += len(buf)
} }
// Allocate new buffer to hold the full pieces and the fragment.
n := 0
for i := range full {
n += len(full[i])
}
n += len(frag) n += len(frag)
// Copy full pieces and fragment in. // Allocate new buffer to hold the full pieces and the fragment.
buf := make([]byte, n) buf := make([]byte, n)
n = 0 n = 0
// Copy full pieces and fragment in.
for i := range full { for i := range full {
n += copy(buf[n:], full[i]) n += copy(buf[n:], full[i])
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment