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/command/config_command.rs | 38 +++++++++++++++------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'crates/shirabe/src/command/config_command.rs') diff --git a/crates/shirabe/src/command/config_command.rs b/crates/shirabe/src/command/config_command.rs index 8f0cbc51..7bf091a1 100644 --- a/crates/shirabe/src/command/config_command.rs +++ b/crates/shirabe/src/command/config_command.rs @@ -18,12 +18,12 @@ use crate::util::Filesystem; use crate::util::Platform; use crate::util::Silencer; use indexmap::IndexMap; -use shirabe_pcre::Preg; use shirabe_php_shim::{ InvalidArgumentException, PhpMixed, RuntimeException, array_is_list, array_merge, escapeshellcmd, exec, explode, file_exists, impl_php_class, implode, in_array_loose, in_array_strict, is_array, is_bool, is_dir, is_numeric, is_object, is_string, json_encode, - php_regex, str_replace, strpos, strtolower, system, touch, var_export, + php_regex, preg_match2, preg_replace, str_replace, strpos, strtolower, system, touch, + var_export, }; use shirabe_semver::VersionParser; use shirabe_symfony_console::command::Command; @@ -206,7 +206,7 @@ impl ConfigCommand { || (key == "repositories" && k.is_none())) { let mut new_k = k.clone().unwrap_or_default(); - new_k.push_str(&Preg::replace( + new_k.push_str(&preg_replace( php_regex!("{^config\\.}"), "", &format!("{}.", key), @@ -269,13 +269,13 @@ impl ConfigCommand { } else { k.clone().unwrap() }; - let id = Preg::replace(php_regex!("{\\..*$}"), "", &id_source); - let id = Preg::replace( + let id = preg_replace(php_regex!("{\\..*$}"), "", &id_source); + let id = preg_replace( php_regex!("{[^a-z0-9]}i"), "-", &strtolower(&shirabe_php_shim::trim(&id, Some(" \t\n\r\0\u{0B}"))), ); - let id = Preg::replace(php_regex!("{-+}"), "-", &id); + let id = preg_replace(php_regex!("{-+}"), "-", &id); format!("https://getcomposer.org/doc/06-config.md#{}", id) }; if is_string(&raw_val) @@ -701,9 +701,10 @@ impl Command for ConfigCommand { let mut source = config.borrow_mut().get_source_of_value(&setting_key); let mut value: PhpMixed; - if let Some(matches) = Preg::is_match3( + if let Some(matches) = preg_match2( php_regex!("/^repos?(?:itories)?(?:\\.(.+))?/"), &setting_key, + 0, ) { if matches.get(1).is_none() { value = data @@ -925,7 +926,7 @@ impl Command for ConfigCommand { } // handle preferred-install per-package config if let Some(matches) = - Preg::is_match3(php_regex!("/^preferred-install\\.(.+)/"), &setting_key) + preg_match2(php_regex!("/^preferred-install\\.(.+)/"), &setting_key, 0) { if input.borrow().get_option("unset")?.as_bool() == Some(true) { self.config_source @@ -959,9 +960,10 @@ impl Command for ConfigCommand { } // handle allow-plugins config setting elements true or false to add/remove - if let Some(matches) = Preg::is_match3( + if let Some(matches) = preg_match2( php_regex!("{^allow-plugins\\.([a-zA-Z0-9/*-]+)}"), &setting_key, + 0, ) { if input.borrow().get_option("unset")?.as_bool() == Some(true) { self.config_source @@ -1028,7 +1030,7 @@ impl Command for ConfigCommand { // handle repositories if let Some(matches) = - Preg::is_match3(php_regex!("/^repos?(?:itories)?\\.(.+)/"), &setting_key) + preg_match2(php_regex!("/^repos?(?:itories)?\\.(.+)/"), &setting_key, 0) { if input.borrow().get_option("unset")?.as_bool() == Some(true) { self.config_source @@ -1097,7 +1099,7 @@ impl Command for ConfigCommand { } // handle extra - if let Some(matches) = Preg::is_match3(php_regex!("/^extra\\.(.+)/"), &setting_key) { + if let Some(matches) = preg_match2(php_regex!("/^extra\\.(.+)/"), &setting_key, 0) { if input.borrow().get_option("unset")?.as_bool() == Some(true) { self.config_source .borrow_mut() @@ -1169,7 +1171,7 @@ impl Command for ConfigCommand { } // handle suggest - if let Some(matches) = Preg::is_match3(php_regex!("/^suggest\\.(.+)/"), &setting_key) { + if let Some(matches) = preg_match2(php_regex!("/^suggest\\.(.+)/"), &setting_key, 0) { if input.borrow().get_option("unset")?.as_bool() == Some(true) { self.config_source .borrow_mut() @@ -1203,7 +1205,7 @@ impl Command for ConfigCommand { } // handle platform - if let Some(matches) = Preg::is_match3(php_regex!("/^platform\\.(.+)/"), &setting_key) { + if let Some(matches) = preg_match2(php_regex!("/^platform\\.(.+)/"), &setting_key, 0) { if input.borrow().get_option("unset")?.as_bool() == Some(true) { self.config_source .borrow_mut() @@ -1320,11 +1322,12 @@ impl Command for ConfigCommand { } // handle auth - if let Some(matches) = Preg::is_match3( + if let Some(matches) = preg_match2( php_regex!( "/^(bitbucket-oauth|github-oauth|gitlab-oauth|gitlab-token|http-basic|custom-headers|bearer|forgejo-token)\\.(.+)/" ), &setting_key, + 0, ) { if input.borrow().get_option("unset")?.as_bool() == Some(true) { self.auth_config_source @@ -1452,7 +1455,7 @@ impl Command for ConfigCommand { } // Check if the header is in correct "Name: Value" format - if Preg::is_match3(php_regex!("/^[^:]+:\\s*.+$/"), header).is_none() { + if preg_match2(php_regex!("/^[^:]+:\\s*.+$/"), header, 0).is_none() { return Err(RuntimeException::new(format!( "Header \"{}\" is not in \"Header-Name: Header-Value\" format", header @@ -1500,7 +1503,7 @@ impl Command for ConfigCommand { } // handle script - if let Some(matches) = Preg::is_match3(php_regex!("/^scripts\\.(.+)/"), &setting_key) { + if let Some(matches) = preg_match2(php_regex!("/^scripts\\.(.+)/"), &setting_key, 0) { if input.borrow().get_option("unset")?.as_bool() == Some(true) { self.config_source .borrow_mut() @@ -1739,9 +1742,10 @@ fn build_unique_config_values() -> IndexMap ( Box::new(|val| { PhpMixed::Bool( - Preg::is_match3( + preg_match2( php_regex!("/^\\s*([0-9.]+)\\s*(?:([kmg])(?:i?b)?)?\\s*$/i"), val.as_string().unwrap_or(""), + 0, ) .is_some(), ) -- cgit v1.3.1-4-g156e