Return the index of the first occurrence of *token* in *text* that lies outside any quoted string or nested bracket, or ``-1`` if there is none. Used so operator/keyword splitting (``and``/``or``/``in``/comparisons) does not match a separator that appears *inside* a quoted operand -- e.
(text: str, token: str)
| 218 | |
| 219 | |
| 220 | def _find_top_level(text: str, token: str) -> int: |
| 221 | """Return the index of the first occurrence of *token* in *text* that lies |
| 222 | outside any quoted string or nested bracket, or ``-1`` if there is none. |
| 223 | |
| 224 | Used so operator/keyword splitting (``and``/``or``/``in``/comparisons) does |
| 225 | not match a separator that appears *inside* a quoted operand -- e.g. the |
| 226 | ``and`` in ``mode == 'read and write'`` or the ``or`` in ``'approve or reject'``. |
| 227 | """ |
| 228 | quote: str | None = None |
| 229 | depth = 0 |
| 230 | i = 0 |
| 231 | n = len(text) |
| 232 | while i < n: |
| 233 | ch = text[i] |
| 234 | if quote is not None: |
| 235 | if ch == quote: |
| 236 | quote = None |
| 237 | elif ch in ("'", '"'): |
| 238 | quote = ch |
| 239 | elif ch in "([{": |
| 240 | depth += 1 |
| 241 | elif ch in ")]}": |
| 242 | depth = max(0, depth - 1) |
| 243 | elif depth == 0 and text.startswith(token, i): |
| 244 | return i |
| 245 | i += 1 |
| 246 | return -1 |
| 247 | |
| 248 | |
| 249 | def _evaluate_simple_expression(expr: str, namespace: dict[str, Any]) -> Any: |
no outgoing calls
no test coverage detected