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/command | |
| 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/command')
| -rw-r--r-- | crates/shirabe/src/command/archive_command.rs | 10 | ||||
| -rw-r--r-- | crates/shirabe/src/command/bump_command.rs | 7 | ||||
| -rw-r--r-- | crates/shirabe/src/command/completion_trait.rs | 12 | ||||
| -rw-r--r-- | crates/shirabe/src/command/config_command.rs | 38 | ||||
| -rw-r--r-- | crates/shirabe/src/command/create_project_command.rs | 6 | ||||
| -rw-r--r-- | crates/shirabe/src/command/diagnose_command.rs | 14 | ||||
| -rw-r--r-- | crates/shirabe/src/command/fund_command.rs | 5 | ||||
| -rw-r--r-- | crates/shirabe/src/command/global_command.rs | 6 | ||||
| -rw-r--r-- | crates/shirabe/src/command/init_command.rs | 36 | ||||
| -rw-r--r-- | crates/shirabe/src/command/package_discovery_trait.rs | 10 | ||||
| -rw-r--r-- | crates/shirabe/src/command/reinstall_command.rs | 5 | ||||
| -rw-r--r-- | crates/shirabe/src/command/remove_command.rs | 7 | ||||
| -rw-r--r-- | crates/shirabe/src/command/repository_command.rs | 7 | ||||
| -rw-r--r-- | crates/shirabe/src/command/script_alias_command.rs | 5 | ||||
| -rw-r--r-- | crates/shirabe/src/command/show_command.rs | 17 | ||||
| -rw-r--r-- | crates/shirabe/src/command/update_command.rs | 13 |
16 files changed, 107 insertions, 91 deletions
diff --git a/crates/shirabe/src/command/archive_command.rs b/crates/shirabe/src/command/archive_command.rs index 31d25b3b..f2f26702 100644 --- a/crates/shirabe/src/command/archive_command.rs +++ b/crates/shirabe/src/command/archive_command.rs @@ -26,8 +26,7 @@ use crate::util::Platform; use crate::util::ProcessExecutor; use crate::util::r#loop::Loop; use indexmap::IndexMap; -use shirabe_pcre::Preg; -use shirabe_php_shim::{LogicException, get_debug_type, impl_php_class, php_regex}; +use shirabe_php_shim::{LogicException, get_debug_type, impl_php_class, php_regex, preg_match2}; use shirabe_symfony_console::command::Command; use shirabe_symfony_console::input::InputInterface; use shirabe_symfony_console::output::OutputInterface; @@ -229,8 +228,11 @@ impl ArchiveCommand { } if let Some(version_str) = &version - && let Some(matches) = - Preg::match3(php_regex!(r"{@(stable|RC|beta|alpha|dev)$}i"), version_str) + && let Some(matches) = preg_match2( + php_regex!(r"{@(stable|RC|beta|alpha|dev)$}i"), + version_str, + 0, + ) { let m1 = matches.get(1).unwrap_or_default().to_string(); let m0 = matches.get(0).unwrap_or_default().to_string(); diff --git a/crates/shirabe/src/command/bump_command.rs b/crates/shirabe/src/command/bump_command.rs index 87109d20..db4888c7 100644 --- a/crates/shirabe/src/command/bump_command.rs +++ b/crates/shirabe/src/command/bump_command.rs @@ -16,10 +16,9 @@ use crate::package::version::VersionBumper; use crate::repository::PlatformRepository; use crate::util::Filesystem; use crate::util::Silencer; -use shirabe_pcre::Preg; use shirabe_php_shim::{ PhpMixed, file_get_contents, file_put_contents, impl_php_class, is_writable, php_regex, - strtolower, + preg_match2, preg_replace, strtolower, }; use shirabe_symfony_console::command::Command; use shirabe_symfony_console::input::InputInterface; @@ -177,7 +176,7 @@ impl BumpCommand { let packages_filter = if !packages_filter.is_empty() { let packages_filter: Vec<String> = packages_filter .iter() - .map(|constraint| Preg::replace(php_regex!(r"{[:= ].+}"), "", constraint)) + .map(|constraint| preg_replace(php_regex!(r"{[:= ].+}"), "", constraint)) .collect(); let unique_lower: Vec<String> = packages_filter .iter() @@ -187,7 +186,7 @@ impl BumpCommand { .collect(); let pattern = base_package::package_names_to_regexp(&unique_lower, "{^(?:%s)$}iD"); for (key, reqs) in tasks.iter_mut() { - reqs.retain(|pkg_name, _| Preg::is_match(&pattern, pkg_name)); + reqs.retain(|pkg_name, _| preg_match2(&pattern, pkg_name, 0).is_some()); } packages_filter } else { diff --git a/crates/shirabe/src/command/completion_trait.rs b/crates/shirabe/src/command/completion_trait.rs index 491c5d07..ce0aef58 100644 --- a/crates/shirabe/src/command/completion_trait.rs +++ b/crates/shirabe/src/command/completion_trait.rs @@ -11,8 +11,7 @@ use crate::repository::RepositoryInterfaceHandle; use crate::repository::RootPackageRepository; use crate::repository::repository_interface::{SEARCH_NAME, SEARCH_VENDOR, SearchResult}; use indexmap::IndexMap; -use shirabe_pcre::Preg; -use shirabe_php_shim::{PhpMixed, php_regex, preg_quote}; +use shirabe_php_shim::{PhpMixed, php_regex, preg_match2, preg_quote}; /// Adds completion to arguments and options. /// @@ -257,10 +256,13 @@ pub trait CompletionTrait: BaseCommand { /// platform packages from the ones available on the currently-running PHP fn suggest_available_package_incl_platform(&self) -> SuggestedValues { SuggestedValues::Closure(Box::new(|this, input, suggestions| { - let matches = if Preg::is_match( + let matches = if preg_match2( php_regex!(r"{^(ext|lib|php)(-|$)|^com}"), &input.get_completion_value(), - ) { + 0, + ) + .is_some() + { this.suggest_platform_package() .call(this, input, suggestions)? } else { @@ -295,7 +297,7 @@ pub trait CompletionTrait: BaseCommand { let mut names: Vec<String> = vec![]; for package in repos.get_packages()? { let name = package.get_name(); - if Preg::is_match(pattern.clone(), &name) { + if preg_match2(pattern.clone(), &name, 0).is_some() { names.push(name); } } 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<String, (ValidatorFn, NormalizerFn)> ( 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(), ) diff --git a/crates/shirabe/src/command/create_project_command.rs b/crates/shirabe/src/command/create_project_command.rs index f1b899d3..fbdcfabb 100644 --- a/crates/shirabe/src/command/create_project_command.rs +++ b/crates/shirabe/src/command/create_project_command.rs @@ -37,12 +37,11 @@ use crate::util::Filesystem; use crate::util::Platform; use crate::util::ProcessExecutor; use indexmap::IndexMap; -use shirabe_pcre::Preg; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ InvalidArgumentException, PhpMixed, RuntimeException, UnexpectedValueException, array_pop, chdir, explode_with_limit, file_exists, getcwd, impl_php_class, implode, is_dir, is_file, - mkdir, realpath, rtrim, strtolower, unlink, + mkdir, preg_match2, realpath, rtrim, strtolower, unlink, }; use shirabe_symfony_console::command::Command; use shirabe_symfony_console::input::InputInterface; @@ -526,7 +525,7 @@ impl CreateProjectCommand { if package_version.is_none() { stability = Some("stable".to_string()); } else { - let matched = Preg::is_match3( + let matched = preg_match2( format!( "{{^[^,\\s]*?@({})$}}i", implode( @@ -538,6 +537,7 @@ impl CreateProjectCommand { ) ), package_version.as_deref().unwrap_or(""), + 0, ); if let Some(matched) = matched { stability = Some(matched.get(1).unwrap_or_default().to_string()); diff --git a/crates/shirabe/src/command/diagnose_command.rs b/crates/shirabe/src/command/diagnose_command.rs index 820b026c..f7f84575 100644 --- a/crates/shirabe/src/command/diagnose_command.rs +++ b/crates/shirabe/src/command/diagnose_command.rs @@ -33,13 +33,12 @@ use crate::util::ProcessExecutor; use crate::util::http::ProxyManager; use crate::util::http::RequestProxy; use indexmap::IndexMap; -use shirabe_pcre::Preg; -use shirabe_php_shim::Catch as _; +use shirabe_php_shim::PhpClass as _; use shirabe_php_shim::{ - AnyThrowable, CmpOp, InvalidArgumentException, PHP_EOL, PhpClass as _, PhpMixed, - RuntimeException, disk_free_space, file_exists, filter_var_boolean, hash, impl_php_class, - implode, is_array, is_string, php_regex, rtrim, str_replace, strpos, strstr, strstr3, - strtolower, trim, version_compare, + AnyThrowable, Catch as _, CmpOp, InvalidArgumentException, PHP_EOL, PhpMixed, RuntimeException, + disk_free_space, file_exists, filter_var_boolean, hash, impl_php_class, implode, is_array, + is_string, php_regex, preg_match2, rtrim, str_replace, strpos, strstr, strstr3, strtolower, + trim, version_compare, }; use shirabe_symfony_console::command::Command; use shirabe_symfony_console::input::InputInterface; @@ -862,9 +861,10 @@ impl DiagnoseCommand { warnings.insert("zlib".to_string(), PhpMixed::Bool(true)); } - if let Some(phpinfo_match) = Preg::is_match3( + if let Some(phpinfo_match) = preg_match2( php_regex!("{Configure Command(?: *</td><td class=\"v\">| *=> *)(.*?)(?:</td>|$)}m"), &diagnostics.phpinfo_general, + 0, ) { let configure = phpinfo_match.get(1).unwrap_or_default().to_string(); let configure = configure.as_str(); diff --git a/crates/shirabe/src/command/fund_command.rs b/crates/shirabe/src/command/fund_command.rs index f900bce0..ea077ef5 100644 --- a/crates/shirabe/src/command/fund_command.rs +++ b/crates/shirabe/src/command/fund_command.rs @@ -10,8 +10,7 @@ use crate::package::base_package::{self}; use crate::repository::CompositeRepository; use crate::repository::RepositoryInterface; use indexmap::IndexMap; -use shirabe_pcre::Preg; -use shirabe_php_shim::{PhpMixed, impl_php_class, php_regex}; +use shirabe_php_shim::{PhpMixed, impl_php_class, php_regex, preg_match2}; use shirabe_semver::constraint::AnyConstraint; use shirabe_semver::constraint::MatchAllConstraint; use shirabe_symfony_console::command::Command; @@ -64,7 +63,7 @@ impl FundCommand { .unwrap_or(""); if r#type == "github" && let Some(matches) = - Preg::is_match3(php_regex!(r"{^https://github.com/([^/]+)$}"), &url) + preg_match2(php_regex!(r"{^https://github.com/([^/]+)$}"), &url, 0) && let Some(sponsor) = matches.get(1).map(str::to_string) { url = format!("https://github.com/sponsors/{}", sponsor); diff --git a/crates/shirabe/src/command/global_command.rs b/crates/shirabe/src/command/global_command.rs index 21375141..f31d29c4 100644 --- a/crates/shirabe/src/command/global_command.rs +++ b/crates/shirabe/src/command/global_command.rs @@ -8,9 +8,8 @@ use crate::console::input::InputArgument; use crate::factory::Factory; use crate::util::Filesystem; use crate::util::Platform; -use shirabe_pcre::Preg; use shirabe_php_shim::{ - LogicException, RuntimeException, chdir, impl_php_class, php_regex, preg_split, + LogicException, RuntimeException, chdir, impl_php_class, php_regex, preg_replace2, preg_split, }; use shirabe_symfony_console::command::Command; use shirabe_symfony_console::completion::CompletionInput; @@ -105,11 +104,12 @@ impl GlobalCommand { )); } - let new_input_str = Preg::replace4( + let new_input_str = preg_replace2( php_regex!(r"{\bg(?:l(?:o(?:b(?:a(?:l)?)?)?)?)?\b}"), "", &Self::input_to_string(&*input.borrow())?, 1, + None, ); self.reset_composer()?; diff --git a/crates/shirabe/src/command/init_command.rs b/crates/shirabe/src/command/init_command.rs index 030a8657..96bdc58d 100644 --- a/crates/shirabe/src/command/init_command.rs +++ b/crates/shirabe/src/command/init_command.rs @@ -19,14 +19,13 @@ use crate::util::Filesystem; use crate::util::ProcessExecutor; use crate::util::Silencer; use indexmap::IndexMap; -use shirabe_pcre::{CaptureKey, Preg}; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ - FILE_IGNORE_NEW_LINES, InvalidArgumentException, PHP_EOL, PHP_SERVER, PhpMixed, + CaptureKey, FILE_IGNORE_NEW_LINES, InvalidArgumentException, PHP_EOL, PHP_SERVER, PhpMixed, array_flip_strings, array_intersect_key, array_map, basename, empty, explode, file, file_exists, file_get_contents, file_put_contents, get_current_user, impl_php_class, implode, - is_dir, is_string, php_regex, preg_quote, realpath, str_replace, strpos, strtolower, trim, - ucwords, + is_dir, is_string, php_regex, preg_match_all2, preg_match2, preg_quote, preg_replace, realpath, + str_replace, strpos, strtolower, trim, ucwords, }; use shirabe_spdx_licenses::SpdxLicenses; use shirabe_symfony_console::command::Command; @@ -90,9 +89,10 @@ impl InitCommand { &self, author: &str, ) -> anyhow::Result<IndexMap<String, Option<String>>> { - if let Some(m) = Preg::is_match3( + if let Some(m) = preg_match2( php_regex!(r#"/^(?P<name>[- .,\p{L}\p{N}\p{Mn}\'’\"()]+)(?:\s+<(?P<email>.+?)>)?$/u"#), author, + 0, ) { let email = m.name("email").map(str::to_string); if let Some(ref email) = email @@ -143,7 +143,7 @@ impl InitCommand { let namespace: Vec<String> = array_map( |part: &String| { - let part = Preg::replace(php_regex!(r"/[^a-z0-9]/i"), " ", part); + let part = preg_replace(php_regex!(r"/[^a-z0-9]/i"), " ", part); let part = ucwords(&part); str_replace(" ", "", &part) }, @@ -168,7 +168,7 @@ impl InitCommand { ) == 0 { *self.git_config.borrow_mut() = Some(IndexMap::new()); - let m = Preg::is_match_all(php_regex!(r"{^([^=]+)=(.*)$}m"), &output); + let m = preg_match_all2(php_regex!(r"{^([^=]+)=(.*)$}m"), &output); if m.occurrence_count() > 0 { let keys: Vec<Option<String>> = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); @@ -210,7 +210,7 @@ impl InitCommand { let lines = file(ignore_file, FILE_IGNORE_NEW_LINES).unwrap_or_default(); for line in &lines { - if Preg::is_match(&pattern, line) { + if preg_match2(&pattern, line, 0).is_some() { return true; } } @@ -329,15 +329,15 @@ impl InitCommand { } fn sanitize_package_name_component(&self, name: &str) -> String { - let name = Preg::replace( + let name = preg_replace( php_regex!(r"{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}"), "$1$3-$2$4", name, ); let name = strtolower(&name); - let name = Preg::replace(php_regex!(r"{^[_.-]+|[_.-]+$|[^a-z0-9_.-]}u"), "", &name); + let name = preg_replace(php_regex!(r"{^[_.-]+|[_.-]+$|[^a-z0-9_.-]}u"), "", &name); - Preg::replace(php_regex!(r"{([_.-]){2,}}u"), "$1", &name) + preg_replace(php_regex!(r"{([_.-]){2,}}u"), "$1", &name) } fn get_default_package_name(&self) -> String { @@ -498,13 +498,15 @@ impl Command for InitCommand { }); if options.contains_key("name") - && !Preg::is_match( + && preg_match2( php_regex!(r"{^[a-z0-9]([_.-]?[a-z0-9]+)*\/[a-z0-9](([_.]|-{1,2})?[a-z0-9]+)*$}D"), options .get("name") .and_then(|v| v.as_string()) .unwrap_or(""), + 0, ) + .is_none() { return Err(InvalidArgumentException::new(format!( "The package name {} is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+", @@ -908,10 +910,13 @@ impl Command for InitCommand { return Ok(PhpMixed::String(name_for_validate.clone())); } - if !Preg::is_match( + if preg_match2( php_regex!(r"{^[a-z0-9]([_.-]?[a-z0-9]+)*\/[a-z0-9](([_.]|-{1,2})?[a-z0-9]+)*$}D"), value.as_string().unwrap_or(""), - ) { + 0, + ) + .is_none() + { return Err(InvalidArgumentException::new(format!( "The package name {} is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+", value.as_string().unwrap_or("") @@ -1221,7 +1226,8 @@ impl Command for InitCommand { value_str }; - if !Preg::is_match(php_regex!(r"{^[^/][A-Za-z0-9\-_/]+/$}"), &value_or_default) + if preg_match2(php_regex!(r"{^[^/][A-Za-z0-9\-_/]+/$}"), &value_or_default, 0) + .is_none() { return Err(InvalidArgumentException::new(format!( "The src folder name \"{}\" is invalid. Please add a relative path with tailing forward slash. [A-Za-z0-9_-/]+/", diff --git a/crates/shirabe/src/command/package_discovery_trait.rs b/crates/shirabe/src/command/package_discovery_trait.rs index d31b5894..a5b606a8 100644 --- a/crates/shirabe/src/command/package_discovery_trait.rs +++ b/crates/shirabe/src/command/package_discovery_trait.rs @@ -19,12 +19,11 @@ use crate::repository::RepositorySet; use crate::repository::{RepositoryInterface, SearchResult}; use crate::util::Filesystem; use indexmap::IndexMap; -use shirabe_pcre::Preg; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ Exception, InvalidArgumentException, LogicException, PHP_EOL, PhpMixed, array_keys, array_slice, asort, explode, file_get_contents, implode, in_array_strict, is_array, is_file, - is_numeric, json_decode_assoc, levenshtein, php_regex, strlen, strpos, trim, + is_numeric, json_decode_assoc, levenshtein, php_regex, preg_match2, strlen, strpos, trim, }; use shirabe_symfony_console::input::InputInterface; use shirabe_symfony_console::output::OutputInterface; @@ -144,10 +143,12 @@ pub trait PackageDiscoveryTrait: BaseCommand { for mut requirement in requires_norm { if requirement.contains_key("version") - && Preg::is_match( + && preg_match2( php_regex!(r"{^\d+(\.\d+)?$}"), requirement.get("version").map(|s| s.as_str()).unwrap_or(""), + 0, ) + .is_some() { io.write_error3( &format!( @@ -330,9 +331,10 @@ pub trait PackageDiscoveryTrait: BaseCommand { } } - if let Some(m) = Preg::is_match3( + if let Some(m) = preg_match2( php_regex!(r"{^\s*(?P<name>[\S/]+)(?:\s+(?P<version>\S+))?\s*$}"), &selection, + 0, ) { if let Some(v) = m.name("version").map(str::to_string) { // parsing `acme/example ~2.3` diff --git a/crates/shirabe/src/command/reinstall_command.rs b/crates/shirabe/src/command/reinstall_command.rs index 9ef45fbf..05c277d5 100644 --- a/crates/shirabe/src/command/reinstall_command.rs +++ b/crates/shirabe/src/command/reinstall_command.rs @@ -15,8 +15,7 @@ use crate::plugin::CommandEvent; use crate::plugin::PluginEvents; use crate::script::ScriptEvents; use crate::util::Platform; -use shirabe_pcre::Preg; -use shirabe_php_shim::{InvalidArgumentException, impl_php_class}; +use shirabe_php_shim::{InvalidArgumentException, impl_php_class, preg_match2}; use shirabe_symfony_console::command::Command; use shirabe_symfony_console::input::InputInterface; use shirabe_symfony_console::output::OutputInterface; @@ -137,7 +136,7 @@ impl Command for ReinstallCommand { let pattern_regexp = base_package::package_name_to_regexp(pattern); let mut matched = false; for package in local_repo.get_canonical_packages()? { - if Preg::is_match(&pattern_regexp, &package.get_name()) { + if preg_match2(&pattern_regexp, &package.get_name(), 0).is_some() { matched = true; package_names_to_reinstall.push(package.get_name()); packages_to_reinstall.push(package); diff --git a/crates/shirabe/src/command/remove_command.rs b/crates/shirabe/src/command/remove_command.rs index bba0285f..7eb88f2b 100644 --- a/crates/shirabe/src/command/remove_command.rs +++ b/crates/shirabe/src/command/remove_command.rs @@ -17,8 +17,7 @@ use crate::json::JsonFile; use crate::package::base_package; use crate::repository::RepositoryInterface; use indexmap::IndexMap; -use shirabe_pcre::Preg; -use shirabe_php_shim::{PhpMixed, UnexpectedValueException, impl_php_class, strtolower}; +use shirabe_php_shim::{PhpMixed, UnexpectedValueException, impl_php_class, preg_grep, strtolower}; use shirabe_symfony_console::command::Command; use shirabe_symfony_console::exception::InvalidArgumentException; use shirabe_symfony_console::input::InputInterface; @@ -393,7 +392,7 @@ impl Command for RemoveCommand { .and_then(|v| v.as_array()) .map(|m| m.keys().cloned().collect()) .unwrap_or_default(); - let matches_in_type: Vec<&String> = Preg::grep( + let matches_in_type: Vec<&String> = preg_grep( base_package::package_name_to_regexp(package), type_keys.iter(), ) @@ -405,7 +404,7 @@ impl Command for RemoveCommand { .and_then(|v| v.as_array()) .map(|m| m.keys().cloned().collect()) .unwrap_or_default(); - let matches_in_alt_type: Vec<&String> = Preg::grep( + let matches_in_alt_type: Vec<&String> = preg_grep( base_package::package_name_to_regexp(package), alt_type_keys.iter(), ) diff --git a/crates/shirabe/src/command/repository_command.rs b/crates/shirabe/src/command/repository_command.rs index 4853d3d3..bbd1eace 100644 --- a/crates/shirabe/src/command/repository_command.rs +++ b/crates/shirabe/src/command/repository_command.rs @@ -10,10 +10,9 @@ use crate::console::input::InputOption; use crate::io::IOInterfaceImmutable; use crate::json::JsonFile; use indexmap::IndexMap; -use shirabe_pcre::Preg; use shirabe_php_shim::{ InvalidArgumentException, PhpMixed, RuntimeException, impl_php_class, parse_url, php_regex, - strtolower, + preg_match2, strtolower, }; use shirabe_symfony_console::command::Command; use shirabe_symfony_console::input::InputInterface; @@ -369,7 +368,9 @@ impl Command for RepositoryCommand { .into()); } let arg1_str = arg1.as_deref().unwrap(); - let repo_config: PhpMixed = if Preg::is_match(php_regex!(r"{^\s*\{}"), arg1_str) { + let repo_config: PhpMixed = if preg_match2(php_regex!(r"{^\s*\{}"), arg1_str, 0) + .is_some() + { JsonFile::parse_json(Some(arg1_str), None)? } else { if arg2.is_none() { diff --git a/crates/shirabe/src/command/script_alias_command.rs b/crates/shirabe/src/command/script_alias_command.rs index 30d82779..713947f4 100644 --- a/crates/shirabe/src/command/script_alias_command.rs +++ b/crates/shirabe/src/command/script_alias_command.rs @@ -6,9 +6,9 @@ use crate::command::base_command::base_command_initialize; use crate::console::input::InputArgument; use crate::console::input::InputOption; use crate::util::Platform; -use shirabe_pcre::Preg; use shirabe_php_shim::{ InvalidArgumentException, LogicException, PhpMixed, impl_php_class, is_string, php_regex, + preg_replace2, }; use shirabe_symfony_console::command::Command; use shirabe_symfony_console::input::InputInterface; @@ -134,7 +134,8 @@ impl Command for ScriptAliasCommand { // TODO(symfony): InputInterface lacks to_string; use a placeholder until it is modeled. let input_as_string = String::new(); let _ = input; - let script_alias_input = Preg::replace4(php_regex!(r"{^\S+ ?}"), "", &input_as_string, 1); + let script_alias_input = + preg_replace2(php_regex!(r"{^\S+ ?}"), "", &input_as_string, 1, None); let mut flags = indexmap::IndexMap::new(); flags.insert( "script-alias-input".to_string(), diff --git a/crates/shirabe/src/command/show_command.rs b/crates/shirabe/src/command/show_command.rs index 4be7873c..e8072ca8 100644 --- a/crates/shirabe/src/command/show_command.rs +++ b/crates/shirabe/src/command/show_command.rs @@ -36,11 +36,11 @@ use crate::repository::RepositoryUtils; use crate::repository::RootPackageRepository; use crate::util::PackageInfo; use indexmap::IndexMap; -use shirabe_pcre::Preg; use shirabe_php_shim::{ CmpOp, DATE_ATOM, InvalidArgumentException, LogicException, PhpMixed, UnexpectedValueException, array_search, date_format_to_strftime, date_local, extension_loaded, impl_php_class, - in_array_loose, in_array_strict, php_regex, preg_quote, realpath, strtolower, version_compare, + in_array_loose, in_array_strict, php_regex, preg_match2, preg_quote, preg_replace, realpath, + strtolower, version_compare, }; use shirabe_semver::Semver; use shirabe_semver::constraint::AnyConstraint; @@ -1373,9 +1373,10 @@ impl ShowCommand { if target_version.is_none() { if major_only - && let Some(groups) = Preg::is_match3( + && let Some(groups) = preg_match2( php_regex!(r"{^(?P<zero_major>(?:0\.)+)?(?P<first_meaningful>\d+)\.}"), &package.get_version(), + 0, ) { let zero_major = groups.name("zero_major").unwrap_or_default().to_string(); @@ -1398,7 +1399,7 @@ impl ShowCommand { if patch_only { let trimmed_version = - Preg::replace(php_regex!(r"{(\.0)+$}D"), "", &package.get_version()); + preg_replace(php_regex!(r"{(\.0)+$}D"), "", &package.get_version()); let parts_needed = if trimmed_version.starts_with('0') { 4 } else { @@ -2341,7 +2342,7 @@ impl Command for ShowCommand { } let matches_filter = match &package_filter_regex { None => true, - Some(r) => Preg::is_match(r, &p.get_name()), + Some(r) => preg_match2(r, &p.get_name(), 0).is_some(), }; if matches_filter { let matches_list = match &package_list_filter { @@ -2420,7 +2421,8 @@ impl Command for ShowCommand { if show_latest && *show_version { for package_or_name in type_packages.values() { if let PackageOrName::Pkg(package) = package_or_name - && !Preg::is_match(&ignored_packages_regex, &package.get_pretty_name()) + && preg_match2(&ignored_packages_regex, &package.get_pretty_name(), 0) + .is_none() { let latest = self.find_latest_package( package.clone(), @@ -2491,7 +2493,8 @@ impl Command for ShowCommand { package_is_up_to_date = package_is_up_to_date || (latest_package.is_none() && show_major_only); let package_is_ignored = - Preg::is_match(&ignored_packages_regex, &package.get_pretty_name()); + preg_match2(&ignored_packages_regex, &package.get_pretty_name(), 0) + .is_some(); if input.borrow().get_option("outdated")?.as_bool() == Some(true) && (package_is_up_to_date || package_is_ignored) { diff --git a/crates/shirabe/src/command/update_command.rs b/crates/shirabe/src/command/update_command.rs index e9f0e3ce..72d97315 100644 --- a/crates/shirabe/src/command/update_command.rs +++ b/crates/shirabe/src/command/update_command.rs @@ -27,10 +27,10 @@ use crate::repository::PlatformRepository; use crate::repository::RepositorySet; use crate::util::HttpDownloader; use indexmap::IndexMap; -use shirabe_pcre::Preg; use shirabe_php_shim::{ InvalidArgumentException, PhpMixed, RuntimeException, array_filter, array_intersect, - array_keys, array_merge_map, array_search_in_vec, impl_php_class, php_regex, strtolower, + array_keys, array_merge_map, array_search_in_vec, impl_php_class, php_regex, preg_match2, + preg_replace, strtolower, }; use shirabe_semver::Intervals; use shirabe_semver::constraint::MultiConstraint; @@ -115,7 +115,7 @@ impl UpdateCommand { let mut version_selector = self.create_version_selector(composer)?; for package in &installed_packages { if let Some(filter) = &filter - && !Preg::is_match(filter, &package.get_name()) + && preg_match2(filter, &package.get_name(), 0).is_none() { continue; } @@ -378,7 +378,7 @@ impl Command for UpdateCommand { if !packages.is_empty() { let allowlist_packages_with_requirements: Vec<String> = array_filter(&packages, |pkg: &String| -> bool { - Preg::is_match(php_regex!(r"{\S+[ =:]\S+}"), pkg) + preg_match2(php_regex!(r"{\S+[ =:]\S+}"), pkg, 0).is_some() }); for (package, constraint) in self.format_requirements(allowlist_packages_with_requirements.clone())? @@ -388,8 +388,7 @@ impl Command for UpdateCommand { // replace the foo/bar:req by foo/bar in the allowlist for package in &allowlist_packages_with_requirements { - let package_name = - Preg::replace(php_regex!(r"{^([^ =:]+)[ =:].*$}"), "$1", package); + let package_name = preg_replace(php_regex!(r"{^([^ =:]+)[ =:].*$}"), "$1", package); if let Some(idx) = array_search_in_vec(package, &packages) { packages[idx] = package_name; } @@ -460,7 +459,7 @@ impl Command for UpdateCommand { continue; } let version = package.get_version(); - let matches = Preg::is_match3(php_regex!(r"{^(\d+\.\d+\.\d+)}"), &version); + let matches = preg_match2(php_regex!(r"{^(\d+\.\d+\.\d+)}"), &version, 0); let Some(matches) = matches else { continue; }; |
