From c1070afe69f53b621adea232527080d2c922e1a9 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 14 Jun 2026 12:48:58 +0900 Subject: refactor(pcre): consolidate duplicate preg_* shim helpers The preg.rs shim had several near-duplicate functions: simple helpers re-implementing logic already covered by their full-featured `2` variants. Delegate or remove the redundant ones while preserving behavior, and migrate the affected callers: - preg_replace / preg_split now delegate to preg_replace2 / preg_split2 - preg_match_all_simple removed; its caller uses preg_match_all - preg_split_chars removed; its caller uses a char-boundary iterator - preg_match_offset removed; its callers use preg_match2 directly Co-Authored-By: Claude Opus 4.8 (1M context) --- .../symfony/console/formatter/output_formatter.rs | 6 +- .../src/symfony/console/input/string_input.rs | 76 +++++++++++++++------- 2 files changed, 54 insertions(+), 28 deletions(-) (limited to 'crates/shirabe-external-packages') diff --git a/crates/shirabe-external-packages/src/symfony/console/formatter/output_formatter.rs b/crates/shirabe-external-packages/src/symfony/console/formatter/output_formatter.rs index 27a74e6..0de90be 100644 --- a/crates/shirabe-external-packages/src/symfony/console/formatter/output_formatter.rs +++ b/crates/shirabe-external-packages/src/symfony/console/formatter/output_formatter.rs @@ -126,12 +126,10 @@ impl OutputFormatter { .expect("preg_replace failed"); style.set_href(&url); } else if r#match[0] == "options" { - let mut options: Vec> = vec![]; - shirabe_php_shim::preg_match_all_simple( + let mut options = shirabe_php_shim::preg_match_all( "([^,;]+)", &shirabe_php_shim::strtolower(&r#match[1]), - &mut options, - )?; + ); let options = shirabe_php_shim::array_shift(&mut options).unwrap_or_default(); for option in &options { style.set_option(option); diff --git a/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs b/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs index 2d53f68..952a18e 100644 --- a/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs +++ b/crates/shirabe-external-packages/src/symfony/console/input/string_input.rs @@ -5,7 +5,7 @@ use crate::symfony::console::input::argv_input::ArgvInput; use crate::symfony::console::input::input_definition::InputDefinition; use crate::symfony::console::input::input_interface::InputInterface; use indexmap::IndexMap; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{CaptureKey, PhpMixed}; /// StringInput represents an input provided as a string. /// @@ -54,52 +54,80 @@ impl StringInput { continue; } - let mut m: Vec = vec![]; - if shirabe_php_shim::preg_match_offset(r"/\s+/A", input, &mut m, 0, cursor) { + let mut m: IndexMap> = IndexMap::new(); + if shirabe_php_shim::preg_match2(r"/\s+/A", input, Some(&mut m), 0, cursor as usize) + .expect("invalid regex") + == 1 + { if token.is_some() { tokens.push(token.take().unwrap()); } - cursor += shirabe_php_shim::strlen(&m[0]); - } else if shirabe_php_shim::preg_match_offset( + cursor += + shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or("")); + } else if shirabe_php_shim::preg_match2( &format!(r#"/([^="'\s]+?)(=?)({}+)/A"#, Self::REGEX_QUOTED_STRING), input, - &mut m, + Some(&mut m), 0, - cursor, - ) { - let inner = shirabe_php_shim::substr(&m[3], 1, Some(-1)); + cursor as usize, + ) + .expect("invalid regex") + == 1 + { + let inner = shirabe_php_shim::substr( + m[&CaptureKey::ByIndex(3)].as_deref().unwrap_or(""), + 1, + Some(-1), + ); let replaced = shirabe_php_shim::str_replace_arr(&["\"'", "'\"", "''", "\"\""], "", &inner); token = Some(format!( "{}{}{}{}", token.unwrap_or_default(), - m[1], - m[2], + m[&CaptureKey::ByIndex(1)].as_deref().unwrap_or(""), + m[&CaptureKey::ByIndex(2)].as_deref().unwrap_or(""), shirabe_php_shim::stripcslashes(&replaced) )); - cursor += shirabe_php_shim::strlen(&m[0]); - } else if shirabe_php_shim::preg_match_offset( + cursor += + shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or("")); + } else if shirabe_php_shim::preg_match2( &format!(r"/{}/A", Self::REGEX_QUOTED_STRING), input, - &mut m, + Some(&mut m), 0, - cursor, - ) { + cursor as usize, + ) + .expect("invalid regex") + == 1 + { token = Some(format!( "{}{}", token.unwrap_or_default(), - shirabe_php_shim::stripcslashes(&shirabe_php_shim::substr(&m[0], 1, Some(-1))) + shirabe_php_shim::stripcslashes(&shirabe_php_shim::substr( + m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or(""), + 1, + Some(-1) + )) )); - cursor += shirabe_php_shim::strlen(&m[0]); - } else if shirabe_php_shim::preg_match_offset( + cursor += + shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or("")); + } else if shirabe_php_shim::preg_match2( &format!(r"/{}/A", Self::REGEX_UNQUOTED_STRING), input, - &mut m, + Some(&mut m), 0, - cursor, - ) { - token = Some(format!("{}{}", token.unwrap_or_default(), m[1])); - cursor += shirabe_php_shim::strlen(&m[0]); + cursor as usize, + ) + .expect("invalid regex") + == 1 + { + token = Some(format!( + "{}{}", + token.unwrap_or_default(), + m[&CaptureKey::ByIndex(1)].as_deref().unwrap_or("") + )); + cursor += + shirabe_php_shim::strlen(m[&CaptureKey::ByIndex(0)].as_deref().unwrap_or("")); } else { // should never happen return Err( -- cgit v1.3.1