Read a Rust string with a regex and kill the surviving mutants
CI / tests (pull_request) Failing after 5m39s

This commit is contained in:
2026-11-26 00:28:10 +01:00
parent dd15690903
commit 6719d10034
3 changed files with 27 additions and 26 deletions
+14 -10
View File
@@ -13,6 +13,7 @@ SCAN = re.compile(
rb"""|\#\[cfg\(test\)\]""",
re.DOTALL,
)
STRING_BODY = re.compile(rb'(?:[^"\\]|\\.)*"', re.DOTALL)
COMMENT_EDGE = re.compile(rb"/\*|\*/")
# What must follow the attribute for it to start an inline module: other attributes, a visibility, then `mod name {`.
MODULE = re.compile(rb"\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+(?P<name>\w+)\s*\{")
@@ -37,9 +38,9 @@ def modules(source: bytes) -> dict[bytes, tuple[int, int]] | None:
if token == b"/*":
position = after_comment(source, position)
elif token == b'"':
position = after_string(source, position, b'"')
position = after_string(source, position)
elif match.group("hashes") is not None:
position = after_string(source, position, b'"' + match.group("hashes"), raw=True)
position = after_raw_string(source, position, match.group("hashes"))
elif token == b"{":
depth += 1
elif token == b"}":
@@ -63,14 +64,17 @@ def after_comment(source: bytes, position: int) -> int | None:
return position if level == 0 else None
def after_string(source: bytes, position: int, closing: bytes, raw: bool = False) -> int | None:
"""The offset after the string whose opening quote ends at position, None where it is never closed. A raw string has no escapes."""
while (found := source.find(closing, position)) >= 0:
between = source[position:found]
if raw or (len(between) - len(between.rstrip(b"\\"))) % 2 == 0:
return found + len(closing)
position = found + 1
return None
def after_string(source: bytes, position: int) -> int | None:
"""The offset after the string whose opening quote ends at position, None where it is never closed. A backslash escapes the next byte."""
closed = STRING_BODY.match(source, position)
return closed.end() if closed else None
def after_raw_string(source: bytes, position: int, hashes: bytes) -> int | None:
"""The offset after the raw string, opened by `r`, hashes and a quote, that ends at position, None where it is never closed. It has no escapes."""
closing = b'"' + hashes
found = source.find(closing, position)
return found + len(closing) if found >= 0 else None
def with_tests_of(planted: bytes, copy: bytes) -> bytes: