diff options
author | Alex Gaynor <alex@alloy.us> | 2019-10-04 20:33:00 +0000 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2019-10-04 21:22:55 +0000 |
commit | 5f4aa5d79fba270d6a14f2a55999b5b6ccec2326 (patch) | |
tree | 73b3ca496b9cc3eabda98df18b6e3cd642c980a5 /src/bufio | |
parent | c1b7f508b0f4db605bfae4216f421bc3ec00de75 (diff) | |
download | go-5f4aa5d79fba270d6a14f2a55999b5b6ccec2326.tar.gz go-5f4aa5d79fba270d6a14f2a55999b5b6ccec2326.zip |
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: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/bufio')
-rw-r--r-- | src/bufio/bufio.go | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/src/bufio/bufio.go b/src/bufio/bufio.go index 0f05d3b322..c29f233f08 100644 --- a/src/bufio/bufio.go +++ b/src/bufio/bufio.go @@ -432,6 +432,7 @@ func (b *Reader) ReadBytes(delim byte) ([]byte, error) { var frag []byte var full [][]byte var err error + n := 0 for { var e error frag, e = b.ReadSlice(delim) @@ -447,18 +448,15 @@ func (b *Reader) ReadBytes(delim byte) ([]byte, error) { buf := make([]byte, len(frag)) copy(buf, frag) 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) - // Copy full pieces and fragment in. + // Allocate new buffer to hold the full pieces and the fragment. buf := make([]byte, n) n = 0 + // Copy full pieces and fragment in. for i := range full { n += copy(buf[n:], full[i]) } |