Replaces all content inside { ... } with just {} to prevent capturing local vars.
(text: str)
| 431 | |
| 432 | |
| 433 | def _strip_inline_bodies(text: str) -> str: |
| 434 | """Replaces all content inside { ... } with just {} to prevent capturing local vars.""" |
| 435 | result = [] |
| 436 | depth = 0 |
| 437 | in_comment = False |
| 438 | in_string = False |
| 439 | i = 0 |
| 440 | n = len(text) |
| 441 | while i < n: |
| 442 | c = text[i] |
| 443 | |
| 444 | # Strings |
| 445 | if not in_comment and c == '"': |
| 446 | if i == 0 or text[i-1] != '\\': |
| 447 | in_string = not in_string |
| 448 | result.append(c) |
| 449 | i += 1 |
| 450 | continue |
| 451 | |
| 452 | # Comments |
| 453 | if not in_string and c == '/' and i + 1 < n: |
| 454 | if text[i+1] == '*': |
| 455 | in_comment = True |
| 456 | result.append('/*') |
| 457 | i += 2 |
| 458 | continue |
| 459 | elif text[i+1] == '/': |
| 460 | # Line comment, skip to newline |
| 461 | while i < n and text[i] != '\n': |
| 462 | if depth == 0: result.append(text[i]) |
| 463 | i += 1 |
| 464 | if i < n: |
| 465 | if depth == 0: result.append('\n') |
| 466 | i += 1 |
| 467 | continue |
| 468 | if in_comment and c == '*' and i + 1 < n and text[i+1] == '/': |
| 469 | in_comment = False |
| 470 | result.append('*/') |
| 471 | i += 2 |
| 472 | continue |
| 473 | |
| 474 | if in_comment or in_string: |
| 475 | if depth == 0: |
| 476 | result.append(c) |
| 477 | i += 1 |
| 478 | continue |
| 479 | |
| 480 | # Braces |
| 481 | if c == '{': |
| 482 | if depth == 0: |
| 483 | result.append('{') |
| 484 | depth += 1 |
| 485 | i += 1 |
| 486 | elif c == '}': |
| 487 | depth -= 1 |
| 488 | if depth == 0: |
| 489 | result.append('}') |
| 490 | i += 1 |