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/package/package.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'crates/shirabe/src/package/package.rs') diff --git a/crates/shirabe/src/package/package.rs b/crates/shirabe/src/package/package.rs index 2e2d5feb..ddeb2932 100644 --- a/crates/shirabe/src/package/package.rs +++ b/crates/shirabe/src/package/package.rs @@ -10,9 +10,9 @@ use crate::repository::RepositoryInterfaceWeakHandle; use crate::util::ComposerMirror; use chrono::{DateTime, Utc}; use indexmap::{IndexMap, IndexSet}; -use shirabe_pcre::Preg; use shirabe_php_shim::{ - E_USER_DEPRECATED, LogicException, PhpMixed, php_regex, strpos, trigger_error, + E_USER_DEPRECATED, LogicException, PhpMixed, PregMatches, php_regex, preg_match2, preg_replace, + preg_replace_callback, strpos, trigger_error, }; /// Mirror entry, e.g. `['url' => 'https://...', 'preferred' => true]`. @@ -139,7 +139,7 @@ impl Package { pub fn get_target_dir(&self) -> Option { let target_dir = self.target_dir.as_ref()?; - let replaced = Preg::replace( + let replaced = preg_replace( php_regex!("{ (?:^|[\\\\/]+) \\.\\.? (?:[\\\\/]+|$) (?:\\.\\.? (?:[\\\\/]+|$) )*}x"), "/", target_dir, @@ -416,12 +416,14 @@ impl Package { // only bitbucket, github and gitlab have auto generated dist URLs that easily allow replacing the reference in the dist URL // TODO generalize this a bit for self-managed/on-prem versions? Some kind of replace token in dist urls which allow this? if self.get_dist_url().is_some() - && Preg::is_match( + && preg_match2( php_regex!( "{^https?://(?:(?:www\\.)?bitbucket\\.org|(api\\.)?github\\.com|(?:www\\.)?gitlab\\.com)/}i" ), &self.get_dist_url().unwrap_or_default(), + 0, ) + .is_some() { self.set_dist_reference(Some(reference.clone())); // Regex pattern compatibility: @@ -431,14 +433,17 @@ impl Package { // lookaround, the capturing version consumes its boundary delimiter, so two 40-hex // SHAs sharing a single `/` between them would not both match; harmless here since a // dist URL never carries more than one SHA reference. - self.set_dist_url(Some(Preg::replace_callback( - php_regex!("{(/|sha=)[a-f0-9]{40}(/|$)}i"), - |m: &shirabe_pcre::PregMatches| -> String { - let get = |i: usize| -> String { m.get(i).unwrap_or_default().to_string() }; - format!("{}{}{}", get(1), reference, get(2)) - }, - &self.get_dist_url().unwrap_or_default(), - ))); + self.set_dist_url(Some( + preg_replace_callback( + php_regex!("{(/|sha=)[a-f0-9]{40}(/|$)}i"), + |m: &PregMatches| -> anyhow::Result { + let get = |i: usize| -> String { m.get(i).unwrap_or_default().to_string() }; + Ok(format!("{}{}{}", get(1), reference, get(2))) + }, + &self.get_dist_url().unwrap_or_default(), + ) + .expect("the replacement callback cannot fail"), + )); } else if self.get_dist_reference().is_some() { // update the dist reference if there was one, but if none was provided ignore it self.set_dist_reference(Some(reference)); -- cgit v1.3.1-4-g156e