Skip to content

Commit 6ce55f1

Browse files
committed
regex: patterns with no alternates are one-pass
Check whether a regex has any 'alt' instructions before rejecting it.
1 parent 07a7c6f commit 6ce55f1

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/regexp/onepass.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,19 @@ func compileOnePass(prog *syntax.Prog) (p *onePassProg) {
471471
syntax.EmptyOp(prog.Inst[prog.Start].Arg)&syntax.EmptyBeginText != syntax.EmptyBeginText {
472472
return nil
473473
}
474-
// every instruction leading to InstMatch must be EmptyEndText
474+
hasAlt := false
475+
for _, inst := range prog.Inst {
476+
if inst.Op == syntax.InstAlt || inst.Op == syntax.InstAltMatch {
477+
hasAlt = true
478+
}
479+
}
480+
// If we have alternates, every instruction leading to InstMatch must be EmptyEndText.
481+
// Also, any match on empty text must be $.
475482
for _, inst := range prog.Inst {
476483
opOut := prog.Inst[inst.Out].Op
477484
switch inst.Op {
478485
default:
479-
if opOut == syntax.InstMatch {
486+
if opOut == syntax.InstMatch && hasAlt {
480487
return nil
481488
}
482489
case syntax.InstAlt, syntax.InstAltMatch:

0 commit comments

Comments
 (0)