1
0
mirror of https://github.com/golang/go synced 2024-11-25 05:57:57 -07:00

path: Fix bug in Match with non-greedy stars

path.Match() errors out when testing "*x" against "xxx"
because it matches the star non-greedily. Ensure that
the last chunk consumes the rest of the name.

R=r, rsc
CC=golang-dev
https://golang.org/cl/223050
This commit is contained in:
Kevin Ballard 2010-02-25 09:15:52 -08:00 committed by Russ Cox
parent 24ee7f799c
commit 20834d644f
2 changed files with 9 additions and 1 deletions

View File

@ -41,7 +41,10 @@ Pattern:
} }
// Look for match at current position. // Look for match at current position.
t, ok, err := matchChunk(chunk, name) t, ok, err := matchChunk(chunk, name)
if ok { // if we're the last chunk, make sure we've exhausted the name
// otherwise we'll give a false result even if we could still match
// using the star
if ok && (len(t) == 0 || len(pattern) > 0) {
name = t name = t
continue continue
} }
@ -54,6 +57,10 @@ Pattern:
for i := 0; i < len(name) && name[i] != '/'; i++ { for i := 0; i < len(name) && name[i] != '/'; i++ {
t, ok, err := matchChunk(chunk, name[i+1:]) t, ok, err := matchChunk(chunk, name[i+1:])
if ok { if ok {
// if we're the last chunk, make sure we exhausted the name
if len(pattern) == 0 && len(t) > 0 {
continue
}
name = t name = t
continue Pattern continue Pattern
} }

View File

@ -64,6 +64,7 @@ var matchTests = []MatchTest{
MatchTest{"[-x]", "a", false, ErrBadPattern}, MatchTest{"[-x]", "a", false, ErrBadPattern},
MatchTest{"\\", "a", false, ErrBadPattern}, MatchTest{"\\", "a", false, ErrBadPattern},
MatchTest{"[a-b-c]", "a", false, ErrBadPattern}, MatchTest{"[a-b-c]", "a", false, ErrBadPattern},
MatchTest{"*x", "xxx", true, nil},
} }
func TestMatch(t *testing.T) { func TestMatch(t *testing.T) {