From cb77f7c7076aa4bac3e6aaa1c164cf9c1d449ddc Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 18 Aug 2026 01:57:02 +0900 Subject: refactor(preg): fold preg_match into preg_match2 preg_match copied every group into a Vec> while preg_match2 handed back the borrowed captures. They now differ only in the offset argument, so preg_match delegates with offset 0 and its callers read groups through PregMatches::get. Going through preg_match2 also makes preg_match honour the PCRE A modifier, which it used to ignore; no caller passes such a pattern. VersionParser::manipulate_version_string takes an index accessor instead of a slice, and VersionParser::normalize matches against a copy of the subject because the captures outlive the assignments to $version. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/formatter/output_formatter.rs | 6 ++++-- .../src/input/argv_input.rs | 4 ++-- crates/shirabe-symfony-console/src/terminal.rs | 24 +++++++++++++--------- 3 files changed, 20 insertions(+), 14 deletions(-) (limited to 'crates/shirabe-symfony-console') diff --git a/crates/shirabe-symfony-console/src/formatter/output_formatter.rs b/crates/shirabe-symfony-console/src/formatter/output_formatter.rs index 571d66c0..256fade1 100644 --- a/crates/shirabe-symfony-console/src/formatter/output_formatter.rs +++ b/crates/shirabe-symfony-console/src/formatter/output_formatter.rs @@ -190,9 +190,11 @@ impl OutputFormatter { prefix = String::new(); } - let matches = preg_match(php_regex!("~(\\n)$~"), &text).unwrap_or_default(); + let trailing = preg_match(php_regex!("~(\\n)$~"), &text) + .and_then(|matches| matches.get(1)) + .unwrap_or("") + .to_string(); text = format!("{}{}", prefix, self.add_line_breaks(&text, width)); - let trailing = matches.get(1).and_then(|m| m.clone()).unwrap_or_default(); text = format!("{}{}", shirabe_php_shim::rtrim(&text, Some("\n")), trailing); if *current_line_length == 0 diff --git a/crates/shirabe-symfony-console/src/input/argv_input.rs b/crates/shirabe-symfony-console/src/input/argv_input.rs index 81582408..94e5299d 100644 --- a/crates/shirabe-symfony-console/src/input/argv_input.rs +++ b/crates/shirabe-symfony-console/src/input/argv_input.rs @@ -526,8 +526,8 @@ impl std::fmt::Display for ArgvInput { if let Some(r#match) = preg_match(php_regex!("{^(-[^=]+=)(.+)}"), token) { return format!( "{}{}", - r#match[1].as_deref().unwrap_or(""), - self.inner.escape_token(r#match[2].as_deref().unwrap_or("")) + r#match.get(1).unwrap_or(""), + self.inner.escape_token(r#match.get(2).unwrap_or("")) ); } diff --git a/crates/shirabe-symfony-console/src/terminal.rs b/crates/shirabe-symfony-console/src/terminal.rs index 8db6fd93..492d8fe2 100644 --- a/crates/shirabe-symfony-console/src/terminal.rs +++ b/crates/shirabe-symfony-console/src/terminal.rs @@ -89,17 +89,17 @@ impl Terminal { // or [w, h] from "wxh" WIDTH.with(|w| { w.set(Some(shirabe_php_shim::intval(&PhpMixed::String( - matches[1].clone().unwrap_or_default(), + matches.get(1).unwrap_or_default().to_string(), )))) }); HEIGHT.with(|h| { - let value = if matches.get(4).map(|m| m.is_some()).unwrap_or(false) { + let value = if matches.get(4).is_some() { shirabe_php_shim::intval(&PhpMixed::String( - matches[4].clone().unwrap_or_default(), + matches.get(4).unwrap_or_default().to_string(), )) } else { shirabe_php_shim::intval(&PhpMixed::String( - matches[2].clone().unwrap_or_default(), + matches.get(2).unwrap_or_default().to_string(), )) }; h.set(Some(value)); @@ -141,12 +141,12 @@ impl Terminal { // extract [w, h] from "rows h; columns w;" WIDTH.with(|w| { w.set(Some(shirabe_php_shim::intval(&PhpMixed::String( - matches[2].clone().unwrap_or_default(), + matches.get(2).unwrap_or_default().to_string(), )))) }); HEIGHT.with(|h| { h.set(Some(shirabe_php_shim::intval(&PhpMixed::String( - matches[1].clone().unwrap_or_default(), + matches.get(1).unwrap_or_default().to_string(), )))) }); } else if let Some(matches) = @@ -155,12 +155,12 @@ impl Terminal { // extract [w, h] from "; h rows; w columns" WIDTH.with(|w| { w.set(Some(shirabe_php_shim::intval(&PhpMixed::String( - matches[2].clone().unwrap_or_default(), + matches.get(2).unwrap_or_default().to_string(), )))) }); HEIGHT.with(|h| { h.set(Some(shirabe_php_shim::intval(&PhpMixed::String( - matches[1].clone().unwrap_or_default(), + matches.get(1).unwrap_or_default().to_string(), )))) }); } @@ -180,8 +180,12 @@ impl Terminal { )?; Some(vec![ - shirabe_php_shim::intval(&PhpMixed::String(matches[2].clone().unwrap_or_default())), - shirabe_php_shim::intval(&PhpMixed::String(matches[1].clone().unwrap_or_default())), + shirabe_php_shim::intval(&PhpMixed::String( + matches.get(2).unwrap_or_default().to_string(), + )), + shirabe_php_shim::intval(&PhpMixed::String( + matches.get(1).unwrap_or_default().to_string(), + )), ]) } -- cgit v1.3.1-4-g156e