Commit 20930c76 authored by Francesc Campoy's avatar Francesc Campoy Committed by Brad Fitzpatrick

regexp: limit the capacity of slices of bytes returned by FindX

This change limits the capacity of the slices of bytes returned by:

- Find
- FindAll
- FindAllSubmatch

to be the same as their length.

Fixes #30169

Change-Id: I07b632757d2bfeab42fce0d42364e2a16c597360
Reviewed-on: https://go-review.googlesource.com/c/161877Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 98cbf45c
......@@ -161,6 +161,9 @@ func TestFind(t *testing.T) {
t.Errorf("expected match; got none: %s", test)
case test.matches != nil && result != nil:
expect := test.text[test.matches[0][0]:test.matches[0][1]]
if len(result) != cap(result) {
t.Errorf("expected capacity %d got %d: %s", len(result), cap(result), test)
}
if expect != string(result) {
t.Errorf("expected %q got %q: %s", expect, result, test)
}
......@@ -242,9 +245,13 @@ func TestFindAll(t *testing.T) {
continue
}
for k, e := range test.matches {
got := result[k]
if len(got) != cap(got) {
t.Errorf("match %d: expected capacity %d got %d: %s", k, len(got), cap(got), test)
}
expect := test.text[e[0]:e[1]]
if expect != string(result[k]) {
t.Errorf("match %d: expected %q got %q: %s", k, expect, result[k], test)
if expect != string(got) {
t.Errorf("match %d: expected %q got %q: %s", k, expect, got, test)
}
}
}
......@@ -323,9 +330,14 @@ func testSubmatchBytes(test *FindTest, n int, submatches []int, result [][]byte,
}
continue
}
got := result[k/2]
if len(got) != cap(got) {
t.Errorf("match %d: expected capacity %d got %d: %s", n, len(got), cap(got), test)
return
}
expect := test.text[submatches[k]:submatches[k+1]]
if expect != string(result[k/2]) {
t.Errorf("match %d: expected %q got %q: %s", n, expect, result, test)
if expect != string(got) {
t.Errorf("match %d: expected %q got %q: %s", n, expect, got, test)
return
}
}
......
......@@ -761,7 +761,7 @@ func (re *Regexp) Find(b []byte) []byte {
if a == nil {
return nil
}
return b[a[0]:a[1]]
return b[a[0]:a[1]:a[1]]
}
// FindIndex returns a two-element slice of integers defining the location of
......@@ -829,7 +829,7 @@ func (re *Regexp) FindSubmatch(b []byte) [][]byte {
ret := make([][]byte, 1+re.numSubexp)
for i := range ret {
if 2*i < len(a) && a[2*i] >= 0 {
ret[i] = b[a[2*i]:a[2*i+1]]
ret[i] = b[a[2*i]:a[2*i+1]:a[2*i+1]]
}
}
return ret
......@@ -1025,7 +1025,7 @@ func (re *Regexp) FindAll(b []byte, n int) [][]byte {
if result == nil {
result = make([][]byte, 0, startSize)
}
result = append(result, b[match[0]:match[1]])
result = append(result, b[match[0]:match[1]:match[1]])
})
return result
}
......@@ -1100,7 +1100,7 @@ func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte {
slice := make([][]byte, len(match)/2)
for j := range slice {
if match[2*j] >= 0 {
slice[j] = b[match[2*j]:match[2*j+1]]
slice[j] = b[match[2*j]:match[2*j+1]:match[2*j+1]]
}
}
result = append(result, slice)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment