From 530d085d4f3e19f94ac3cf8f8ac3b17000214b2e Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 18 Aug 2026 01:57:02 +0900 Subject: refactor(pcre): inline Preg into its call sites and drop the crate Preg had shed everything it owned: after the last few rounds its methods were one-line forwards to the shim's preg_*(), differing only in a default argument or a wrapper the caller unwrapped anyway. The 460 call sites now name the shim function, and shirabe-pcre is gone from the workspace along with its LICENSE entry. The forwards expand as they read: isMatch becomes preg_match2(.., 0).is_some() (is_none() where PHP negates it), isMatch3 and match3 drop the .is_some(), matchAll counts through preg_match_all2(..).occurrence_count(), and replace4/replace5 spell out the limit and count arguments preg_replace2 takes. Callbacks are the one place the shapes differ: preg_replace_callback carries an error out of the callback, so the fourteen infallible closures wrap their result in Ok() and expect() it back. Config::process() is the fifteenth, and it drops the `error` cell it captured to smuggle a failure past a closure that could only return a String. The `?` in the closure now carries it, which is what the PHP does -- a throw from the callback leaves preg_replace_callback at the failing match rather than running the remaining replacements and reporting the last error. The module doc that explained why composer/pcre's exceptions and *StrictGroups() variants have no counterpart moves to the shim's preg module, where the functions it describes live. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe/src/downloader/download_manager.rs | 7 ++-- crates/shirabe/src/downloader/fossil_downloader.rs | 7 ++-- crates/shirabe/src/downloader/git_downloader.rs | 47 +++++++++++++--------- crates/shirabe/src/downloader/svn_downloader.rs | 27 +++++++------ crates/shirabe/src/downloader/zip_downloader.rs | 12 +++--- 5 files changed, 55 insertions(+), 45 deletions(-) (limited to 'crates/shirabe/src/downloader') diff --git a/crates/shirabe/src/downloader/download_manager.rs b/crates/shirabe/src/downloader/download_manager.rs index 15b009e6..f2360f9d 100644 --- a/crates/shirabe/src/downloader/download_manager.rs +++ b/crates/shirabe/src/downloader/download_manager.rs @@ -8,12 +8,11 @@ use crate::io::io_interface; use crate::package::PackageInterfaceHandle; use crate::util::Filesystem; use indexmap::IndexMap; -use shirabe_pcre::Preg; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ InvalidArgumentException, LogicException, PhpMixed, RuntimeException, array_keys, - array_reverse, array_shift, dirname, implode, in_array_strict, preg_quote, rtrim, str_replace, - strtolower, usort, + array_reverse, array_shift, dirname, implode, in_array_strict, preg_match2, preg_quote, rtrim, + str_replace, strtolower, usort, }; /// Downloaders manager. @@ -431,7 +430,7 @@ impl DownloadManager { "{{^{}$}}i", str_replace("\\*", ".*", &preg_quote(pattern, None)), ); - if Preg::is_match(&pattern_regex, &package.get_name()) { + if preg_match2(&pattern_regex, &package.get_name(), 0).is_some() { if "dist" == preference || (!package.is_dev() && "auto" == preference) { return "dist".to_string(); } diff --git a/crates/shirabe/src/downloader/fossil_downloader.rs b/crates/shirabe/src/downloader/fossil_downloader.rs index cdc5f268..e7b3a20d 100644 --- a/crates/shirabe/src/downloader/fossil_downloader.rs +++ b/crates/shirabe/src/downloader/fossil_downloader.rs @@ -12,8 +12,9 @@ use crate::package::PackageInterfaceHandle; use crate::util::Filesystem; use crate::util::ProcessExecutor; use indexmap::IndexMap; -use shirabe_pcre::Preg; -use shirabe_php_shim::{PhpMixed, RuntimeException, impl_php_class, php_regex, preg_split}; +use shirabe_php_shim::{ + PhpMixed, RuntimeException, impl_php_class, php_regex, preg_match2, preg_split, +}; #[derive(Debug)] pub struct FossilDownloader { @@ -227,7 +228,7 @@ impl VcsDownloader for FossilDownloader { }; for line in lines { - if Preg::is_match(&match_pattern, &line) { + if preg_match2(&match_pattern, &line, 0).is_some() { break; } log.push_str(&line); diff --git a/crates/shirabe/src/downloader/git_downloader.rs b/crates/shirabe/src/downloader/git_downloader.rs index b816aa18..d3dea453 100644 --- a/crates/shirabe/src/downloader/git_downloader.rs +++ b/crates/shirabe/src/downloader/git_downloader.rs @@ -17,11 +17,10 @@ use crate::util::Platform; use crate::util::ProcessExecutor; use crate::util::Url; use indexmap::IndexMap; -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, - substr, trim, version_compare, + CaptureKey, CmpOp, PhpMixed, RuntimeException, array_map, basename, dirname, impl_php_class, + implode, in_array_strict, is_dir, php_regex, preg_match_all2, preg_match2, preg_quote, + preg_replace, preg_split, realpath, rtrim, strlen, strpos, substr, trim, version_compare, }; #[derive(Debug)] @@ -95,13 +94,13 @@ impl GitDownloader { } let mut refs = trim(&output, None); - let Some(head_match) = Preg::is_match3(php_regex!(r"{^([a-f0-9]+) HEAD$}mi"), &refs) else { + let Some(head_match) = preg_match2(php_regex!(r"{^([a-f0-9]+) HEAD$}mi"), &refs, 0) else { // could not match the HEAD for some reason return Ok(None); }; let head_ref = head_match.get(1).unwrap_or_default().to_string(); - let branches_match = Preg::is_match_all( + let branches_match = preg_match_all2( format!("{{^{} refs/heads/(.+)$}}mi", preg_quote(&head_ref, None)), &refs, ); @@ -128,7 +127,7 @@ impl GitDownloader { // try to find matching branch names in remote repos for candidate in &candidate_branches { - let m = Preg::is_match_all( + let m = preg_match_all2( format!( "{{^[a-f0-9]+ refs/remotes/((?:[^/]+)/{})$}}mi", preg_quote(candidate, None) @@ -275,7 +274,7 @@ impl GitDownloader { // If the non-existent branch is actually the name of a file, the file // is checked out. - let mut branch = Preg::replace( + let mut branch = preg_replace( php_regex!(r"{(?:^dev-|(?:\.x)?-dev$)}i"), "", pretty_version, @@ -299,12 +298,14 @@ impl GitDownloader { // check whether non-commitish are branches or tags, and fetch branches with the remote name let git_ref = reference.to_string(); - if !Preg::is_match(php_regex!(r"{^[a-f0-9]{40}$}"), reference) + if preg_match2(php_regex!(r"{^[a-f0-9]{40}$}"), reference, 0).is_none() && branches.is_some() - && Preg::is_match( + && preg_match2( format!("{{^\\s+composer/{}$}}m", preg_quote(reference, None)), branches.as_deref().unwrap_or(""), + 0, ) + .is_some() { let mut command1: Vec = vec!["git".to_string(), "checkout".to_string()]; command1.extend(force.clone()); @@ -345,17 +346,21 @@ impl GitDownloader { } // try to checkout branch by name and then reset it so it's on the proper branch name - if Preg::is_match(php_regex!(r"{^[a-f0-9]{40}$}"), reference) { + if preg_match2(php_regex!(r"{^[a-f0-9]{40}$}"), reference, 0).is_some() { // add 'v' in front of the branch if it was stripped when generating the pretty name if branches.is_some() - && !Preg::is_match( + && preg_match2( format!("{{^\\s+composer/{}$}}m", preg_quote(&branch, None)), branches.as_deref().unwrap_or(""), + 0, ) - && Preg::is_match( + .is_none() + && preg_match2( format!("{{^\\s+composer/v{}$}}m", preg_quote(&branch, None)), branches.as_deref().unwrap_or(""), + 0, ) + .is_some() { branch = format!("v{}", branch); } @@ -500,12 +505,13 @@ impl GitDownloader { fn set_push_url(&self, path: &str, url: &str) { // set push url for github projects - if let Some(match_) = Preg::is_match3( + if let Some(match_) = preg_match2( format!( "{{^(?:https?|git)://{}/([^/]+)/([^/]+?)(?:\\.git)?$}}", GitUtil::get_github_domains_regex(&self.inner.config.borrow()) ), url, + 0, ) { let protocols = self.inner.config.borrow_mut().get("github-protocols"); let m1 = match_.get(1).unwrap_or_default().to_string(); @@ -640,7 +646,8 @@ impl GitDownloader { } fn get_short_hash(&self, reference: &str) -> String { - if !self.inner.io.is_verbose() && Preg::is_match(php_regex!(r"{^[0-9a-f]{40}$}"), reference) + if !self.inner.io.is_verbose() + && preg_match2(php_regex!(r"{^[0-9a-f]{40}$}"), reference, 0).is_some() { return substr(reference, 0, Some(10)); } @@ -769,7 +776,7 @@ impl VcsDownloader for GitDownloader { .get("cache-vcs-dir") .as_string() .unwrap_or(""), - Preg::replace(r"{[^a-z0-9.]}i", "-", &Url::sanitize(url.to_string())), + preg_replace(r"{[^a-z0-9.]}i", "-", &Url::sanitize(url.to_string())), ); let git_version = GitUtil::get_version(&self.inner.process); @@ -834,7 +841,7 @@ impl VcsDownloader for GitDownloader { .get("cache-vcs-dir") .as_string() .unwrap_or(""), - Preg::replace(r"{[^a-z0-9.]}i", "-", &Url::sanitize(url.to_string())), + preg_replace(r"{[^a-z0-9.]}i", "-", &Url::sanitize(url.to_string())), ); let r#ref = package.get_source_reference().unwrap_or_default(); @@ -994,7 +1001,7 @@ impl VcsDownloader for GitDownloader { .get("cache-vcs-dir") .as_string() .unwrap_or(""), - Preg::replace(r"{[^a-z0-9.]}i", "-", &Url::sanitize(url.to_string())), + preg_replace(r"{[^a-z0-9.]}i", "-", &Url::sanitize(url.to_string())), ); let r#ref = target.get_source_reference().unwrap_or_default(); @@ -1094,9 +1101,9 @@ impl VcsDownloader for GitDownloader { Some(&path), ) == 0 && let Some(origin_match) = - Preg::is_match3(php_regex!(r"{^origin\s+(?P\S+)}m"), &output) + preg_match2(php_regex!(r"{^origin\s+(?P\S+)}m"), &output, 0) && let Some(composer_match) = - Preg::is_match3(php_regex!(r"{^composer\s+(?P\S+)}m"), &output) + preg_match2(php_regex!(r"{^composer\s+(?P\S+)}m"), &output, 0) { let origin_url = origin_match.name("url").unwrap_or_default().to_string(); let composer_url = composer_match.name("url").unwrap_or_default().to_string(); diff --git a/crates/shirabe/src/downloader/svn_downloader.rs b/crates/shirabe/src/downloader/svn_downloader.rs index 4931e539..a31e89c9 100644 --- a/crates/shirabe/src/downloader/svn_downloader.rs +++ b/crates/shirabe/src/downloader/svn_downloader.rs @@ -15,10 +15,9 @@ use crate::util::Filesystem; use crate::util::ProcessExecutor; use crate::util::Svn as SvnUtil; use indexmap::IndexMap; -use shirabe_pcre::Preg; use shirabe_php_shim::{ - CmpOp, PhpMixed, RuntimeException, impl_php_class, is_dir, php_regex, preg_split, - version_compare, + CmpOp, PhpMixed, RuntimeException, impl_php_class, is_dir, php_regex, preg_match2, + preg_replace, preg_split, version_compare, }; #[derive(Debug)] @@ -354,8 +353,8 @@ impl VcsDownloader for SvnDownloader { to_reference: &str, path: &str, ) -> anyhow::Result { - if Preg::is_match(php_regex!(r"{@(\d+)$}"), from_reference) - && Preg::is_match(php_regex!(r"{@(\d+)$}"), to_reference) + if preg_match2(php_regex!(r"{@(\d+)$}"), from_reference, 0).is_some() + && preg_match2(php_regex!(r"{@(\d+)$}"), to_reference, 0).is_some() { // retrieve the svn base url from the checkout folder let command = vec![ @@ -383,7 +382,7 @@ impl VcsDownloader for SvnDownloader { } let url_pattern = "#(.*)#"; - let base_url = if let Some(matches) = Preg::match3(url_pattern, &output) { + let base_url = if let Some(matches) = preg_match2(url_pattern, &output, 0) { matches.get(1).unwrap_or_default().to_string() } else { return Err(RuntimeException::new(format!( @@ -394,8 +393,8 @@ impl VcsDownloader for SvnDownloader { }; // strip paths from references and only keep the actual revision - let from_revision = Preg::replace(php_regex!(r"{.*@(\d+)$}"), "$1", from_reference); - let to_revision = Preg::replace(php_regex!(r"{.*@(\d+)$}"), "$1", to_reference); + let from_revision = preg_replace(php_regex!(r"{.*@(\d+)$}"), "$1", from_reference); + let to_revision = preg_replace(php_regex!(r"{.*@(\d+)$}"), "$1", to_reference); let command = vec![ "svn".to_string(), @@ -453,11 +452,13 @@ impl ChangeReportInterface for SvnDownloader { Some(path), ); - Ok(if Preg::is_match(php_regex!("{^ *[^X ] +}m"), &output) { - Some(output) - } else { - None - }) + Ok( + if preg_match2(php_regex!("{^ *[^X ] +}m"), &output, 0).is_some() { + Some(output) + } else { + None + }, + ) } } diff --git a/crates/shirabe/src/downloader/zip_downloader.rs b/crates/shirabe/src/downloader/zip_downloader.rs index b4802066..7b0ad050 100644 --- a/crates/shirabe/src/downloader/zip_downloader.rs +++ b/crates/shirabe/src/downloader/zip_downloader.rs @@ -8,13 +8,12 @@ use crate::package::PackageInterfaceHandle; use crate::util::IniHelper; use crate::util::Platform; use indexmap::IndexMap; -use shirabe_pcre::Preg; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ CmpOp, ErrorException, PhpMixed, RuntimeException, UnexpectedValueException, ZipArchive, bin2hex, class_exists, file_exists, file_get_contents, filesize, function_exists, hash_file, - impl_php_class, is_file, json_encode, php_regex, random_int, str_replace, strlen, substr, - version_compare, + impl_php_class, is_file, json_encode, php_regex, preg_match2, random_int, str_replace, strlen, + substr, version_compare, }; use shirabe_symfony_process::ExecutableFinder; use std::sync::Mutex; @@ -113,8 +112,11 @@ 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 Some(m) = preg_match2( + php_regex!(r"{^\s*7-Zip(?:\s\[64\])?\s([0-9.]+)}"), + &output, + 0, + ) { let m1 = m.get(1).unwrap_or_default().to_string(); if version_compare(&m1, "21.01", CmpOp::Lt) { -- cgit v1.3.1-4-g156e