From 1b42bff7d960082d7fbd6158ec226e38899c0ca9 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Thu, 25 Jun 2026 13:12:28 +0900 Subject: fix(preg): treat escaped \$ in replacement as a literal dollar sign Co-Authored-By: Claude Opus 4.8 (1M context) --- crates/shirabe-php-shim/src/preg.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/shirabe-php-shim/src/preg.rs b/crates/shirabe-php-shim/src/preg.rs index 3d36137..56aa402 100644 --- a/crates/shirabe-php-shim/src/preg.rs +++ b/crates/shirabe-php-shim/src/preg.rs @@ -441,6 +441,12 @@ fn php_replacement_expand(template: &str, caps: ®ex::Captures, out: &mut Vec< out.push(b'\\'); i += 2; } + // A backslash escapes a following `$`, yielding a literal dollar sign (so an escaped + // `\$1` is not mistaken for the `$1` backreference). + b'\\' if i + 1 < bytes.len() && bytes[i + 1] == b'$' => { + out.push(b'$'); + i += 2; + } b'$' if i + 1 < bytes.len() && bytes[i + 1] == b'{' => { let rest = &bytes[i + 2..]; match rest.iter().position(|&b| b == b'}') { -- cgit v1.3.1