mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
bytes: limit allocation in SplitN
This commit is contained in:
parent
6f1dce0fcb
commit
f8df09d65e
2 changed files with 5 additions and 0 deletions
|
|
@ -348,6 +348,9 @@ func genSplit(s, sep []byte, sepSave, n int) [][]byte {
|
||||||
if n < 0 {
|
if n < 0 {
|
||||||
n = Count(s, sep) + 1
|
n = Count(s, sep) + 1
|
||||||
}
|
}
|
||||||
|
if n > len(s)+1 {
|
||||||
|
n = len(s) + 1
|
||||||
|
}
|
||||||
|
|
||||||
a := make([][]byte, n)
|
a := make([][]byte, n)
|
||||||
n--
|
n--
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
. "bytes"
|
. "bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"internal/testenv"
|
"internal/testenv"
|
||||||
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -723,6 +724,7 @@ var splittests = []SplitTest{
|
||||||
{"1 2", " ", 3, []string{"1", "2"}},
|
{"1 2", " ", 3, []string{"1", "2"}},
|
||||||
{"123", "", 2, []string{"1", "23"}},
|
{"123", "", 2, []string{"1", "23"}},
|
||||||
{"123", "", 17, []string{"1", "2", "3"}},
|
{"123", "", 17, []string{"1", "2", "3"}},
|
||||||
|
{"bT", "T", math.MaxInt / 4, []string{"b", ""}},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSplit(t *testing.T) {
|
func TestSplit(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue