Coverage for archdocs/prefilter.py: 100%

2 statements  

« prev     ^ index     » next       coverage.py v7.15.3, created at 2026-08-03 00:00 +0000

1"""Cheap rejection of sources before any regex runs. 

2 

3`str.__contains__` scans with memchr while `re` walks its own bytecode on a virtual 

4machine, so on a miss a substring costs tens of times less than a pattern. Every parser 

5misses on the overwhelming majority of a project's files — celery is mentioned in a module 

6or two and nowhere else — so the miss is what to optimise: first the cheap "is the word in 

7this file at all", and only then the expensive "is it in the right context". 

8 

9Literals have to be a necessary condition for the pattern to match, covering every one of 

10its alternatives: a superfluous literal costs one wasted regex run, a missing one silently 

11costs a feature on the diagram. The source arrives already lowercased because the patterns 

12are compiled with `IGNORECASE`, and one `lower()` per file is enough for all of them. 

13""" 

14 

15 

16def contains_any_literal(lowered_source: str, every_literal: tuple[str, ...], /) -> bool: 

17 return any(one_literal in lowered_source for one_literal in every_literal)