MCPcopy Index your code
hub / github.com/python/cpython / getwidth

Method getwidth

Lib/re/_parser.py:178–228  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

176 def append(self, code):
177 self.data.append(code)
178 def getwidth(self):
179 # determine the width (min, max) for this subpattern
180 if self.width is not None:
181 return self.width
182 lo = hi = 0
183 for op, av in self.data:
184 if op is BRANCH:
185 i = MAXWIDTH
186 j = 0
187 for av in av[1]:
188 l, h = av.getwidth()
189 i = min(i, l)
190 j = max(j, h)
191 lo = lo + i
192 hi = hi + j
193 elif op is ATOMIC_GROUP:
194 i, j = av.getwidth()
195 lo = lo + i
196 hi = hi + j
197 elif op is SUBPATTERN:
198 i, j = av[-1].getwidth()
199 lo = lo + i
200 hi = hi + j
201 elif op in _REPEATCODES:
202 i, j = av[2].getwidth()
203 lo = lo + i * av[0]
204 if av[1] == MAXREPEAT and j:
205 hi = MAXWIDTH
206 else:
207 hi = hi + j * av[1]
208 elif op in _UNITCODES:
209 lo = lo + 1
210 hi = hi + 1
211 elif op is GROUPREF:
212 i, j = self.state.groupwidths[av]
213 lo = lo + i
214 hi = hi + j
215 elif op is GROUPREF_EXISTS:
216 i, j = av[1].getwidth()
217 if av[2] is not None:
218 l, h = av[2].getwidth()
219 i = min(i, l)
220 j = max(j, h)
221 else:
222 i = 0
223 lo = lo + i
224 hi = hi + j
225 elif op is SUCCESS:
226 break
227 self.width = min(lo, MAXWIDTH), min(hi, MAXWIDTH)
228 return self.width
229
230class Tokenizer:
231 def __init__(self, string):

Callers 3

_compileFunction · 0.80
_compile_infoFunction · 0.80
closegroupMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected