]> Cypherpunks repositories - gostls13.git/commitdiff
fix bug in prefix code: must stop one character before any potential match of an...
authorRob Pike <r@golang.org>
Mon, 23 Nov 2009 22:06:21 +0000 (14:06 -0800)
committerRob Pike <r@golang.org>
Mon, 23 Nov 2009 22:06:21 +0000 (14:06 -0800)
Fixes #308.

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

src/pkg/regexp/all_test.go
src/pkg/regexp/regexp.go

index fb6f3a03019c744f44a027942709645aeed39d10..6c586ba984ceba7f6e6f32a9c264b6bcf0284afa 100644 (file)
@@ -95,6 +95,8 @@ var matches = []tester{
        tester{`a*(|(b))c*`, "aacc", vec{0, 4, 2, 2, -1, -1}},
        tester{`(.*).*`, "ab", vec{0, 2, 0, 2}},
        tester{`[.]`, ".", vec{0, 1}},
+       tester{`/$`, "/abc/", vec{4, 5}},
+       tester{`/$`, "/abc", vec{}},
 }
 
 func compileTest(t *testing.T, expr string, error os.Error) *Regexp {
index 8f17954d7918192adec053c05b778fd73482cc5d..014a9fdc7ae75b6717232f92f2fc29d159c31428 100644 (file)
@@ -633,15 +633,18 @@ func (re *Regexp) setPrefix() {
        var utf = make([]byte, utf8.UTFMax);
        // First instruction is start; skip that.
        i := re.inst.At(0).(instr).next().index();
+Loop:
        for i < re.inst.Len() {
                inst := re.inst.At(i).(instr);
                // stop if this is not a char
                if inst.kind() != _CHAR {
                        break
                }
-               // stop if this char starts a closure; liberal but easy test: is an ALT next?
-               if re.inst.At(inst.next().index()).(instr).kind() == _ALT {
-                       break
+               // stop if this char can be followed by a match for an empty string,
+               // which includes closures, ^, and $.
+               switch re.inst.At(inst.next().index()).(instr).kind() {
+               case _BOT, _EOT, _ALT:
+                       break Loop
                }
                n := utf8.EncodeRune(inst.(*_Char).char, utf);
                b = bytes.Add(b, utf[0:n]);