From c7aa10384a2548167466b4c61f9eff29ed8611f6 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 17 Aug 2026 07:13:02 +0900 Subject: refactor(preg): report unmatched groups as null throughout The shim carried two reporting modes for the preg_* $matches maps: PHP's default (trailing unmatched groups dropped, interior ones ""), and the PREG_UNMATCHED_AS_NULL form, picked by calling a *_unmatched_as_null() variant. The regex crate hands out Option, which maps onto the null form directly, and no caller distinguished a dropped group from a null one -- preg_match() and preg_match_all2() already reported nulls unconditionally. Keep only the null form; the shim API no longer mirrors PHP's flag set, which is intended. Preg::is_match_with_indexed_captures() modelled PHP's "unset" as a truncated Vec, and now returns Vec>. That is what Composer actually does: Preg::isMatch() always sets PREG_UNMATCHED_AS_NULL, and its callers test groups with `!== null`. preg_match_all(), preg_match_all_set_order() and preg_split_delim_capture() still hand back Vec and keep the "" form. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe/src/command/fund_command.rs | 2 +- crates/shirabe/src/command/update_command.rs | 6 ++---- crates/shirabe/src/installer/binary_installer.rs | 6 +++--- crates/shirabe/src/package/version/version_guesser.rs | 6 +++--- crates/shirabe/src/repository/vcs/svn_driver.rs | 12 ++++++------ crates/shirabe/src/util/hg.rs | 2 +- crates/shirabe/src/util/platform.rs | 4 +--- 7 files changed, 17 insertions(+), 21 deletions(-) (limited to 'crates/shirabe/src') diff --git a/crates/shirabe/src/command/fund_command.rs b/crates/shirabe/src/command/fund_command.rs index 7f2dd24c..109943d6 100644 --- a/crates/shirabe/src/command/fund_command.rs +++ b/crates/shirabe/src/command/fund_command.rs @@ -67,7 +67,7 @@ impl FundCommand { php_regex!(r"{^https://github.com/([^/]+)$}"), &url, ) - && let Some(sponsor) = matches.into_iter().nth(1) + && let Some(sponsor) = matches.into_iter().nth(1).flatten() { url = format!("https://github.com/sponsors/{}", sponsor); } diff --git a/crates/shirabe/src/command/update_command.rs b/crates/shirabe/src/command/update_command.rs index 38a79e30..2eca6db7 100644 --- a/crates/shirabe/src/command/update_command.rs +++ b/crates/shirabe/src/command/update_command.rs @@ -466,10 +466,8 @@ impl Command for UpdateCommand { let Some(matches) = matches else { continue; }; - let constraint = parser.parse_constraints(&format!( - "~{}", - matches.get(1).cloned().unwrap_or_default() - ))?; + let constraint = parser + .parse_constraints(&format!("~{}", matches[1].clone().unwrap_or_default()))?; if let Some(existing) = temporary_constraints.get(&package.get_name()) { temporary_constraints.insert( package.get_name(), diff --git a/crates/shirabe/src/installer/binary_installer.rs b/crates/shirabe/src/installer/binary_installer.rs index d5ece82f..606e943f 100644 --- a/crates/shirabe/src/installer/binary_installer.rs +++ b/crates/shirabe/src/installer/binary_installer.rs @@ -328,10 +328,10 @@ impl BinaryInstaller { &bin_contents, ) { // carry over the existing shebang if present, otherwise add our own - let proxy_code = if m.get(1).is_none() { + let proxy_code = if m[1].is_none() { "#!/usr/bin/env php".to_string() } else { - trim(m.get(1).map(|s| s.as_str()).unwrap_or(""), None) + trim(m[1].as_deref().unwrap_or(""), None) }; let bin_path_exported = self .filesystem @@ -377,7 +377,7 @@ impl BinaryInstaller { $data = str_replace('__FILE__', var_export($this->realpath, true), $data);" .to_string(); } - if trim(m.first().map(|s| s.as_str()).unwrap_or(""), None) != "\w+)|%(?P\w+)%)(?P.*)#"), |matches: &PregMatchedGroups| -> String { let var = matches .get(&CaptureKey::ByName("dvar".to_string())) - .filter(|dvar| !dvar.is_empty()) .or_else(|| matches.get(&CaptureKey::ByName("pvar".to_string()))) .map(|s| s.as_str()) .unwrap_or(""); -- cgit v1.3.1-4-g156e