Debian Codegraph @

backtrack.go

raw symbols SCIP
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // backtrack is a regular expression search with submatch
6 // tracking for small regular expressions and texts. It allocates
7 // a bit vector with (length of input) * (length of prog) bits,
8 // to make sure it never explores the same (character position, instruction)
9 // state multiple times. This limits the search to run in time linear in
10 // the length of the test.
11 //
12 // backtrack is a fast replacement for the NFA code on small
13 // regexps when onepass cannot be used.
14
15 package regexpscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/
16
17 import (
18 "regexp/syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/"
19 "syncscip-go gomod github.com/golang/go/src go1.21 sync/"
20 )
21
22 // A job is an entry on the backtracker's job stack. It holds
23 // the instruction pc and the position in the input.
24 type jobscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/job# struct {
25 pcscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/job#pc. uint32
26 argscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/job#arg. bool
27 posscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/job#pos. int
28 }
29
30 const (
31 visitedBitsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/visitedBits. = 32
32 maxBacktrackProgscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/maxBacktrackProg. = 500 // len(prog.Inst) <= max
33 maxBacktrackVectorscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/maxBacktrackVector. = 256 * 1024 // bit vector size <= max (bits)
34 )
35
36 // bitState holds state for the backtracker.
37 type bitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState# struct {
38 endscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#end. int
39 capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap. []int
40 matchcapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#matchcap. []int
41 jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs. []jobscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/job#
42 visitedscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#visited. []uint32
43
44 inputsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#inputs. inputsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/inputs#
45 }
46
47 var bitStatePoolscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitStatePool. syncscip-go gomod github.com/golang/go/src go1.21 sync/.Poolscip-go gomod github.com/golang/go/src go1.21 sync/Pool#
48
49 func newBitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/newBitState().() *bitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState# {
50 b, ok := bitStatePoolscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitStatePool..Getscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Get().().(*bitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#)
51 if !ok {
52 b = new(bitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#)
53 }
54 return b
55 }
56
57 func freeBitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/freeBitState().(b *bitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#) {
58 b.inputsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#inputs..clearscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/inputs#clear().()
59 bitStatePoolscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitStatePool..Putscip-go gomod github.com/golang/go/src go1.21 sync/Pool#Put().(b)
60 }
61
62 // maxBitStateLen returns the maximum length of a string to search with
63 // the backtracker using prog.
64 func maxBitStateLenscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/maxBitStateLen().(prog *syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.Progscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#) int {
65 if !shouldBacktrackscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/shouldBacktrack().(prog) {
66 return 0
67 }
68 return maxBacktrackVectorscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/maxBacktrackVector. / len(prog.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.)
69 }
70
71 // shouldBacktrack reports whether the program is too
72 // long for the backtracker to run.
73 func shouldBacktrackscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/shouldBacktrack().(prog *syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.Progscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#) bool {
74 return len(prog.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.) <= maxBacktrackProgscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/maxBacktrackProg.
75 }
76
77 // reset resets the state of the backtracker.
78 // end is the end position in the input.
79 // ncap is the number of captures.
80 func (b *bitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#) resetscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#reset().(prog *syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.Progscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#, end int, ncap int) {
81 b.endscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#end. = end
82
83 if cap(b.jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs.) == 0 {
84 b.jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs. = make([]jobscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/job#, 0, 256)
85 } else {
86 b.jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs. = b.jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs.[:0]
87 }
88
89 visitedSize := (len(prog.Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.)*(end+1) + visitedBitsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/visitedBits. - 1) / visitedBitsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/visitedBits.
90 if cap(b.visitedscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#visited.) < visitedSize {
91 b.visitedscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#visited. = make([]uint32, visitedSize, maxBacktrackVectorscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/maxBacktrackVector./visitedBitsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/visitedBits.)
92 } else {
93 b.visitedscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#visited. = b.visitedscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#visited.[:visitedSize]
94 clear(b.visitedscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#visited.) // set to 0
95 }
96
97 if cap(b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.) < ncap {
98 b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap. = make([]int, ncap)
99 } else {
100 b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap. = b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.[:ncap]
101 }
102 for i := range b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap. {
103 b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.[i] = -1
104 }
105
106 if cap(b.matchcapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#matchcap.) < ncap {
107 b.matchcapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#matchcap. = make([]int, ncap)
108 } else {
109 b.matchcapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#matchcap. = b.matchcapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#matchcap.[:ncap]
110 }
111 for i := range b.matchcapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#matchcap. {
112 b.matchcapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#matchcap.[i] = -1
113 }
114 }
115
116 // shouldVisit reports whether the combination of (pc, pos) has not
117 // been visited yet.
118 func (b *bitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#) shouldVisitscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#shouldVisit().(pc uint32, pos int) bool {
119 n := uint(int(pc)*(b.endscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#end.+1) + pos)
120 if b.visitedscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#visited.[n/visitedBitsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/visitedBits.]&(1<<(n&(visitedBitsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/visitedBits.-1))) != 0 {
121 return false
122 }
123 b.visitedscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#visited.[n/visitedBitsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/visitedBits.] |= 1 << (n & (visitedBitsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/visitedBits. - 1))
124 return true
125 }
126
127 // push pushes (pc, pos, arg) onto the job stack if it should be
128 // visited.
129 func (b *bitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#) pushscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#push().(re *Regexpscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#, pc uint32, pos int, arg bool) {
130 // Only check shouldVisit when arg is false.
131 // When arg is true, we are continuing a previous visit.
132 if re.progscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#prog..Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.[pc].Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. != syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstFailscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstFail. && (arg || b.shouldVisitscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#shouldVisit().(pc, pos)) {
133 b.jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs. = append(b.jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs., jobscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/job#{pcscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/job#pc.: pc, argscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/job#arg.: arg, posscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/job#pos.: pos})
134 }
135 }
136
137 // tryBacktrack runs a backtracking search starting at pos.
138 func (re *Regexpscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#) tryBacktrackscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#tryBacktrack().(b *bitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#, i inputscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/input#, pc uint32, pos int) bool {
139 longest := re.longestscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#longest.
140
141 b.pushscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#push().(re, pc, pos, false)
142 for len(b.jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs.) > 0 {
143 l := len(b.jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs.) - 1
144 // Pop job off the stack.
145 pc := b.jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs.[l].pcscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/job#pc.
146 pos := b.jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs.[l].posscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/job#pos.
147 arg := b.jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs.[l].argscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/job#arg.
148 b.jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs. = b.jobsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#jobs.[:l]
149
150 // Optimization: rather than push and pop,
151 // code that is going to Push and continue
152 // the loop simply updates ip, p, and arg
153 // and jumps to CheckAndLoop. We have to
154 // do the ShouldVisit check that Push
155 // would have, but we avoid the stack
156 // manipulation.
157 goto Skip
158 CheckAndLoop:
159 if !b.shouldVisitscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#shouldVisit().(pc, pos) {
160 continue
161 }
162 Skip:
163
164 inst := &re.progscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#prog..Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.[pc]
165
166 switch inst.Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. {
167 default:
168 panic("bad inst")
169 case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstFailscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstFail.:
170 panic("unexpected InstFail")
171 case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAlt.:
172 // Cannot just
173 // b.push(inst.Out, pos, false)
174 // b.push(inst.Arg, pos, false)
175 // If during the processing of inst.Out, we encounter
176 // inst.Arg via another path, we want to process it then.
177 // Pushing it here will inhibit that. Instead, re-push
178 // inst with arg==true as a reminder to push inst.Arg out
179 // later.
180 if arg {
181 // Finished inst.Out; try inst.Arg.
182 arg = false
183 pc = inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.
184 goto CheckAndLoop
185 } else {
186 b.pushscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#push().(re, pc, pos, true)
187 pc = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.
188 goto CheckAndLoop
189 }
190
191 case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstAltMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstAltMatch.:
192 // One opcode consumes runes; the other leads to match.
193 switch re.progscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#prog..Instscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Inst.[inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.].Opscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Op. {
194 case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRunescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRune1scip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune1., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRuneAnyscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRuneAny., syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRuneAnyNotNLscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRuneAnyNotNL.:
195 // inst.Arg is the match.
196 b.pushscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#push().(re, inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg., pos, false)
197 pc = inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.
198 pos = b.endscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#end.
199 goto CheckAndLoop
200 }
201 // inst.Out is the match - non-greedy
202 b.pushscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#push().(re, inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out., b.endscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#end., false)
203 pc = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.
204 goto CheckAndLoop
205
206 case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRunescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune.:
207 r, width := i.stepscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/input#step.(pos)
208 if !inst.MatchRunescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#MatchRune().(r) {
209 continue
210 }
211 pos += width
212 pc = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.
213 goto CheckAndLoop
214
215 case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRune1scip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRune1.:
216 r, width := i.stepscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/input#step.(pos)
217 if r != inst.Runescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Rune.[0] {
218 continue
219 }
220 pos += width
221 pc = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.
222 goto CheckAndLoop
223
224 case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRuneAnyNotNLscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRuneAnyNotNL.:
225 r, width := i.stepscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/input#step.(pos)
226 if r == '\n' || r == endOfTextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/endOfText. {
227 continue
228 }
229 pos += width
230 pc = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.
231 goto CheckAndLoop
232
233 case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstRuneAnyscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstRuneAny.:
234 r, width := i.stepscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/input#step.(pos)
235 if r == endOfTextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/endOfText. {
236 continue
237 }
238 pos += width
239 pc = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.
240 goto CheckAndLoop
241
242 case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstCapturescip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstCapture.:
243 if arg {
244 // Finished inst.Out; restore the old value.
245 b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.[inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.] = pos
246 continue
247 } else {
248 if inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg. < uint32(len(b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.)) {
249 // Capture pos to register, but save old value.
250 b.pushscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#push().(re, pc, b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.[inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.], true) // come back when we're done.
251 b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.[inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.] = pos
252 }
253 pc = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.
254 goto CheckAndLoop
255 }
256
257 case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstEmptyWidthscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstEmptyWidth.:
258 flag := i.contextscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/input#context.(pos)
259 if !flag.matchscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/lazyFlag#match().(syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyOpscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyOp#(inst.Argscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Arg.)) {
260 continue
261 }
262 pc = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.
263 goto CheckAndLoop
264
265 case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstNopscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstNop.:
266 pc = inst.Outscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Inst#Out.
267 goto CheckAndLoop
268
269 case syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.InstMatchscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/InstMatch.:
270 // We found a match. If the caller doesn't care
271 // where the match is, no point going further.
272 if len(b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.) == 0 {
273 return true
274 }
275
276 // Record best match so far.
277 // Only need to check end point, because this entire
278 // call is only considering one start position.
279 if len(b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.) > 1 {
280 b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.[1] = pos
281 }
282 if old := b.matchcapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#matchcap.[1]; old == -1 || (longest && pos > 0 && pos > old) {
283 copy(b.matchcapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#matchcap., b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.)
284 }
285
286 // If going for first match, we're done.
287 if !longest {
288 return true
289 }
290
291 // If we used the entire text, no longer match is possible.
292 if pos == b.endscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#end. {
293 return true
294 }
295
296 // Otherwise, continue on in hope of a longer match.
297 continue
298 }
299 }
300
301 return longest && len(b.matchcapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#matchcap.) > 1 && b.matchcapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#matchcap.[1] >= 0
302 }
303
304 // backtrack runs a backtracking search of prog on the input starting at pos.
305 func (re *Regexpscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#) backtrackscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#backtrack().(ib []byte, is string, pos int, ncap int, dstCap []int) []int {
306 startCond := re.condscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#cond.
307 if startCond == ^syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyOpscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyOp#(0) { // impossible
308 return nil
309 }
310 if startCond&syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyBeginTextscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyBeginText. != 0 && pos != 0 {
311 // Anchored match, past beginning of text.
312 return nil
313 }
314
315 b := newBitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/newBitState().()
316 i, end := b.inputsscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#inputs..initscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/inputs#init().(nil, ib, is)
317 b.resetscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#reset().(re.progscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#prog., end, ncap)
318
319 // Anchored search must start at the beginning of the input
320 if startCond&syntaxscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/.EmptyBeginTextscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/EmptyBeginText. != 0 {
321 if len(b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.) > 0 {
322 b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.[0] = pos
323 }
324 if !re.tryBacktrackscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#tryBacktrack().(b, i, uint32(re.progscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#prog..Startscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Start.), pos) {
325 freeBitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/freeBitState().(b)
326 return nil
327 }
328 } else {
329
330 // Unanchored search, starting from each possible text position.
331 // Notice that we have to try the empty string at the end of
332 // the text, so the loop condition is pos <= end, not pos < end.
333 // This looks like it's quadratic in the size of the text,
334 // but we are not clearing visited between calls to TrySearch,
335 // so no work is duplicated and it ends up still being linear.
336 width := -1
337 for ; pos <= end && width != 0; pos += width {
338 if len(re.prefixscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#prefix.) > 0 {
339 // Match requires literal prefix; fast search for it.
340 advance := i.indexscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/input#index.(re, pos)
341 if advance < 0 {
342 freeBitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/freeBitState().(b)
343 return nil
344 }
345 pos += advance
346 }
347
348 if len(b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.) > 0 {
349 b.capscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#cap.[0] = pos
350 }
351 if re.tryBacktrackscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#tryBacktrack().(b, i, uint32(re.progscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/Regexp#prog..Startscip-go gomod github.com/golang/go/src go1.21 `regexp/syntax`/Prog#Start.), pos) {
352 // Match must be leftmost; done.
353 goto Match
354 }
355 _, width = i.stepscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/input#step.(pos)
356 }
357 freeBitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/freeBitState().(b)
358 return nil
359 }
360
361 Match:
362 dstCap = append(dstCap, b.matchcapscip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/bitState#matchcap....)
363 freeBitStatescip-go gomod github.com/grafana/regexp . `github.com/grafana/regexp`/freeBitState().(b)
364 return dstCap
365 }