diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-18 01:57:02 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-18 01:57:02 +0900 |
| commit | 530d085d4f3e19f94ac3cf8f8ac3b17000214b2e (patch) | |
| tree | b4de2c2443e2bb2cfc692454ac284dc1d2313e59 /crates/shirabe/src/util/process_executor.rs | |
| parent | 0caac63bacefb9a1f62848636d47fca07f592bba (diff) | |
| download | php-shirabe-530d085d4f3e19f94ac3cf8f8ac3b17000214b2e.tar.gz php-shirabe-530d085d4f3e19f94ac3cf8f8ac3b17000214b2e.tar.zst php-shirabe-530d085d4f3e19f94ac3cf8f8ac3b17000214b2e.zip | |
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) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/util/process_executor.rs')
| -rw-r--r-- | crates/shirabe/src/util/process_executor.rs | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/crates/shirabe/src/util/process_executor.rs b/crates/shirabe/src/util/process_executor.rs index 2bfaeb94..158729b4 100644 --- a/crates/shirabe/src/util/process_executor.rs +++ b/crates/shirabe/src/util/process_executor.rs @@ -7,13 +7,12 @@ use crate::signal::SignalSubscription; use crate::util::GitHub; use crate::util::Platform; use indexmap::IndexMap; -use shirabe_pcre::{Preg, PregMatches}; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ - LogicException, PHP_EOL, PhpMixed, RuntimeException, array_intersect, array_map, + LogicException, PHP_EOL, PhpMixed, PregMatches, RuntimeException, array_intersect, array_map, escapeshellarg, explode, implode, in_array_strict, is_array, is_dir, is_numeric, is_string, - php_regex, preg_split, rtrim, str_replace, strcspn, strlen, strpbrk, strtolower, strtr_array, - substr_replace, trim, + php_regex, preg_match2, preg_replace, preg_replace_callback, preg_replace2, preg_split, rtrim, + str_replace, strcspn, strlen, strpbrk, strtolower, strtr_array, substr_replace, trim, }; use shirabe_symfony_process::ExecutableFinder; use shirabe_symfony_process::Process; @@ -217,7 +216,7 @@ impl ProcessExecutor { if is_string(&command) { let mut command_str = command.as_string().unwrap_or("").to_string(); if Platform::is_windows() - && let Some(m) = Preg::is_match3(php_regex!(r"{^([^:/\\]++) }"), &command_str) + && let Some(m) = preg_match2(php_regex!(r"{^([^:/\\]++) }"), &command_str, 0) { let m1 = m.get(1).unwrap_or_default().to_string(); command_str = substr_replace( @@ -829,25 +828,31 @@ impl ProcessExecutor { } else { String::new() }; - let safe_command = Preg::replace_callback( + let safe_command = preg_replace_callback( php_regex!(r"{://(?P<user>[^:/\s]+):(?P<password>[^@\s/]+)@}i"), - |m: &PregMatches| -> String { + |m: &PregMatches| -> anyhow::Result<String> { // if the username looks like a long (12char+) hex string, or a modern github token (e.g. ghp_xxx, github_pat_xxx) we obfuscate that - if Preg::is_match( + if preg_match2( GitHub::GITHUB_TOKEN_REGEX, m.name("user").unwrap_or_default(), - ) { - return "://***:***@".to_string(); + 0, + ) + .is_some() + { + return Ok("://***:***@".to_string()); } - if Preg::is_match(r"{^[a-f0-9]{12,}$}", m.name("user").unwrap_or_default()) { - return "://***:***@".to_string(); + if preg_match2(r"{^[a-f0-9]{12,}$}", m.name("user").unwrap_or_default(), 0) + .is_some() + { + return Ok("://***:***@".to_string()); } - format!("://{}:***@", m.name("user").unwrap_or_default()) + Ok(format!("://{}:***@", m.name("user").unwrap_or_default())) }, &command_string, - ); - let safe_command = Preg::replace( + ) + .expect("the replacement callback cannot fail"); + let safe_command = preg_replace( php_regex!(r"{--password (.*[^\\]') }"), "--password '***' ", &safe_command, @@ -894,26 +899,27 @@ impl ProcessExecutor { let mut quote = strpbrk(&argument, " \t,").is_some(); let mut dquotes: usize = 0; // PHP: Preg::replace('/(\\\\*)"/', '$1$1\\"', $argument, -1, $dquotes) - argument = Preg::replace5( + argument = preg_replace2( php_regex!(r#"/(\\*)"/"#), r#"$1$1\""#, &argument, -1, - &mut dquotes, + Some(&mut dquotes), ); - let meta = dquotes > 0 || Preg::is_match(php_regex!(r"/%[^%]+%|![^!]+!/"), &argument); + let meta = + dquotes > 0 || preg_match2(php_regex!(r"/%[^%]+%|![^!]+!/"), &argument, 0).is_some(); if !meta && !quote { quote = strpbrk(&argument, "^&|<>()").is_some(); } if quote { - argument = format!("\"{}\"", Preg::replace(r"/(\\*)$/", "$1$1", &argument)); + argument = format!("\"{}\"", preg_replace(r"/(\\*)$/", "$1$1", &argument)); } if meta { - argument = Preg::replace(php_regex!(r#"/(["^&|<>()%])/"#), "^$1", &argument); - argument = Preg::replace(php_regex!(r"/(!)/"), "^^$1", &argument); + argument = preg_replace(php_regex!(r#"/(["^&|<>()%])/"#), "^$1", &argument); + argument = preg_replace(php_regex!(r"/(!)/"), "^^$1", &argument); } argument |
