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

Function _optimize_charset

Lib/re/_compiler.py:248–395  ·  view source on GitHub ↗
(charset, iscased=None, fixup=None, fixes=None)

Source from the content-addressed store, hash-verified

246 emit(FAILURE)
247
248def _optimize_charset(charset, iscased=None, fixup=None, fixes=None):
249 # internal: optimize character set
250 out = []
251 tail = []
252 charmap = bytearray(256)
253 hascased = False
254 for op, av in charset:
255 while True:
256 try:
257 if op is LITERAL:
258 if fixup: # IGNORECASE and not LOCALE
259 av = fixup(av)
260 charmap[av] = 1
261 if fixes and av in fixes:
262 for k in fixes[av]:
263 charmap[k] = 1
264 if not hascased and iscased(av):
265 hascased = True
266 else:
267 charmap[av] = 1
268 elif op is RANGE:
269 r = range(av[0], av[1]+1)
270 if fixup: # IGNORECASE and not LOCALE
271 if fixes:
272 for i in map(fixup, r):
273 charmap[i] = 1
274 if i in fixes:
275 for k in fixes[i]:
276 charmap[k] = 1
277 else:
278 for i in map(fixup, r):
279 charmap[i] = 1
280 if not hascased:
281 hascased = any(map(iscased, r))
282 else:
283 for i in r:
284 charmap[i] = 1
285 elif op is NEGATE:
286 out.append((op, av))
287 elif op is CATEGORY and tail and (CATEGORY, CH_NEGATE[av]) in tail:
288 # Optimize [\s\S] etc.
289 out = [] if out else _CHARSET_ALL
290 return out, False
291 else:
292 tail.append((op, av))
293 except IndexError:
294 if len(charmap) == 256:
295 # character set contains non-UCS1 character codes
296 charmap += b'\0' * 0xff00
297 continue
298 # Character set contains non-BMP character codes.
299 # For range, all BMP characters in the range are already
300 # proceeded.
301 if fixup: # IGNORECASE and not LOCALE
302 # For now, IN_UNI_IGNORE+LITERAL and
303 # IN_UNI_IGNORE+RANGE_UNI_IGNORE work for all non-BMP
304 # characters, because two characters (at least one of
305 # which is not in the BMP) match case-insensitively

Callers 2

_compileFunction · 0.85
_compile_infoFunction · 0.85

Calls 6

_mk_bitmapFunction · 0.85
_bytes_to_codesFunction · 0.85
take_bytesMethod · 0.80
anyFunction · 0.50
appendMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…