MCPcopy Create free account
hub / github.com/dlclark/regexp2 / captureSlotsInUse

Function captureSlotsInUse

syntax/code.go:123–150  ·  view source on GitHub ↗

captureSlotsInUse returns the capture slots whose values can affect matching. Group 0 is always retained as the success marker. Ordinary captures that are never referenced by the pattern may be omitted by bool-only matching APIs.

(codes []int, capsize int)

Source from the content-addressed store, hash-verified

121// Group 0 is always retained as the success marker. Ordinary captures that are
122// never referenced by the pattern may be omitted by bool-only matching APIs.
123func captureSlotsInUse(codes []int, capsize int) []bool {
124 inUse := make([]bool, capsize)
125 if capsize > 0 {
126 inUse[0] = true
127 }
128 for pos := 0; pos < len(codes); {
129 op := InstOp(codes[pos]) & Mask
130 switch op {
131 case Ref, Testref:
132 capnum := codes[pos+1]
133 if capnum >= 0 && capnum < len(inUse) {
134 inUse[capnum] = true
135 }
136 case Capturemark:
137 // Balancing groups both observe and mutate capture state. Keep both
138 // sides live even if no later backreference refers to them.
139 if codes[pos+2] != -1 {
140 for _, capnum := range codes[pos+1 : pos+3] {
141 if capnum >= 0 && capnum < len(inUse) {
142 inUse[capnum] = true
143 }
144 }
145 }
146 }
147 pos += opcodeSize(op)
148 }
149 return inUse
150}
151
152// PrepareCharSetASCIIBitmaps builds bounded ASCII lookup tables for compiled
153// character classes before the regexp is shared across goroutines.

Callers 1

codeFromTreeMethod · 0.85

Calls 2

InstOpTypeAlias · 0.85
opcodeSizeFunction · 0.85

Tested by

no test coverage detected