]> Cypherpunks repositories - gostls13.git/commitdiff
regexp: fix prefix bug.
authorRob Pike <r@golang.org>
Mon, 3 Jan 2011 19:35:34 +0000 (11:35 -0800)
committerRob Pike <r@golang.org>
Mon, 3 Jan 2011 19:35:34 +0000 (11:35 -0800)
After a prefix match, the old code advanced the length of the
prefix.  This is incorrect since the full match might begin
in the middle of the prefix. (Consider "aaaab+" matching
"aaaaaab").

Fixes #1373

R=rsc
CC=golang-dev
https://golang.org/cl/3795044

src/pkg/regexp/find_test.go
src/pkg/regexp/regexp.go

index 34a7986731e6ee62678661463ec2ce3bb5578f20..1690711dd783da837847675c511b9f27c862675c 100644 (file)
@@ -78,6 +78,7 @@ var findTests = []FindTest{
        {`axxb$`, "axxcb", nil},
        {`data`, "daXY data", build(1, 5, 9)},
        {`da(.)a$`, "daXY data", build(1, 5, 9, 7, 8)},
+       {`zx+`, "zzx", build(1, 1, 3)},
 
        // can backslash-escape any punctuation
        {`\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~`,
index 4d13fad8b3c67df0ce292125a77505e6c6d8bbf9..be3ce2028ea7baa08c363579c9feb82795c1c04c 100644 (file)
@@ -758,7 +758,6 @@ func (re *Regexp) doExecute(str string, bytestr []byte, pos int) []int {
                return nil
        }
        // fast check for initial plain substring
-       prefixed := false // has this iteration begun by skipping a prefix?
        if re.prefix != "" {
                advance := 0
                if anchored {
@@ -781,8 +780,7 @@ func (re *Regexp) doExecute(str string, bytestr []byte, pos int) []int {
                if advance == -1 {
                        return nil
                }
-               pos += advance + len(re.prefix)
-               prefixed = true
+               pos += advance
        }
        arena := &matchArena{nil, 2 * (re.nbra + 1)}
        for pos <= end {
@@ -790,12 +788,7 @@ func (re *Regexp) doExecute(str string, bytestr []byte, pos int) []int {
                        // prime the pump if we haven't seen a match yet
                        match := arena.noMatch()
                        match.m[0] = pos
-                       if prefixed {
-                               s[out] = arena.addState(s[out], re.prefixStart, true, match, pos, end)
-                               prefixed = false // next iteration should start at beginning of machine.
-                       } else {
-                               s[out] = arena.addState(s[out], re.start.next, false, match, pos, end)
-                       }
+                       s[out] = arena.addState(s[out], re.start.next, false, match, pos, end)
                        arena.free(match) // if addState saved it, ref was incremented
                }
                in, out = out, in // old out state is new in state