@@ -36,7 +36,6 @@ type bitState struct {
36
36
37
37
end int
38
38
cap []int
39
- reqcap bool // whether any captures are requested
40
39
input input
41
40
jobs []job
42
41
visited []uint32
@@ -72,12 +71,10 @@ func shouldBacktrack(prog *syntax.Prog) bool {
72
71
}
73
72
74
73
// reset resets the state of the backtracker.
75
- // end is the end position in the input. ncap and reqcap are the number
76
- // of the machine's capture registers and the number of user-requested
77
- // captures respectively.
78
- func (b * bitState ) reset (end int , ncap int , reqcap int ) {
74
+ // end is the end position in the input.
75
+ // ncap is the number of captures.
76
+ func (b * bitState ) reset (end int , ncap int ) {
79
77
b .end = end
80
- b .reqcap = reqcap > 0
81
78
82
79
if cap (b .jobs ) == 0 {
83
80
b .jobs = make ([]job , 0 , 256 )
@@ -95,8 +92,10 @@ func (b *bitState) reset(end int, ncap int, reqcap int) {
95
92
}
96
93
}
97
94
98
- if len (b .cap ) < ncap {
95
+ if cap (b .cap ) < ncap {
99
96
b .cap = make ([]int , ncap )
97
+ } else {
98
+ b .cap = b .cap [:ncap ]
100
99
}
101
100
for i := range b .cap {
102
101
b .cap [i ] = - 1
@@ -271,15 +270,17 @@ func (m *machine) tryBacktrack(b *bitState, i input, pc uint32, pos int) bool {
271
270
case syntax .InstMatch :
272
271
// We found a match. If the caller doesn't care
273
272
// where the match is, no point going further.
274
- if ! b . reqcap {
273
+ if len ( b . cap ) == 0 {
275
274
m .matched = true
276
275
return m .matched
277
276
}
278
277
279
278
// Record best match so far.
280
279
// Only need to check end point, because this entire
281
280
// call is only considering one start position.
282
- b .cap [1 ] = pos
281
+ if len (b .cap ) > 1 {
282
+ b .cap [1 ] = pos
283
+ }
283
284
if ! m .matched || (longest && pos > 0 && pos > m .matchcap [1 ]) {
284
285
copy (m .matchcap , b .cap )
285
286
}
@@ -305,7 +306,7 @@ func (m *machine) tryBacktrack(b *bitState, i input, pc uint32, pos int) bool {
305
306
}
306
307
307
308
// backtrack runs a backtracking search of prog on the input starting at pos.
308
- func (m * machine ) backtrack (i input , pos int , end int , reqcap int ) bool {
309
+ func (m * machine ) backtrack (i input , pos int , end int , ncap int ) bool {
309
310
if ! i .canCheckPrefix () {
310
311
panic ("backtrack called for a RuneReader" )
311
312
}
@@ -320,15 +321,18 @@ func (m *machine) backtrack(i input, pos int, end int, reqcap int) bool {
320
321
}
321
322
322
323
b := m .b
323
- b .reset (end , len ( m . matchcap ), reqcap )
324
+ b .reset (end , ncap )
324
325
326
+ m .matchcap = m .matchcap [:ncap ]
325
327
for i := range m .matchcap {
326
328
m .matchcap [i ] = - 1
327
329
}
328
330
329
331
// Anchored search must start at the beginning of the input
330
332
if startCond & syntax .EmptyBeginText != 0 {
331
- b .cap [0 ] = pos
333
+ if len (b .cap ) > 0 {
334
+ b .cap [0 ] = pos
335
+ }
332
336
return m .tryBacktrack (b , i , uint32 (m .p .Start ), pos )
333
337
}
334
338
@@ -349,7 +353,9 @@ func (m *machine) backtrack(i input, pos int, end int, reqcap int) bool {
349
353
pos += advance
350
354
}
351
355
352
- b .cap [0 ] = pos
356
+ if len (b .cap ) > 0 {
357
+ b .cap [0 ] = pos
358
+ }
353
359
if m .tryBacktrack (b , i , uint32 (m .p .Start ), pos ) {
354
360
// Match must be leftmost; done.
355
361
return true
0 commit comments