From 347368040e12e891fb79368dcf0984585d92a273 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 26 Nov 2026 00:53:45 +0100 Subject: [PATCH] End a raw string at its first closing quote by regex --- bin/task_bench/rust_test_modules.py | 5 ++--- bin/task_bench/test_rust_test_modules.py | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/bin/task_bench/rust_test_modules.py b/bin/task_bench/rust_test_modules.py index fc245f5c..18641c03 100644 --- a/bin/task_bench/rust_test_modules.py +++ b/bin/task_bench/rust_test_modules.py @@ -72,9 +72,8 @@ def after_string(source: bytes, position: int) -> int | 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 + closed = re.compile(b'"' + hashes).search(source, position) + return closed.end() if closed else None def with_tests_of(planted: bytes, copy: bytes) -> bytes: diff --git a/bin/task_bench/test_rust_test_modules.py b/bin/task_bench/test_rust_test_modules.py index 458246e8..1ec7a883 100644 --- a/bin/task_bench/test_rust_test_modules.py +++ b/bin/task_bench/test_rust_test_modules.py @@ -77,6 +77,10 @@ class ModulesTest(unittest.TestCase): source = "#[cfg(test)]\nmod tests {\n const A: &str = r\"\\\";\n}\nfn after() {}\n" self.assertEqual(list(spelt(source)), ["tests"]) + def test_ends_a_raw_string_at_its_first_closing_quote(self): + source = "#[cfg(test)]\nmod tests {\n const A: &str = r#\"x\"#;\n}\nconst B: &str = r#\"y\"#;\n" + self.assertEqual(list(spelt(source)), ["tests"]) + def test_reads_a_string_that_ends_in_two_backslashes_or_in_an_escaped_quote(self): for literal in ('"\\\\"', '"a\\""', '"\\}\\}"'): with self.subTest(literal=literal):