From 24a4f39bd981768cc299121b506f6bba584cb31d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 16 Aug 2026 17:58:11 +0900 Subject: refactor(preg): merge preg_replace_callback2 into preg_replace_callback preg_replace_callback2 had one caller, Preg::replace_callback6, which was itself reached only from Preg::replace_callback with the default limit, count and flags. Fold the two shim functions into one and drop those parameters along with replace_callback6. The surviving callback takes callback2's IndexMap of matches: it can be keyed by group name, and it omits trailing non-participating groups the way PHP does, which is what the callbacks in Process and ProgressBar test for. --- crates/shirabe-symfony-process/src/process.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'crates/shirabe-symfony-process/src') diff --git a/crates/shirabe-symfony-process/src/process.rs b/crates/shirabe-symfony-process/src/process.rs index 87c6c68d..556238cf 100644 --- a/crates/shirabe-symfony-process/src/process.rs +++ b/crates/shirabe-symfony-process/src/process.rs @@ -11,7 +11,7 @@ use crate::pipes::unix_pipes::UnixPipes; use crate::pipes::windows_pipes::WindowsPipes; use crate::process_utils::ProcessUtils; use indexmap::IndexMap; -use shirabe_php_shim::{Descriptor, PhpMixed, PhpResource, php_regex}; +use shirabe_php_shim::{CaptureKey, Descriptor, PhpMixed, PhpResource, php_regex}; use std::sync::OnceLock; /// A user-supplied callback invoked with the output type ("out"/"err") and a chunk of output. @@ -935,9 +935,9 @@ impl Process { )++ ) | [^"]*+ )"/x"# ), - |m: &[Option]| -> anyhow::Result { - let m0 = m.first().cloned().flatten().unwrap_or_default(); - let m1 = m.get(1).cloned().flatten(); + |m: &IndexMap>| -> anyhow::Result { + let m0 = m[&CaptureKey::ByIndex(0)].clone().unwrap_or_default(); + let m1 = m.get(&CaptureKey::ByIndex(1)).cloned().flatten(); if m1.is_none() { return Ok(m0); } @@ -1073,8 +1073,12 @@ impl Process { ) -> anyhow::Result { shirabe_php_shim::preg_replace_callback( php_regex!(r#"/"\$\{:([_a-zA-Z]+[_a-zA-Z0-9]*)\}"/"#), - |matches: &[Option]| -> anyhow::Result { - let key = matches.get(1).cloned().flatten().unwrap_or_default(); + |matches: &IndexMap>| -> anyhow::Result { + let key = matches + .get(&CaptureKey::ByIndex(1)) + .cloned() + .flatten() + .unwrap_or_default(); match env.get(&key) { None => Err(InvalidArgumentException::new(format!( "Command line is missing a value for parameter \"{}\": {}", -- cgit v1.3.1-4-g156e