mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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>
This commit is contained in:
parent
3043c355f4
commit
a9e2479a44
2 changed files with 65 additions and 7 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue