From fed0a6e7ac361af9b963c1f62411b1a85478230c Mon Sep 17 00:00:00 2001 From: nsfisis Date: Tue, 18 Aug 2026 01:57:02 +0900 Subject: refactor(preg): add preg_is_match for existence-only call sites The capture groups were discarded at 162 of the preg_match call sites, which only tested the Option. They now call preg_is_match, which lets the regex engine skip capture tracking. Co-Authored-By: Claude Opus 5 (1M context) --- crates/shirabe/src/command/bump_command.rs | 4 ++-- crates/shirabe/src/command/completion_trait.rs | 10 ++++------ crates/shirabe/src/command/config_command.rs | 17 +++++++---------- crates/shirabe/src/command/init_command.rs | 21 +++++++++++---------- .../shirabe/src/command/package_discovery_trait.rs | 6 +++--- crates/shirabe/src/command/reinstall_command.rs | 4 ++-- crates/shirabe/src/command/repository_command.rs | 6 ++---- crates/shirabe/src/command/show_command.rs | 12 +++++------- crates/shirabe/src/command/update_command.rs | 8 ++++---- 9 files changed, 40 insertions(+), 48 deletions(-) (limited to 'crates/shirabe/src/command') diff --git a/crates/shirabe/src/command/bump_command.rs b/crates/shirabe/src/command/bump_command.rs index 3c75c937..3b680a29 100644 --- a/crates/shirabe/src/command/bump_command.rs +++ b/crates/shirabe/src/command/bump_command.rs @@ -18,7 +18,7 @@ use crate::util::Filesystem; use crate::util::Silencer; use shirabe_php_shim::{ PhpMixed, file_get_contents, file_put_contents, impl_php_class, is_writable, php_regex, - preg_match, preg_replace, strtolower, + preg_is_match, preg_replace, strtolower, }; use shirabe_symfony_console::command::Command; use shirabe_symfony_console::input::InputInterface; @@ -186,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_match(&pattern, pkg_name).is_some()); + reqs.retain(|pkg_name, _| preg_is_match(&pattern, pkg_name)); } packages_filter } else { diff --git a/crates/shirabe/src/command/completion_trait.rs b/crates/shirabe/src/command/completion_trait.rs index eda80164..851c62b8 100644 --- a/crates/shirabe/src/command/completion_trait.rs +++ b/crates/shirabe/src/command/completion_trait.rs @@ -11,7 +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_php_shim::{PhpMixed, php_regex, preg_match, preg_quote}; +use shirabe_php_shim::{PhpMixed, php_regex, preg_is_match, preg_quote}; /// Adds completion to arguments and options. /// @@ -256,12 +256,10 @@ 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_match( + let matches = if preg_is_match( php_regex!(r"{^(ext|lib|php)(-|$)|^com}"), &input.get_completion_value(), - ) - .is_some() - { + ) { this.suggest_platform_package() .call(this, input, suggestions)? } else { @@ -296,7 +294,7 @@ pub trait CompletionTrait: BaseCommand { let mut names: Vec = vec![]; for package in repos.get_packages()? { let name = package.get_name(); - if preg_match(pattern.clone(), &name).is_some() { + if preg_is_match(pattern.clone(), &name) { names.push(name); } } diff --git a/crates/shirabe/src/command/config_command.rs b/crates/shirabe/src/command/config_command.rs index f1eb14b0..0788d694 100644 --- a/crates/shirabe/src/command/config_command.rs +++ b/crates/shirabe/src/command/config_command.rs @@ -22,8 +22,8 @@ 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, preg_match, preg_replace, str_replace, strpos, strtolower, system, touch, - var_export, + php_regex, preg_is_match, preg_match, preg_replace, str_replace, strpos, strtolower, system, + touch, var_export, }; use shirabe_semver::VersionParser; use shirabe_symfony_console::command::Command; @@ -1449,7 +1449,7 @@ impl Command for ConfigCommand { } // Check if the header is in correct "Name: Value" format - if preg_match(php_regex!("/^[^:]+:\\s*.+$/"), header).is_none() { + if !preg_is_match(php_regex!("/^[^:]+:\\s*.+$/"), header) { return Err(RuntimeException::new(format!( "Header \"{}\" is not in \"Header-Name: Header-Value\" format", header @@ -1735,13 +1735,10 @@ fn build_unique_config_values() -> IndexMap "cache-files-maxsize".to_string(), ( Box::new(|val| { - PhpMixed::Bool( - preg_match( - php_regex!("/^\\s*([0-9.]+)\\s*(?:([kmg])(?:i?b)?)?\\s*$/i"), - val.as_string().unwrap_or(""), - ) - .is_some(), - ) + PhpMixed::Bool(preg_is_match( + php_regex!("/^\\s*([0-9.]+)\\s*(?:([kmg])(?:i?b)?)?\\s*$/i"), + val.as_string().unwrap_or(""), + )) }), Box::new(|val| val.clone()), ), diff --git a/crates/shirabe/src/command/init_command.rs b/crates/shirabe/src/command/init_command.rs index 3ca6c0fb..54ebc995 100644 --- a/crates/shirabe/src/command/init_command.rs +++ b/crates/shirabe/src/command/init_command.rs @@ -24,8 +24,8 @@ use shirabe_php_shim::{ 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_match, preg_match_all, preg_quote, preg_replace, realpath, - str_replace, strpos, strtolower, trim, ucwords, + is_dir, is_string, php_regex, preg_is_match, preg_match, preg_match_all, preg_quote, + preg_replace, realpath, str_replace, strpos, strtolower, trim, ucwords, }; use shirabe_spdx_licenses::SpdxLicenses; use shirabe_symfony_console::command::Command; @@ -209,7 +209,7 @@ impl InitCommand { let lines = file(ignore_file, FILE_IGNORE_NEW_LINES).unwrap_or_default(); for line in &lines { - if preg_match(&pattern, line).is_some() { + if preg_is_match(&pattern, line) { return true; } } @@ -497,14 +497,13 @@ impl Command for InitCommand { }); if options.contains_key("name") - && preg_match( + && !preg_is_match( 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(""), ) - .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,9 +907,12 @@ impl Command for InitCommand { return Ok(PhpMixed::String(name_for_validate.clone())); } - if preg_match(php_regex!(r"{^[a-z0-9]([_.-]?[a-z0-9]+)*\/[a-z0-9](([_.]|-{1,2})?[a-z0-9]+)*$}D"), value.as_string().unwrap_or("")) - .is_none() - { + if !preg_is_match( + php_regex!( + r"{^[a-z0-9]([_.-]?[a-z0-9]+)*\/[a-z0-9](([_.]|-{1,2})?[a-z0-9]+)*$}D" + ), + value.as_string().unwrap_or(""), + ) { 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("") @@ -1220,8 +1222,7 @@ impl Command for InitCommand { value_str }; - if preg_match(php_regex!(r"{^[^/][A-Za-z0-9\-_/]+/$}"), &value_or_default) - .is_none() + if !preg_is_match(php_regex!(r"{^[^/][A-Za-z0-9\-_/]+/$}"), &value_or_default) { 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 8e25c1b9..b743b5c1 100644 --- a/crates/shirabe/src/command/package_discovery_trait.rs +++ b/crates/shirabe/src/command/package_discovery_trait.rs @@ -23,7 +23,8 @@ 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, preg_match, strlen, strpos, trim, + is_numeric, json_decode_assoc, levenshtein, php_regex, preg_is_match, preg_match, strlen, + strpos, trim, }; use shirabe_symfony_console::input::InputInterface; use shirabe_symfony_console::output::OutputInterface; @@ -143,11 +144,10 @@ pub trait PackageDiscoveryTrait: BaseCommand { for mut requirement in requires_norm { if requirement.contains_key("version") - && preg_match( + && preg_is_match( php_regex!(r"{^\d+(\.\d+)?$}"), requirement.get("version").map(|s| s.as_str()).unwrap_or(""), ) - .is_some() { io.write_error3( &format!( diff --git a/crates/shirabe/src/command/reinstall_command.rs b/crates/shirabe/src/command/reinstall_command.rs index d0b36778..010d2b5d 100644 --- a/crates/shirabe/src/command/reinstall_command.rs +++ b/crates/shirabe/src/command/reinstall_command.rs @@ -15,7 +15,7 @@ use crate::plugin::CommandEvent; use crate::plugin::PluginEvents; use crate::script::ScriptEvents; use crate::util::Platform; -use shirabe_php_shim::{InvalidArgumentException, impl_php_class, preg_match}; +use shirabe_php_shim::{InvalidArgumentException, impl_php_class, preg_is_match}; use shirabe_symfony_console::command::Command; use shirabe_symfony_console::input::InputInterface; use shirabe_symfony_console::output::OutputInterface; @@ -136,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_match(&pattern_regexp, &package.get_name()).is_some() { + if preg_is_match(&pattern_regexp, &package.get_name()) { matched = true; package_names_to_reinstall.push(package.get_name()); packages_to_reinstall.push(package); diff --git a/crates/shirabe/src/command/repository_command.rs b/crates/shirabe/src/command/repository_command.rs index daeaaf74..8f68b16b 100644 --- a/crates/shirabe/src/command/repository_command.rs +++ b/crates/shirabe/src/command/repository_command.rs @@ -12,7 +12,7 @@ use crate::json::JsonFile; use indexmap::IndexMap; use shirabe_php_shim::{ InvalidArgumentException, PhpMixed, RuntimeException, impl_php_class, parse_url, php_regex, - preg_match, strtolower, + preg_is_match, strtolower, }; use shirabe_symfony_console::command::Command; use shirabe_symfony_console::input::InputInterface; @@ -368,9 +368,7 @@ impl Command for RepositoryCommand { .into()); } let arg1_str = arg1.as_deref().unwrap(); - let repo_config: PhpMixed = if preg_match(php_regex!(r"{^\s*\{}"), arg1_str) - .is_some() - { + let repo_config: PhpMixed = if preg_is_match(php_regex!(r"{^\s*\{}"), arg1_str) { JsonFile::parse_json(Some(arg1_str), None)? } else { if arg2.is_none() { diff --git a/crates/shirabe/src/command/show_command.rs b/crates/shirabe/src/command/show_command.rs index acb4f5c8..9c7bf47d 100644 --- a/crates/shirabe/src/command/show_command.rs +++ b/crates/shirabe/src/command/show_command.rs @@ -39,8 +39,8 @@ use indexmap::IndexMap; 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_match, preg_quote, preg_replace, realpath, - strtolower, version_compare, + in_array_loose, in_array_strict, php_regex, preg_is_match, preg_match, preg_quote, + preg_replace, realpath, strtolower, version_compare, }; use shirabe_semver::Semver; use shirabe_semver::constraint::AnyConstraint; @@ -2341,7 +2341,7 @@ impl Command for ShowCommand { } let matches_filter = match &package_filter_regex { None => true, - Some(r) => preg_match(r, &p.get_name()).is_some(), + Some(r) => preg_is_match(r, &p.get_name()), }; if matches_filter { let matches_list = match &package_list_filter { @@ -2420,8 +2420,7 @@ 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_match(&ignored_packages_regex, &package.get_pretty_name()) - .is_none() + && !preg_is_match(&ignored_packages_regex, &package.get_pretty_name()) { let latest = self.find_latest_package( package.clone(), @@ -2492,8 +2491,7 @@ 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_match(&ignored_packages_regex, &package.get_pretty_name()) - .is_some(); + preg_is_match(&ignored_packages_regex, &package.get_pretty_name()); 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 dcfbc371..213ac248 100644 --- a/crates/shirabe/src/command/update_command.rs +++ b/crates/shirabe/src/command/update_command.rs @@ -29,8 +29,8 @@ use crate::util::HttpDownloader; use indexmap::IndexMap; 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, preg_match, - preg_replace, strtolower, + array_keys, array_merge_map, array_search_in_vec, impl_php_class, php_regex, preg_is_match, + preg_match, 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_match(filter, &package.get_name()).is_none() + && !preg_is_match(filter, &package.get_name()) { continue; } @@ -378,7 +378,7 @@ impl Command for UpdateCommand { if !packages.is_empty() { let allowlist_packages_with_requirements: Vec = array_filter(&packages, |pkg: &String| -> bool { - preg_match(php_regex!(r"{\S+[ =:]\S+}"), pkg).is_some() + preg_is_match(php_regex!(r"{\S+[ =:]\S+}"), pkg) }); for (package, constraint) in self.format_requirements(allowlist_packages_with_requirements.clone())? -- cgit v1.3.1-4-g156e