mirror of
https://github.com/golang/go
synced 2024-11-24 10:30:10 -07:00
strings: limit allocation in SplitN
This commit is contained in:
parent
7ca6902c17
commit
d1f45b44a8
@ -244,6 +244,9 @@ func genSplit(s, sep string, sepSave, n int) []string {
|
||||
n = Count(s, sep) + 1
|
||||
}
|
||||
|
||||
if n > len(s)+1 {
|
||||
n = len(s) + 1
|
||||
}
|
||||
a := make([]string, n)
|
||||
n--
|
||||
i := 0
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"strconv"
|
||||
@ -404,6 +405,7 @@ var splittests = []SplitTest{
|
||||
{faces, "~", -1, []string{faces}},
|
||||
{"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
|
||||
{"1 2", " ", 3, []string{"1", "2"}},
|
||||
{"", "T", math.MaxInt / 4, []string{""}},
|
||||
}
|
||||
|
||||
func TestSplit(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user