diff options
| author | Ian Lance Taylor <iant@golang.org> | 2017-10-30 16:05:18 -0700 |
|---|---|---|
| committer | Ian Lance Taylor <iant@golang.org> | 2017-11-03 19:06:15 +0000 |
| commit | a9e2479a4453ce1a2b3583212a6e64c99c31bbfe (patch) | |
| tree | c806165bbd78f3d591b51dfadbba3314e396f57e /src/bytes/bytes.go | |
| parent | 3043c355f430bb304bc0d09d8162632baa028a83 (diff) | |
| download | go-a9e2479a4453ce1a2b3583212a6e64c99c31bbfe.tar.xz | |
bytes: set cap of slices returned by Split and Fields and friends
This avoids the problem in which appending to a slice returned by
Split can affect subsequent slices.
Fixes #21149.
Change-Id: Ie3df2b9ceeb9605d4625f47d49073c5f348cf0a1
Reviewed-on: https://go-review.googlesource.com/74510
Reviewed-by: Jelte Fennema <github-tech@jeltef.nl>
Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/bytes/bytes.go')
| -rw-r--r-- | src/bytes/bytes.go | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go index 40d76be094..68ed8e1b43 100644 --- a/src/bytes/bytes.go +++ b/src/bytes/bytes.go @@ -39,7 +39,7 @@ func explode(s []byte, n int) [][]byte { break } _, size = utf8.DecodeRune(s) - a[na] = s[0:size] + a[na] = s[0:size:size] s = s[size:] na++ } @@ -219,7 +219,7 @@ func genSplit(s, sep []byte, sepSave, n int) [][]byte { if m < 0 { break } - a[i] = s[:m+sepSave] + a[i] = s[: m+sepSave : m+sepSave] s = s[m+len(sep):] i++ } @@ -302,7 +302,7 @@ func Fields(s []byte) [][]byte { i++ continue } - a[na] = s[fieldStart:i] + a[na] = s[fieldStart:i:i] na++ i++ // Skip spaces in between fields. @@ -312,7 +312,7 @@ func Fields(s []byte) [][]byte { fieldStart = i } if fieldStart < len(s) { // Last field might end at EOF. - a[na] = s[fieldStart:] + a[na] = s[fieldStart:len(s):len(s)] } return a } @@ -363,7 +363,7 @@ func FieldsFunc(s []byte, f func(rune) bool) [][]byte { // Create subslices from recorded field indices. a := make([][]byte, len(spans)) for i, span := range spans { - a[i] = s[span.start:span.end] + a[i] = s[span.start:span.end:span.end] } return a |
