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/util/filesystem.rs | 40 +++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'crates/shirabe/src/util/filesystem.rs') diff --git a/crates/shirabe/src/util/filesystem.rs b/crates/shirabe/src/util/filesystem.rs index 2ef51ba1..b79beeae 100644 --- a/crates/shirabe/src/util/filesystem.rs +++ b/crates/shirabe/src/util/filesystem.rs @@ -3,14 +3,14 @@ use crate::util::Platform; use crate::util::ProcessExecutor; use crate::util::Silencer; -use shirabe_pcre::Preg; use shirabe_php_shim::{ - ErrorException, LogicException, PhpMixed, RuntimeException, array_pop, basename, chdir, - clearstatcache, clearstatcache2, copy, dirname, explode, fclose, feof, file_exists, + ErrorException, LogicException, PhpMixed, PregMatches, RuntimeException, array_pop, basename, + chdir, clearstatcache, clearstatcache2, copy, dirname, explode, fclose, feof, file_exists, file_get_contents, file_put_contents, fileatime, filemtime, filesize, fopen, fread, function_exists, fwrite, implode, is_dir, is_file, is_link, is_readable, lstat, mkdir, - php_regex, rename, rmdir, rtrim, str_repeat, str_replace, strlen, strpos, strtoupper, strtr, - substr, substr_count, symlink, touch, unlink, usleep, var_export, + php_regex, preg_match2, preg_replace, preg_replace_callback, rename, rmdir, rtrim, str_repeat, + str_replace, strlen, strpos, strtoupper, strtr, substr, substr_count, symlink, touch, unlink, + usleep, var_export, }; use shirabe_symfony_filesystem::exception::IOException; use shirabe_symfony_finder::Finder; @@ -246,7 +246,7 @@ impl Filesystem { return Ok(Some(true)); } - if Preg::is_match3(php_regex!("{^(?:[a-z]:)?[/\\\\]+$}i"), directory).is_some() { + if preg_match2(php_regex!("{^(?:[a-z]:)?[/\\\\]+$}i"), directory, 0).is_some() { return Err(RuntimeException::new(format!("Aborting an attempted deletion of {}, this was probably not intended, if it is a real use case please report it.", directory)) .into()); } @@ -578,7 +578,7 @@ impl Filesystem { let mut common_path = to.clone(); while strpos(&format!("{}/", from), &format!("{}/", common_path)) != Some(0) && "/" != common_path - && Preg::is_match3(php_regex!("{^[A-Z]:/?$}i"), &common_path).is_none() + && preg_match2(php_regex!("{^[A-Z]:/?$}i"), &common_path, 0).is_none() { common_path = strtr(&dirname(&common_path), "\\", "/"); } @@ -635,7 +635,7 @@ impl Filesystem { let mut common_path = to.clone(); while strpos(&format!("{}/", from), &format!("{}/", common_path)) != Some(0) && "/" != common_path - && Preg::is_match3(php_regex!("{^[A-Z]:/?$}i"), &common_path).is_none() + && preg_match2(php_regex!("{^[A-Z]:/?$}i"), &common_path, 0).is_none() && "." != common_path { common_path = strtr(&dirname(&common_path), "\\", "/"); @@ -735,9 +735,10 @@ impl Filesystem { } // extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive: - if let Some(prefix_match) = Preg::is_match3( + if let Some(prefix_match) = preg_match2( php_regex!("{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix"), &path, + 0, ) { prefix = prefix_match.get(1).unwrap_or_default().to_string(); path = substr(&path, strlen(&prefix), None); @@ -760,14 +761,15 @@ impl Filesystem { } // ensure c: is normalized to C: - prefix = Preg::replace_callback( + prefix = preg_replace_callback( php_regex!("{(^|://)[a-z]:$}i"), - |m: &shirabe_pcre::PregMatches| -> String { + |m: &PregMatches| -> anyhow::Result { let s = m.get(0).unwrap_or_default().to_string(); - strtoupper(&s) + Ok(strtoupper(&s)) }, &prefix, - ); + ) + .expect("the replacement callback cannot fail"); format!("{}{}{}", prefix, absolute, implode("/", &parts)) } @@ -777,7 +779,7 @@ impl Filesystem { /// And other possible unforeseen disasters, see https://github.com/composer/composer/pull/9422 pub fn trim_trailing_slash(path: &str) -> String { let mut path = path.to_string(); - if Preg::is_match3(php_regex!("{^[/\\\\]+$}"), &path).is_none() { + if preg_match2(php_regex!("{^[/\\\\]+$}"), &path, 0).is_none() { path = rtrim(&path, Some("/\\")); } @@ -789,18 +791,20 @@ impl Filesystem { // on windows, \\foo indicates network paths so we exclude those from local paths, however it is unsafe // on linux as file:////foo (which would be a network path \\foo on windows) will resolve to /foo which could be a local path if Platform::is_windows() { - return Preg::is_match3( + return preg_match2( php_regex!( "{^(file://(?!//)|/(?!/)|/?[a-z]:[\\\\/]|\\.\\.[\\\\/]|[a-z0-9_.-]+[\\\\/])}i" ), path, + 0, ) .is_some(); } - Preg::is_match3( + preg_match2( php_regex!("{^(file://|/|/?[a-z]:[\\\\/]|\\.\\.[\\\\/]|[a-z0-9_.-]+[\\\\/])}i"), path, + 0, ) .is_some() } @@ -808,14 +812,14 @@ impl Filesystem { pub fn get_platform_path(path: &str) -> String { let mut path = path.to_string(); if Platform::is_windows() { - path = Preg::replace( + path = preg_replace( php_regex!("{^(?:file:///([a-z]):?/)}i"), "file://$1:/", &path, ); } - Preg::replace(php_regex!("{^file://}i"), "", &path) + preg_replace(php_regex!("{^file://}i"), "", &path) } /// Cross-platform safe version of is_readable() -- cgit v1.3.1-4-g156e