From 79b504e55cd4c4d1da102c3a076dc2a2e1edcd65 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 17 Aug 2026 08:05:42 +0900 Subject: refactor(pcre): return the Preg $matches instead of filling an out-param `Composer\Pcre\Preg` fills `$matches` through a by-ref parameter, and the port mirrored that with a `&mut` (or `Option<&mut>`) out-param plus a bool or count return. Callers had to declare an empty map one line ahead of the call, and the type never said the map is only meaningful when the call matched. Return the matches instead: - match3/match4/is_match3/is_match4 -> Option - is_match_named -> Option - match_all2/is_match_all -> PregMatchesAll - is_match_all_with_offsets3 -> PregMatchesAllWithOffsets Nothing is lost: the bool is `Option::is_some()`, and the occurrence count is the length of any one column of a PREG_PATTERN_ORDER map, now spelled `PregMatchesAll::occurrence_count()`. is_match() still answers the bool question directly for callers that want no groups. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe/src/downloader/git_downloader.rs | 69 ++++++++++--------------- crates/shirabe/src/downloader/svn_downloader.rs | 5 +- crates/shirabe/src/downloader/zip_downloader.rs | 23 ++++----- 3 files changed, 37 insertions(+), 60 deletions(-) (limited to 'crates/shirabe/src/downloader') diff --git a/crates/shirabe/src/downloader/git_downloader.rs b/crates/shirabe/src/downloader/git_downloader.rs index f60b231d..d3f4441a 100644 --- a/crates/shirabe/src/downloader/git_downloader.rs +++ b/crates/shirabe/src/downloader/git_downloader.rs @@ -17,7 +17,7 @@ use crate::util::Platform; use crate::util::ProcessExecutor; use crate::util::Url; use indexmap::IndexMap; -use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups, PregMatchesAll}; +use shirabe_pcre::{CaptureKey, Preg}; use shirabe_php_shim::{ CmpOp, PhpMixed, RuntimeException, array_map, basename, dirname, impl_php_class, implode, in_array_strict, is_dir, php_regex, preg_quote, preg_split, realpath, rtrim, strlen, strpos, @@ -95,26 +95,20 @@ impl GitDownloader { } let mut refs = trim(&output, None); - let mut head_match = PregMatchedGroups::new(); - if !Preg::is_match3( - php_regex!(r"{^([a-f0-9]+) HEAD$}mi"), - &refs, - Some(&mut head_match), - ) { + let Some(head_match) = Preg::is_match3(php_regex!(r"{^([a-f0-9]+) HEAD$}mi"), &refs) else { // could not match the HEAD for some reason return Ok(None); - } + }; let head_ref = head_match .get(&CaptureKey::ByIndex(1)) .cloned() .unwrap_or_default(); - let mut branches_match = PregMatchesAll::new(); - if !Preg::is_match_all( + let branches_match = Preg::is_match_all( format!("{{^{} refs/heads/(.+)$}}mi", preg_quote(&head_ref, None)), &refs, - &mut branches_match, - ) { + ); + if branches_match.occurrence_count() == 0 { // not on a branch, we are either on a not-modified tag or some sort of detached head, so skip this return Ok(None); } @@ -137,15 +131,14 @@ impl GitDownloader { // try to find matching branch names in remote repos for candidate in &candidate_branches { - let mut m = PregMatchesAll::new(); - if Preg::is_match_all( + let m = Preg::is_match_all( format!( "{{^[a-f0-9]+ refs/remotes/((?:[^/]+)/{})$}}mi", preg_quote(candidate, None) ), &refs, - &mut m, - ) { + ); + if m.occurrence_count() > 0 { let matches: Vec> = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); for match_ in matches { @@ -510,14 +503,12 @@ impl GitDownloader { fn set_push_url(&self, path: &str, url: &str) { // set push url for github projects - let mut match_ = PregMatchedGroups::new(); - if Preg::is_match3( + if let Some(match_) = Preg::is_match3( format!( "{{^(?:https?|git)://{}/([^/]+)/([^/]+?)(?:\\.git)?$}}", GitUtil::get_github_domains_regex(&self.inner.config.borrow()) ), url, - Some(&mut match_), ) { let protocols = self.inner.config.borrow_mut().get("github-protocols"); let m1 = match_ @@ -1114,31 +1105,23 @@ impl VcsDownloader for GitDownloader { &mut output, Some(&path), ) == 0 + && let Some(origin_match) = + Preg::is_match3(php_regex!(r"{^origin\s+(?P\S+)}m"), &output) + && let Some(composer_match) = + Preg::is_match3(php_regex!(r"{^composer\s+(?P\S+)}m"), &output) { - let mut origin_match = PregMatchedGroups::new(); - let mut composer_match = PregMatchedGroups::new(); - if Preg::is_match3( - php_regex!(r"{^origin\s+(?P\S+)}m"), - &output, - Some(&mut origin_match), - ) && Preg::is_match3( - php_regex!(r"{^composer\s+(?P\S+)}m"), - &output, - Some(&mut composer_match), - ) { - let origin_url = origin_match - .get(&CaptureKey::ByName("url".to_string())) - .cloned() - .unwrap_or_default(); - let composer_url = composer_match - .get(&CaptureKey::ByName("url".to_string())) - .cloned() - .unwrap_or_default(); - if origin_url == composer_url - && Some(composer_url.as_str()) != target.get_source_url().as_deref() - { - update_origin_url = true; - } + let origin_url = origin_match + .get(&CaptureKey::ByName("url".to_string())) + .cloned() + .unwrap_or_default(); + let composer_url = composer_match + .get(&CaptureKey::ByName("url".to_string())) + .cloned() + .unwrap_or_default(); + if origin_url == composer_url + && Some(composer_url.as_str()) != target.get_source_url().as_deref() + { + update_origin_url = true; } } if update_origin_url && target.get_source_url().is_some() { diff --git a/crates/shirabe/src/downloader/svn_downloader.rs b/crates/shirabe/src/downloader/svn_downloader.rs index 29662be8..ce908206 100644 --- a/crates/shirabe/src/downloader/svn_downloader.rs +++ b/crates/shirabe/src/downloader/svn_downloader.rs @@ -15,7 +15,7 @@ use crate::util::Filesystem; use crate::util::ProcessExecutor; use crate::util::Svn as SvnUtil; use indexmap::IndexMap; -use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups}; +use shirabe_pcre::{CaptureKey, Preg}; use shirabe_php_shim::{ CmpOp, PhpMixed, RuntimeException, impl_php_class, is_dir, php_regex, preg_split, version_compare, @@ -383,8 +383,7 @@ impl VcsDownloader for SvnDownloader { } let url_pattern = "#(.*)#"; - let mut matches = PregMatchedGroups::new(); - let base_url = if Preg::match3(url_pattern, &output, Some(&mut matches)) { + let base_url = if let Some(matches) = Preg::match3(url_pattern, &output) { matches .get(&CaptureKey::ByIndex(1)) .cloned() diff --git a/crates/shirabe/src/downloader/zip_downloader.rs b/crates/shirabe/src/downloader/zip_downloader.rs index 91078dc9..cefada15 100644 --- a/crates/shirabe/src/downloader/zip_downloader.rs +++ b/crates/shirabe/src/downloader/zip_downloader.rs @@ -8,7 +8,7 @@ use crate::package::PackageInterfaceHandle; use crate::util::IniHelper; use crate::util::Platform; use indexmap::IndexMap; -use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups}; +use shirabe_pcre::{CaptureKey, Preg}; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ CmpOp, ErrorException, PhpMixed, RuntimeException, UnexpectedValueException, ZipArchive, @@ -113,20 +113,15 @@ impl ZipDownloader { .execute(&[command_spec[1].as_str()], &mut output, None::<&str>) .unwrap_or(1) == 0 + && let Some(m) = + Preg::is_match3(php_regex!(r"{^\s*7-Zip(?:\s\[64\])?\s([0-9.]+)}"), &output) { - let mut m = PregMatchedGroups::new(); - if Preg::is_match3( - php_regex!(r"{^\s*7-Zip(?:\s\[64\])?\s([0-9.]+)}"), - &output, - Some(&mut m), - ) { - let m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); - if version_compare(&m1, "21.01", CmpOp::Lt) { - self.inner.io.borrow().write_error(&format!( - " Unzipping using {} {} may result in incorrect file permissions. Install {} 21.01+ or unzip to ensure you get correct permissions.", - executable, m1, executable, - )); - } + let m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); + if version_compare(&m1, "21.01", CmpOp::Lt) { + self.inner.io.borrow().write_error(&format!( + " Unzipping using {} {} may result in incorrect file permissions. Install {} 21.01+ or unzip to ensure you get correct permissions.", + executable, m1, executable, + )); } } } -- cgit v1.3.1-4-g156e