| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| package regexp |
|
|
| import ( |
| "regexp/syntax" |
| "sync" |
| ) |
|
|
| |
| |
| type job struct { |
| pc uint32 |
| arg bool |
| pos int |
| } |
|
|
| const ( |
| visitedBits = 32 |
| maxBacktrackProg = 500 |
| maxBacktrackVector = 256 * 1024 |
| ) |
|
|
| |
| type bitState struct { |
| end int |
| cap []int |
| matchcap []int |
| jobs []job |
| visited []uint32 |
|
|
| inputs inputs |
| } |
|
|
| var bitStatePool sync.Pool |
|
|
| func newBitState() *bitState { |
| b, ok := bitStatePool.Get().(*bitState) |
| if !ok { |
| b = new(bitState) |
| } |
| return b |
| } |
|
|
| func freeBitState(b *bitState) { |
| b.inputs.clear() |
| bitStatePool.Put(b) |
| } |
|
|
| |
| |
| func maxBitStateLen(prog *syntax.Prog) int { |
| if !shouldBacktrack(prog) { |
| return 0 |
| } |
| return maxBacktrackVector / len(prog.Inst) |
| } |
|
|
| |
| |
| func shouldBacktrack(prog *syntax.Prog) bool { |
| return len(prog.Inst) <= maxBacktrackProg |
| } |
|
|
| |
| |
| |
| func (b *bitState) reset(prog *syntax.Prog, end int, ncap int) { |
| b.end = end |
|
|
| if cap(b.jobs) == 0 { |
| b.jobs = make([]job, 0, 256) |
| } else { |
| b.jobs = b.jobs[:0] |
| } |
|
|
| visitedSize := (len(prog.Inst)*(end+1) + visitedBits - 1) / visitedBits |
| if cap(b.visited) < visitedSize { |
| b.visited = make([]uint32, visitedSize, maxBacktrackVector/visitedBits) |
| } else { |
| b.visited = b.visited[:visitedSize] |
| clear(b.visited) |
| } |
|
|
| if cap(b.cap) < ncap { |
| b.cap = make([]int, ncap) |
| } else { |
| b.cap = b.cap[:ncap] |
| } |
| for i := range b.cap { |
| b.cap[i] = -1 |
| } |
|
|
| if cap(b.matchcap) < ncap { |
| b.matchcap = make([]int, ncap) |
| } else { |
| b.matchcap = b.matchcap[:ncap] |
| } |
| for i := range b.matchcap { |
| b.matchcap[i] = -1 |
| } |
| } |
|
|
| |
| |
| func (b *bitState) shouldVisit(pc uint32, pos int) bool { |
| n := uint(int(pc)*(b.end+1) + pos) |
| if b.visited[n/visitedBits]&(1<<(n&(visitedBits-1))) != 0 { |
| return false |
| } |
| b.visited[n/visitedBits] |= 1 << (n & (visitedBits - 1)) |
| return true |
| } |
|
|
| |
| |
| func (b *bitState) push(re *Regexp, pc uint32, pos int, arg bool) { |
| |
| |
| if re.prog.Inst[pc].Op != syntax.InstFail && (arg || b.shouldVisit(pc, pos)) { |
| b.jobs = append(b.jobs, job{pc: pc, arg: arg, pos: pos}) |
| } |
| } |
|
|
| |
| func (re *Regexp) tryBacktrack(b *bitState, i input, pc uint32, pos int) bool { |
| longest := re.longest |
|
|
| b.push(re, pc, pos, false) |
| for len(b.jobs) > 0 { |
| l := len(b.jobs) - 1 |
| |
| pc := b.jobs[l].pc |
| pos := b.jobs[l].pos |
| arg := b.jobs[l].arg |
| b.jobs = b.jobs[:l] |
|
|
| |
| |
| |
| |
| |
| |
| |
| goto Skip |
| CheckAndLoop: |
| if !b.shouldVisit(pc, pos) { |
| continue |
| } |
| Skip: |
|
|
| inst := &re.prog.Inst[pc] |
|
|
| switch inst.Op { |
| default: |
| panic("bad inst") |
| case syntax.InstFail: |
| panic("unexpected InstFail") |
| case syntax.InstAlt: |
| |
| |
| |
| |
| |
| |
| |
| |
| if arg { |
| |
| arg = false |
| pc = inst.Arg |
| goto CheckAndLoop |
| } else { |
| b.push(re, pc, pos, true) |
| pc = inst.Out |
| goto CheckAndLoop |
| } |
|
|
| case syntax.InstAltMatch: |
| |
| switch re.prog.Inst[inst.Out].Op { |
| case syntax.InstRune, syntax.InstRune1, syntax.InstRuneAny, syntax.InstRuneAnyNotNL: |
| |
| b.push(re, inst.Arg, pos, false) |
| pc = inst.Arg |
| pos = b.end |
| goto CheckAndLoop |
| } |
| |
| b.push(re, inst.Out, b.end, false) |
| pc = inst.Out |
| goto CheckAndLoop |
|
|
| case syntax.InstRune: |
| r, width := i.step(pos) |
| if !inst.MatchRune(r) { |
| continue |
| } |
| pos += width |
| pc = inst.Out |
| goto CheckAndLoop |
|
|
| case syntax.InstRune1: |
| r, width := i.step(pos) |
| if r != inst.Rune[0] { |
| continue |
| } |
| pos += width |
| pc = inst.Out |
| goto CheckAndLoop |
|
|
| case syntax.InstRuneAnyNotNL: |
| r, width := i.step(pos) |
| if r == '\n' || r == endOfText { |
| continue |
| } |
| pos += width |
| pc = inst.Out |
| goto CheckAndLoop |
|
|
| case syntax.InstRuneAny: |
| r, width := i.step(pos) |
| if r == endOfText { |
| continue |
| } |
| pos += width |
| pc = inst.Out |
| goto CheckAndLoop |
|
|
| case syntax.InstCapture: |
| if arg { |
| |
| b.cap[inst.Arg] = pos |
| continue |
| } else { |
| if inst.Arg < uint32(len(b.cap)) { |
| |
| b.push(re, pc, b.cap[inst.Arg], true) |
| b.cap[inst.Arg] = pos |
| } |
| pc = inst.Out |
| goto CheckAndLoop |
| } |
|
|
| case syntax.InstEmptyWidth: |
| flag := i.context(pos) |
| if !flag.match(syntax.EmptyOp(inst.Arg)) { |
| continue |
| } |
| pc = inst.Out |
| goto CheckAndLoop |
|
|
| case syntax.InstNop: |
| pc = inst.Out |
| goto CheckAndLoop |
|
|
| case syntax.InstMatch: |
| |
| |
| if len(b.cap) == 0 { |
| return true |
| } |
|
|
| |
| |
| |
| if len(b.cap) > 1 { |
| b.cap[1] = pos |
| } |
| if old := b.matchcap[1]; old == -1 || (longest && pos > 0 && pos > old) { |
| copy(b.matchcap, b.cap) |
| } |
|
|
| |
| if !longest { |
| return true |
| } |
|
|
| |
| if pos == b.end { |
| return true |
| } |
|
|
| |
| continue |
| } |
| } |
|
|
| return longest && len(b.matchcap) > 1 && b.matchcap[1] >= 0 |
| } |
|
|
| |
| func (re *Regexp) backtrack(ib []byte, is string, pos int, ncap int, dstCap []int) []int { |
| startCond := re.cond |
| if startCond == ^syntax.EmptyOp(0) { |
| return nil |
| } |
| if startCond&syntax.EmptyBeginText != 0 && pos != 0 { |
| |
| return nil |
| } |
|
|
| b := newBitState() |
| i, end := b.inputs.init(nil, ib, is) |
| b.reset(re.prog, end, ncap) |
|
|
| |
| if startCond&syntax.EmptyBeginText != 0 { |
| if len(b.cap) > 0 { |
| b.cap[0] = pos |
| } |
| if !re.tryBacktrack(b, i, uint32(re.prog.Start), pos) { |
| freeBitState(b) |
| return nil |
| } |
| } else { |
|
|
| |
| |
| |
| |
| |
| |
| width := -1 |
| for ; pos <= end && width != 0; pos += width { |
| if len(re.prefix) > 0 { |
| |
| advance := i.index(re, pos) |
| if advance < 0 { |
| freeBitState(b) |
| return nil |
| } |
| pos += advance |
| } |
|
|
| if len(b.cap) > 0 { |
| b.cap[0] = pos |
| } |
| if re.tryBacktrack(b, i, uint32(re.prog.Start), pos) { |
| |
| goto Match |
| } |
| _, width = i.step(pos) |
| } |
| freeBitState(b) |
| return nil |
| } |
|
|
| Match: |
| dstCap = append(dstCap, b.matchcap...) |
| freeBitState(b) |
| return dstCap |
| } |
|
|