Blank out string/char literals and comments so brace counting is sane.
(text)
| 51 | |
| 52 | |
| 53 | def strip_noise(text): |
| 54 | """Blank out string/char literals and comments so brace counting is sane.""" |
| 55 | out = [] |
| 56 | i, n = 0, len(text) |
| 57 | while i < n: |
| 58 | c = text[i] |
| 59 | if c == "/" and i + 1 < n and text[i + 1] == "/": |
| 60 | while i < n and text[i] != "\n": |
| 61 | out.append(" ") |
| 62 | i += 1 |
| 63 | elif c == "/" and i + 1 < n and text[i + 1] == "*": |
| 64 | while i + 1 < n and not (text[i] == "*" and text[i + 1] == "/"): |
| 65 | out.append("\n" if text[i] == "\n" else " ") |
| 66 | i += 1 |
| 67 | out.append(" ") |
| 68 | i += 2 |
| 69 | elif c in "\"'": |
| 70 | quote = c |
| 71 | out.append(" ") |
| 72 | i += 1 |
| 73 | while i < n and text[i] != quote: |
| 74 | if text[i] == "\\": |
| 75 | out.append(" ") |
| 76 | i += 1 |
| 77 | if i < n: |
| 78 | out.append("\n" if text[i] == "\n" else " ") |
| 79 | i += 1 |
| 80 | out.append(" ") |
| 81 | i += 1 |
| 82 | else: |
| 83 | out.append(c) |
| 84 | i += 1 |
| 85 | return "".join(out) |
| 86 | |
| 87 | |
| 88 | def function_spans(path): |