End (exclusive) of the comment, char or string literal opening at ``i``. Returns ``i`` when no such token opens there. A line-bounded token (a ``//`` comment, a char literal, a regular string) stops *before* its newline, so the caller still sees the next line start. Holes of interpolate
(source: bytes, i: int)
| 4546 | |
| 4547 | |
| 4548 | def _csharp_token_end(source: bytes, i: int) -> int: |
| 4549 | """End (exclusive) of the comment, char or string literal opening at ``i``. |
| 4550 | |
| 4551 | Returns ``i`` when no such token opens there. A line-bounded token (a ``//`` |
| 4552 | comment, a char literal, a regular string) stops *before* its newline, so |
| 4553 | the caller still sees the next line start. Holes of interpolated strings |
| 4554 | are walked as code, so a quote inside ``{...}`` does not end the string. |
| 4555 | """ |
| 4556 | n = len(source) |
| 4557 | if source.startswith(b"//", i): |
| 4558 | nl = source.find(b"\n", i) |
| 4559 | return n if nl == -1 else nl |
| 4560 | if source.startswith(b"/*", i): |
| 4561 | end = source.find(b"*/", i + 2) |
| 4562 | return n if end == -1 else end + 2 |
| 4563 | if source[i:i + 1] == b"'": |
| 4564 | j = i + 1 |
| 4565 | while j < n and source[j:j + 1] not in (b"'", b"\n"): |
| 4566 | j += 2 if source[j:j + 1] == b"\\" else 1 |
| 4567 | return j + 1 if source[j:j + 1] == b"'" else min(j, n) |
| 4568 | quote, run, interpolated, verbatim = _csharp_string_opener(source, i) |
| 4569 | if run == 0: |
| 4570 | return i |
| 4571 | if run >= 3: |
| 4572 | end = source.find(b'"' * run, quote + run) |
| 4573 | return n if end == -1 else end + run |
| 4574 | if run == 2: |
| 4575 | return quote + 2 |
| 4576 | j = quote + 1 |
| 4577 | while j < n: |
| 4578 | ch = source[j:j + 1] |
| 4579 | if verbatim and source.startswith(b'""', j): |
| 4580 | j += 2 |
| 4581 | elif ch == b"\\" and not verbatim: |
| 4582 | j += 2 |
| 4583 | elif ch == b'"': |
| 4584 | return j + 1 |
| 4585 | elif ch == b"\n" and not verbatim: |
| 4586 | return j |
| 4587 | elif interpolated and source.startswith(b"{{", j): |
| 4588 | j += 2 |
| 4589 | elif interpolated and ch == b"{": |
| 4590 | depth = 1 |
| 4591 | j += 1 |
| 4592 | while j < n and depth: |
| 4593 | end = _csharp_token_end(source, j) |
| 4594 | if end > j: |
| 4595 | j = end |
| 4596 | continue |
| 4597 | c = source[j:j + 1] |
| 4598 | depth += (c == b"{") - (c == b"}") |
| 4599 | j += 1 |
| 4600 | else: |
| 4601 | j += 1 |
| 4602 | return n |
| 4603 | |
| 4604 | |
| 4605 | def _csharp_branch_balanced(text: bytearray, start: int, end: int) -> bool: |
no test coverage detected