Source File
backtrack.go
Belonging Package
regexp
package regexp
import (
)
type job struct {
pc uint32
arg bool
pos int
}
const (
visitedBits = 32
maxBacktrackProg = 500 // len(prog.Inst) <= max
maxBacktrackVector = 256 * 1024 // bit vector size <= max (bits)
)
func ( *syntax.Prog) int {
if !shouldBacktrack() {
return 0
}
return maxBacktrackVector / len(.Inst)
}
func ( *bitState) ( *syntax.Prog, int, int) {
.end =
if cap(.jobs) == 0 {
.jobs = make([]job, 0, 256)
} else {
.jobs = .jobs[:0]
}
:= (len(.Inst)*(+1) + visitedBits - 1) / visitedBits
if cap(.visited) < {
.visited = make([]uint32, , maxBacktrackVector/visitedBits)
} else {
.visited = .visited[:]
for := range .visited {
.visited[] = 0
}
}
if cap(.cap) < {
.cap = make([]int, )
} else {
.cap = .cap[:]
}
for := range .cap {
.cap[] = -1
}
if cap(.matchcap) < {
.matchcap = make([]int, )
} else {
.matchcap = .matchcap[:]
}
for := range .matchcap {
.matchcap[] = -1
}
}
func ( *bitState) ( uint32, int) bool {
:= uint(int()*(.end+1) + )
if .visited[/visitedBits]&(1<<(&(visitedBits-1))) != 0 {
return false
}
.visited[/visitedBits] |= 1 << ( & (visitedBits - 1))
return true
}
.push(, .Out, .end, false)
= .Out
goto
case syntax.InstRune:
, := .step()
if !.MatchRune() {
continue
}
+=
= .Out
goto
case syntax.InstRune1:
, := .step()
if != .Rune[0] {
continue
}
+=
= .Out
goto
case syntax.InstRuneAnyNotNL:
, := .step()
if == '\n' || == endOfText {
continue
}
+=
= .Out
goto
case syntax.InstRuneAny:
, := .step()
if == endOfText {
continue
}
+=
= .Out
goto
case syntax.InstCapture:
if ! {
return true
}
if &syntax.EmptyBeginText != 0 {
if len(.cap) > 0 {
.cap[0] =
}
if !.tryBacktrack(, , uint32(.prog.Start), ) {
freeBitState()
return nil
}
} else {
:= -1
for ; <= && != 0; += {
goto
}
_, = .step()
}
freeBitState()
return nil
}
:
= append(, .matchcap...)
freeBitState()
return
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |