Split *text* on commas that are not inside quotes or nested brackets. Used for list-literal elements so a quoted element containing a comma (e.g. ``["a, b", "c"]``) is not split mid-string, and nested lists/calls (e.g. ``[[1, 2], 3]``) are kept intact.
(text: str)
| 184 | |
| 185 | |
| 186 | def _split_top_level_commas(text: str) -> list[str]: |
| 187 | """Split *text* on commas that are not inside quotes or nested brackets. |
| 188 | |
| 189 | Used for list-literal elements so a quoted element containing a comma |
| 190 | (e.g. ``["a, b", "c"]``) is not split mid-string, and nested lists/calls |
| 191 | (e.g. ``[[1, 2], 3]``) are kept intact. |
| 192 | """ |
| 193 | parts: list[str] = [] |
| 194 | buf: list[str] = [] |
| 195 | quote: str | None = None |
| 196 | depth = 0 |
| 197 | for ch in text: |
| 198 | if quote is not None: |
| 199 | buf.append(ch) |
| 200 | if ch == quote: |
| 201 | quote = None |
| 202 | elif ch in ("'", '"'): |
| 203 | quote = ch |
| 204 | buf.append(ch) |
| 205 | elif ch in "([{": |
| 206 | depth += 1 |
| 207 | buf.append(ch) |
| 208 | elif ch in ")]}": |
| 209 | depth = max(0, depth - 1) |
| 210 | buf.append(ch) |
| 211 | elif ch == "," and depth == 0: |
| 212 | parts.append("".join(buf)) |
| 213 | buf = [] |
| 214 | else: |
| 215 | buf.append(ch) |
| 216 | parts.append("".join(buf)) |
| 217 | return parts |
| 218 | |
| 219 | |
| 220 | def _find_top_level(text: str, token: str) -> int: |
no outgoing calls
no test coverage detected