Return (code_without_comments, updated_in_block). Removes // line comments and /* ... */ block comments (non-nesting, C-style).
(line: str, in_block: bool)
| 24 | """ |
| 25 | |
| 26 | def strip_comments(line: str, in_block: bool) -> tuple[str, bool]: |
| 27 | """ |
| 28 | Return (code_without_comments, updated_in_block). |
| 29 | Removes // line comments and /* ... */ block comments (non-nesting, C-style). |
| 30 | """ |
| 31 | i = 0 |
| 32 | out_parts: list[str] = [] |
| 33 | n = len(line) |
| 34 | |
| 35 | while i < n: |
| 36 | if in_block: |
| 37 | end = line.find("*/", i) |
| 38 | if end == -1: |
| 39 | # Entire remainder is inside a block comment. |
| 40 | return ("".join(out_parts), True) |
| 41 | i = end + 2 |
| 42 | in_block = False |
| 43 | continue |
| 44 | |
| 45 | # Not in block: look for next // or /* from current i |
| 46 | sl = line.find("//", i) |
| 47 | bl = line.find("/*", i) |
| 48 | |
| 49 | if sl != -1 and (bl == -1 or sl < bl): |
| 50 | # Line comment starts first: take code up to '//' and stop |
| 51 | out_parts.append(line[i:sl]) |
| 52 | return ("".join(out_parts), in_block) |
| 53 | |
| 54 | if bl != -1: |
| 55 | # Block comment starts: take code up to '/*', then enter block |
| 56 | out_parts.append(line[i:bl]) |
| 57 | i = bl + 2 |
| 58 | in_block = True |
| 59 | continue |
| 60 | |
| 61 | # No more comments |
| 62 | out_parts.append(line[i:]) |
| 63 | break |
| 64 | |
| 65 | return ("".join(out_parts), in_block) |
| 66 | |
| 67 | def iter_source_files(root: Path, exts: set[str], excludes: set[str]) -> list[Path]: |
| 68 | """ |