diff options
author | Rob Pike <r@golang.org> | 2010-12-14 12:01:35 -0800 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2010-12-14 12:01:35 -0800 |
commit | da1cbe5d11b60ea6e640f7ff3c5e3fff1a642b61 (patch) | |
tree | b088f9073b78c51323e92bda0cbd646214af9331 | |
parent | f620a430f270abe794d0dcfb7597a28ec649f51c (diff) | |
download | go-da1cbe5d11b60ea6e640f7ff3c5e3fff1a642b61.tar.gz go-da1cbe5d11b60ea6e640f7ff3c5e3fff1a642b61.zip |
regexp: simplify code for brackets, per rsc suggestion
R=rsc
CC=golang-dev
https://golang.org/cl/3545044
-rw-r--r-- | src/pkg/regexp/regexp.go | 26 |
1 files changed, 9 insertions, 17 deletions
diff --git a/src/pkg/regexp/regexp.go b/src/pkg/regexp/regexp.go index 1cc48a5394..1728c7ec26 100644 --- a/src/pkg/regexp/regexp.go +++ b/src/pkg/regexp/regexp.go @@ -98,8 +98,7 @@ const ( iCharClass // [a-z] character class iAny // '.' any character including newline iNotNL // [^\n] special case: any character but newline - iBra // '(' parenthesized expression - iEbra // ')'; end of '(' parenthesized expression + iBra // '(' parenthesized expression: 2*braNum for left, 2*braNum+1 for right iAlt // '|' alternation iNop // do nothing; makes it easy to link without patching ) @@ -135,9 +134,11 @@ func (i *instr) print() { case iNotNL: print("notnl") case iBra: - print("bra", i.braNum) - case iEbra: - print("ebra", i.braNum) + if i.braNum&1 == 0 { + print("bra", i.braNum/2) + } else { + print("ebra", i.braNum/2) + } case iAlt: print("alt(", i.left.index, ")") case iNop: @@ -391,12 +392,10 @@ func (p *parser) term() (start, end *instr) { } p.nlpar-- p.nextc() - bra := &instr{kind: iBra} + bra := &instr{kind: iBra, braNum: 2 * nbra} p.re.add(bra) - ebra := &instr{kind: iEbra} + ebra := &instr{kind: iBra, braNum: 2*nbra + 1} p.re.add(ebra) - bra.braNum = nbra - ebra.braNum = nbra if start == nil { if end == nil { p.error(ErrInternal) @@ -709,13 +708,7 @@ func (a *matchArena) addState(s []state, inst *instr, prefixed bool, match *matc } return s case iBra: - n := inst.braNum - 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 + match.m[inst.braNum] = pos s = a.addState(s, inst.next, prefixed, match, pos, end) 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) } case iBra: - case iEbra: case iAlt: case iEnd: // choose leftmost longest |