Return `line` with string literals and `//` comments blanked out, so bracket-counting on the result ignores brackets that live inside strings or end-of-line comments.
(line: str)
| 161 | |
| 162 | |
| 163 | def _strip_strings_and_comments(line: str) -> str: |
| 164 | """Return `line` with string literals and `//` comments blanked out, so |
| 165 | bracket-counting on the result ignores brackets that live inside strings |
| 166 | or end-of-line comments.""" |
| 167 | result: list[str] = [] |
| 168 | i = 0 |
| 169 | n = len(line) |
| 170 | while i < n: |
| 171 | ch = line[i] |
| 172 | |
| 173 | # Line comment — drop everything to end-of-line |
| 174 | if ch == "/" and i + 1 < n and line[i + 1] == "/": |
| 175 | break |
| 176 | |
| 177 | # Double-quoted string |
| 178 | if ch == '"': |
| 179 | result.append(" ") |
| 180 | i += 1 |
| 181 | while i < n: |
| 182 | if line[i] == "\\" and i + 1 < n: |
| 183 | i += 2 |
| 184 | result.append(" ") |
| 185 | continue |
| 186 | if line[i] == '"': |
| 187 | result.append(" ") |
| 188 | i += 1 |
| 189 | break |
| 190 | result.append(" ") |
| 191 | i += 1 |
| 192 | continue |
| 193 | |
| 194 | # Single-quoted string (JS string literal) |
| 195 | if ch == "'": |
| 196 | result.append(" ") |
| 197 | i += 1 |
| 198 | while i < n: |
| 199 | if line[i] == "\\" and i + 1 < n: |
| 200 | i += 2 |
| 201 | result.append(" ") |
| 202 | continue |
| 203 | if line[i] == "'": |
| 204 | result.append(" ") |
| 205 | i += 1 |
| 206 | break |
| 207 | result.append(" ") |
| 208 | i += 1 |
| 209 | continue |
| 210 | |
| 211 | result.append(ch) |
| 212 | i += 1 |
| 213 | |
| 214 | return "".join(result) |
| 215 | |
| 216 | |
| 217 | def _bracket_delta(line: str) -> int: |
no test coverage detected