1
0
mirror of https://github.com/golang/go synced 2024-11-21 18:24:46 -07:00

regexp: simplify code for brackets, per rsc suggestion

R=rsc
CC=golang-dev
https://golang.org/cl/3545044
This commit is contained in:
Rob Pike 2010-12-14 12:01:35 -08:00
parent f620a430f2
commit da1cbe5d11

View File

@ -98,8 +98,7 @@ const (
iCharClass // [a-z] character class iCharClass // [a-z] character class
iAny // '.' any character including newline iAny // '.' any character including newline
iNotNL // [^\n] special case: any character but newline iNotNL // [^\n] special case: any character but newline
iBra // '(' parenthesized expression iBra // '(' parenthesized expression: 2*braNum for left, 2*braNum+1 for right
iEbra // ')'; end of '(' parenthesized expression
iAlt // '|' alternation iAlt // '|' alternation
iNop // do nothing; makes it easy to link without patching iNop // do nothing; makes it easy to link without patching
) )
@ -135,9 +134,11 @@ func (i *instr) print() {
case iNotNL: case iNotNL:
print("notnl") print("notnl")
case iBra: case iBra:
print("bra", i.braNum) if i.braNum&1 == 0 {
case iEbra: print("bra", i.braNum/2)
print("ebra", i.braNum) } else {
print("ebra", i.braNum/2)
}
case iAlt: case iAlt:
print("alt(", i.left.index, ")") print("alt(", i.left.index, ")")
case iNop: case iNop:
@ -391,12 +392,10 @@ func (p *parser) term() (start, end *instr) {
} }
p.nlpar-- p.nlpar--
p.nextc() p.nextc()
bra := &instr{kind: iBra} bra := &instr{kind: iBra, braNum: 2 * nbra}
p.re.add(bra) p.re.add(bra)
ebra := &instr{kind: iEbra} ebra := &instr{kind: iBra, braNum: 2*nbra + 1}
p.re.add(ebra) p.re.add(ebra)
bra.braNum = nbra
ebra.braNum = nbra
if start == nil { if start == nil {
if end == nil { if end == nil {
p.error(ErrInternal) p.error(ErrInternal)
@ -709,13 +708,7 @@ func (a *matchArena) addState(s []state, inst *instr, prefixed bool, match *matc
} }
return s return s
case iBra: case iBra:
n := inst.braNum match.m[inst.braNum] = pos
match.m[2*n] = pos
s = a.addState(s, inst.next, prefixed, match, pos, end)
return s
case iEbra:
n := inst.braNum
match.m[2*n+1] = pos
s = a.addState(s, inst.next, prefixed, match, pos, end) s = a.addState(s, inst.next, prefixed, match, pos, end)
return s return s
} }
@ -821,7 +814,6 @@ func (re *Regexp) doExecute(str string, bytestr []byte, pos int) []int {
s[out] = arena.addState(s[out], st.inst.next, st.prefixed, st.match, pos, end) s[out] = arena.addState(s[out], st.inst.next, st.prefixed, st.match, pos, end)
} }
case iBra: case iBra:
case iEbra:
case iAlt: case iAlt:
case iEnd: case iEnd:
// choose leftmost longest // choose leftmost longest