From 34c74255d781ad0a0bf7cc5ad4ec1761bef61e04 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 18 Aug 2026 01:57:02 +0900 Subject: refactor(pcre): drop the two bespoke isMatch variants is_match_named and is_match_with_indexed_captures reshaped a match into a name-keyed map or a number-positioned vec, each allocating a String per group up front for callers that then read one or two of them. Every one of the eleven call sites ports a plain Preg::isMatch in PHP, so they now call is_match3 and reach for the group they want through get(&CaptureKey::ByIndex(N)) / get(&CaptureKey::ByName(..)), the same way the rest of the tree already reads a match. Falling out of that: PregNamedGroups existed only to type the first variant; PregMatches::iter() only to build both; and PregMatches::pattern only to give iter() the capture names. PregMatches is now a plain wrapper over regex::Captures, so preg_replace_callback no longer clones the resolved pattern for every match, and preg_match_map! is internal to the shim again. SvnDriver::get_file_content and get_change_date recover the flat `isMatch(..) && $match[2] !== null` condition the PHP has, which the vec shape had forced into a nested if. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe/src/json/json_manipulator.rs | 43 +++++++++++++++++------------ 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'crates/shirabe/src/json/json_manipulator.rs') diff --git a/crates/shirabe/src/json/json_manipulator.rs b/crates/shirabe/src/json/json_manipulator.rs index 92661d41..0f0d50de 100644 --- a/crates/shirabe/src/json/json_manipulator.rs +++ b/crates/shirabe/src/json/json_manipulator.rs @@ -737,21 +737,23 @@ impl JsonManipulator { &children[cm.value_end..] ); } else { - if let Some(leading_match) = Preg::is_match_named( + if let Some(leading_match) = Preg::is_match3( php_regex!( "#^\\{(?P\\s*?)(?P\\S+.*?)?(?P\\s*)\\}$#s" ), &children, ) { let mut whitespace = leading_match - .get("trailingspace") - .cloned() - .unwrap_or_default(); + .get(&CaptureKey::ByName("trailingspace".to_string())) + .unwrap_or_default() + .to_string(); let leading_space = leading_match - .get("leadingspace") - .cloned() - .unwrap_or_default(); - let content_present = leading_match.get("content").is_some(); + .get(&CaptureKey::ByName("leadingspace".to_string())) + .unwrap_or_default() + .to_string(); + let content_present = leading_match + .get(&CaptureKey::ByName("content".to_string())) + .is_some(); if content_present { let mut value_local = value; if let Some(ref sub) = sub_name { @@ -937,10 +939,12 @@ impl JsonManipulator { let children_clean = children_clean.ok_or_else(|| InvalidArgumentException::new("JsonManipulator: $childrenClean is not defined. Please report at https://github.com/nsfisis/php-shirabe/issues/new.".to_string()))?; // no child data left, $name was the only key in - if let Some(empty_match) = Preg::is_match_named( + if let Some(empty_match) = Preg::is_match3( php_regex!("#^\\{\\s*?(?P\\S+.*?)?(?P\\s*)\\}$#s"), &children_clean, - ) && empty_match.get("content").is_none() + ) && empty_match + .get(&CaptureKey::ByName("content".to_string())) + .is_none() { self.contents = format!( "{}{{{}{}}}{}", @@ -1032,20 +1036,20 @@ impl JsonManipulator { return Ok(false); } - if let Some(leading_match) = Preg::is_match_named( + if let Some(leading_match) = Preg::is_match3( php_regex!( "#^\\[(?P\\s*?)(?P\\S+.*?)?(?P\\s*)\\]$#s" ), &children, ) { let leading_whitespace = leading_match - .get("leadingspace") - .cloned() - .unwrap_or_default(); + .get(&CaptureKey::ByName("leadingspace".to_string())) + .unwrap_or_default() + .to_string(); let mut whitespace = leading_match - .get("trailingspace") - .cloned() - .unwrap_or_default(); + .get(&CaptureKey::ByName("trailingspace".to_string())) + .unwrap_or_default() + .to_string(); let mut leading_item_whitespace = format!("{}{}{}", self.newline, self.indent, self.indent); let mut trailing_item_whitespace = whitespace.clone(); @@ -1058,7 +1062,10 @@ impl JsonManipulator { item_depth = 0; } - if leading_match.get("content").is_some() { + if leading_match + .get(&CaptureKey::ByName("content".to_string())) + .is_some() + { // child missing but non empty children if append { children = Preg::replace( -- cgit v1.3.1-4-g156e