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 | fed0a6e7ac361af9b963c1f62411b1a85478230c (patch) | |
| tree | 5cde64a24845c761890fbcbe05e0d702f1ec8df7 /crates/shirabe/src/repository | |
| parent | e093b2be1c333e67c96aebb0a5291bea9ae3d6db (diff) | |
| download | php-shirabe-fed0a6e7ac361af9b963c1f62411b1a85478230c.tar.gz php-shirabe-fed0a6e7ac361af9b963c1f62411b1a85478230c.tar.zst php-shirabe-fed0a6e7ac361af9b963c1f62411b1a85478230c.zip | |
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) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/repository')
15 files changed, 53 insertions, 61 deletions
diff --git a/crates/shirabe/src/repository/array_repository.rs b/crates/shirabe/src/repository/array_repository.rs index b475b0e0..12b9714e 100644 --- a/crates/shirabe/src/repository/array_repository.rs +++ b/crates/shirabe/src/repository/array_repository.rs @@ -12,7 +12,7 @@ use crate::repository::{ RepositoryInterfaceHandle, RepositoryInterfaceWeakHandle, SearchResult, }; use indexmap::IndexMap; -use shirabe_php_shim::{implode, php_regex, preg_match, preg_quote, preg_split, strtolower}; +use shirabe_php_shim::{implode, php_regex, preg_is_match, preg_quote, preg_split, strtolower}; use shirabe_semver::constraint::AnyConstraint; use shirabe_semver::constraint::SimpleConstraint; use std::rc::Weak; @@ -357,7 +357,7 @@ impl RepositoryInterface for ArrayRepository { let fulltext_match = mode == crate::repository::SEARCH_FULLTEXT && complete.is_some() - && preg_match( + && preg_is_match( ®ex, &format!( "{} {}", @@ -368,10 +368,9 @@ impl RepositoryInterface for ArrayRepository { .get_description() .unwrap_or_default() ), - ) - .is_some(); + ); - if preg_match(®ex, &name).is_some() || fulltext_match { + if preg_is_match(®ex, &name) || fulltext_match { if mode == crate::repository::SEARCH_VENDOR { matches.insert( name.clone(), diff --git a/crates/shirabe/src/repository/composer_repository.rs b/crates/shirabe/src/repository/composer_repository.rs index fbf9f767..79588fc5 100644 --- a/crates/shirabe/src/repository/composer_repository.rs +++ b/crates/shirabe/src/repository/composer_repository.rs @@ -43,7 +43,7 @@ use shirabe_php_shim::{ json_decode_assoc, parse_url, php_regex, preg_split, realpath, strtolower, strtr, urlencode, var_export, }; -use shirabe_php_shim::{Catch as _, preg_grep, preg_match, preg_replace}; +use shirabe_php_shim::{Catch as _, preg_grep, preg_is_match, preg_match, preg_replace}; use shirabe_semver::CompilingMatcher; use shirabe_semver::constraint::AnyConstraint; use shirabe_semver::constraint::MatchAllConstraint; @@ -161,7 +161,7 @@ impl ComposerRepository { .and_then(|v| v.as_string()) .unwrap_or("") .to_string(); - if preg_match(php_regex!(r"{^[\w.]+\??://}"), &url_str).is_none() { + if !preg_is_match(php_regex!(r"{^[\w.]+\??://}"), &url_str) { if let Some(local_file_path) = realpath(&url_str) { // it is a local path, add file scheme repo_config.insert( @@ -2708,7 +2708,7 @@ impl ComposerRepository { // url-encode $ signs in URLs as bad proxies choke on them if let Some(pos) = filename.find('$') && pos > 0 - && preg_match(php_regex!(r"{^https?://}i"), &filename).is_some() + && preg_is_match(php_regex!(r"{^https?://}i"), &filename) { filename = format!("{}%24{}", &filename[..pos], &filename[pos + 1..]); } @@ -3307,7 +3307,7 @@ impl ComposerRepository { if let Some(ref patterns) = self.available_package_patterns { for provider_regex in patterns.iter() { - if preg_match(provider_regex, name).is_some() { + if preg_is_match(provider_regex, name) { return Ok(true); } } diff --git a/crates/shirabe/src/repository/filter_repository.rs b/crates/shirabe/src/repository/filter_repository.rs index 77236a21..77c492fa 100644 --- a/crates/shirabe/src/repository/filter_repository.rs +++ b/crates/shirabe/src/repository/filter_repository.rs @@ -9,7 +9,7 @@ use crate::repository::{ RepositoryInterfaceHandle, SearchResult, }; use indexmap::IndexMap; -use shirabe_php_shim::{InvalidArgumentException, PhpMixed, preg_match}; +use shirabe_php_shim::{InvalidArgumentException, PhpMixed, preg_is_match}; use shirabe_semver::constraint::AnyConstraint; #[derive(Debug)] @@ -123,14 +123,14 @@ impl FilterRepository { } if let Some(only) = &self.only { - return preg_match(only, name).is_some(); + return preg_is_match(only, name); } if self.exclude.is_none() { return true; } - preg_match(self.exclude.as_ref().unwrap(), name).is_none() + !preg_is_match(self.exclude.as_ref().unwrap(), name) } } diff --git a/crates/shirabe/src/repository/path_repository.rs b/crates/shirabe/src/repository/path_repository.rs index 3d36f65c..05b68382 100644 --- a/crates/shirabe/src/repository/path_repository.rs +++ b/crates/shirabe/src/repository/path_repository.rs @@ -25,7 +25,7 @@ use crate::util::Url; use indexmap::IndexMap; use shirabe_php_shim::{ GLOB_BRACE, GLOB_MARK, GLOB_ONLYDIR, PhpMixed, RuntimeException, defined, file_exists, - file_get_contents, glob_with_flags, hash, php_regex, preg_match, realpath, serialize, + file_get_contents, glob_with_flags, hash, php_regex, preg_is_match, realpath, serialize, }; #[derive(Debug)] @@ -158,9 +158,9 @@ impl PathRepository { let url_matches = self.get_url_matches()?; if url_matches.is_empty() { - if preg_match(php_regex!(r"{[*{}]}"), &self.url).is_some() { + if preg_is_match(php_regex!(r"{[*{}]}"), &self.url) { let mut url = self.url.clone(); - while preg_match(php_regex!(r"{[*{}]}"), &url).is_some() { + while preg_is_match(php_regex!(r"{[*{}]}"), &url) { url = shirabe_php_shim::dirname(&url); } // the parent directory before any wildcard exists, so we assume it is correctly configured but simply empty diff --git a/crates/shirabe/src/repository/platform_repository.rs b/crates/shirabe/src/repository/platform_repository.rs index 839d17d6..30627dbf 100644 --- a/crates/shirabe/src/repository/platform_repository.rs +++ b/crates/shirabe/src/repository/platform_repository.rs @@ -19,8 +19,8 @@ use indexmap::IndexMap; use shirabe_php_rpc::PlatformInfo; use shirabe_php_shim::{ InvalidArgumentException, PhpMixed, UnexpectedValueException, array_map_str_fn, - array_slice_strs, explode, get_class, implode, is_string, php_regex, preg_match, preg_replace, - str_replace, strpos, strtolower, var_export, + array_slice_strs, explode, get_class, implode, is_string, php_regex, preg_is_match, preg_match, + preg_replace, str_replace, strpos, strtolower, var_export, }; use shirabe_semver::constraint::SimpleConstraint; use std::sync::{LazyLock, Mutex}; @@ -1604,7 +1604,7 @@ impl PlatformRepository { return cached; } - let result = preg_match(Self::PLATFORM_PACKAGE_REGEX, name).is_some(); + let result = preg_is_match(Self::PLATFORM_PACKAGE_REGEX, name); cache.insert(name.to_string(), result); result } diff --git a/crates/shirabe/src/repository/vcs/fossil_driver.rs b/crates/shirabe/src/repository/vcs/fossil_driver.rs index d5a9334f..9a678284 100644 --- a/crates/shirabe/src/repository/vcs/fossil_driver.rs +++ b/crates/shirabe/src/repository/vcs/fossil_driver.rs @@ -13,7 +13,7 @@ use chrono::{DateTime, FixedOffset, Utc}; use indexmap::IndexMap; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ - PhpMixed, RuntimeException, dirname, is_dir, is_file, is_writable, php_regex, preg_match, + PhpMixed, RuntimeException, dirname, is_dir, is_file, is_writable, php_regex, preg_is_match, preg_replace, }; @@ -301,16 +301,14 @@ impl FossilDriver { url: &str, deep: bool, ) -> anyhow::Result<bool> { - if preg_match( + if preg_is_match( php_regex!(r"#(^(?:https?|ssh)://(?:[^@]@)?(?:chiselapp\.com|fossil\.))#i"), url, - ) - .is_some() - { + ) { return Ok(true); } - if preg_match(php_regex!(r"!/fossil/|\.fossil!"), url).is_some() { + if preg_is_match(php_regex!(r"!/fossil/|\.fossil!"), url) { return Ok(true); } diff --git a/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs b/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs index a05e9835..b1960974 100644 --- a/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs +++ b/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs @@ -19,7 +19,7 @@ use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ InvalidArgumentException, LogicException, PhpMixed, RuntimeException, array_key_exists, array_search_mixed, extension_loaded, http_build_query, implode, is_array, php_regex, - preg_match, preg_replace, strpos, + preg_is_match, preg_match, preg_replace, strpos, }; #[derive(Debug)] @@ -797,12 +797,10 @@ impl GitBitbucketDriver { url: &str, _deep: bool, ) -> anyhow::Result<bool> { - if preg_match( + if !preg_is_match( php_regex!(r"#^https?://bitbucket\.org/([^/]+)/([^/]+?)(\.git|/?)?$#i"), url, - ) - .is_none() - { + ) { return Ok(false); } diff --git a/crates/shirabe/src/repository/vcs/git_driver.rs b/crates/shirabe/src/repository/vcs/git_driver.rs index 63598517..f45dbcaa 100644 --- a/crates/shirabe/src/repository/vcs/git_driver.rs +++ b/crates/shirabe/src/repository/vcs/git_driver.rs @@ -16,8 +16,8 @@ use chrono::{DateTime, FixedOffset, Utc}; use indexmap::IndexMap; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ - InvalidArgumentException, RuntimeException, dirname, is_dir, is_writable, preg_match, - preg_replace, realpath, sys_get_temp_dir, + InvalidArgumentException, RuntimeException, dirname, is_dir, is_writable, preg_is_match, + preg_match, preg_replace, realpath, sys_get_temp_dir, }; use shirabe_php_shim::{PhpMixed, php_regex}; @@ -98,7 +98,7 @@ impl GitDriver { .into()); } - if preg_match(php_regex!(r"{^ssh://[^@]+@[^:]+:[^0-9]+}"), &self.inner.url).is_some() { + if preg_is_match(php_regex!(r"{^ssh://[^@]+@[^:]+:[^0-9]+}"), &self.inner.url) { return Err(InvalidArgumentException::new(format!( "The source URL {} is invalid, ssh URLs should have a port number after \":\".\nUse ssh://git@example.com:22/path or just git@example.com:path if you do not want to provide a password or custom port.", self.inner.url @@ -343,7 +343,7 @@ impl GitDriver { ); for branch in self.inner.process.borrow().split_lines(&output) { if !branch.is_empty() - && preg_match(php_regex!(r"{^ *[^/]+/HEAD }"), &branch).is_none() + && !preg_is_match(php_regex!(r"{^ *[^/]+/HEAD }"), &branch) && let Some(caps) = preg_match( php_regex!(r"{^(?:\* )? *(\S+) *([a-f0-9]+)(?: .*)?$}"), &branch, @@ -367,12 +367,10 @@ impl GitDriver { url: &str, deep: bool, ) -> anyhow::Result<bool> { - if preg_match( + if preg_is_match( php_regex!(r"#(^git://|\.git/?$|git(?:olite)?@|//git\.|//github.com/)#i"), url, - ) - .is_some() - { + ) { return Ok(true); } diff --git a/crates/shirabe/src/repository/vcs/github_driver.rs b/crates/shirabe/src/repository/vcs/github_driver.rs index f310da3d..c41cc2d0 100644 --- a/crates/shirabe/src/repository/vcs/github_driver.rs +++ b/crates/shirabe/src/repository/vcs/github_driver.rs @@ -18,8 +18,8 @@ use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ InvalidArgumentException, PhpMixed, RuntimeException, array_diff, array_map, array_search_mixed, base64_decode, basename, empty, explode, extension_loaded, in_array_loose, - parse_url, php_regex, preg_match, preg_replace, preg_split, strpos, strtolower, substr, trim, - urlencode, + parse_url, php_regex, preg_is_match, preg_match, preg_replace, preg_split, strpos, strtolower, + substr, trim, urlencode, }; #[derive(Debug)] @@ -642,9 +642,7 @@ impl GitHubDriver { }; if bits.scheme.is_none() && bits.host.is_none() { - if preg_match(php_regex!(r"{^[a-z0-9-]++\.[a-z]{2,3}$}"), &item_url) - .is_some() - { + if preg_is_match(php_regex!(r"{^[a-z0-9-]++\.[a-z]{2,3}$}"), &item_url) { result[key_idx].insert( "url".to_string(), PhpMixed::String(format!("https://{}", item_url)), diff --git a/crates/shirabe/src/repository/vcs/gitlab_driver.rs b/crates/shirabe/src/repository/vcs/gitlab_driver.rs index 229d043b..b40297da 100644 --- a/crates/shirabe/src/repository/vcs/gitlab_driver.rs +++ b/crates/shirabe/src/repository/vcs/gitlab_driver.rs @@ -19,7 +19,7 @@ use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ InvalidArgumentException, LogicException, PhpMixed, RuntimeException, array_search_mixed, array_shift, ctype_alnum, empty, explode, extension_loaded, implode, in_array_loose, is_array, - is_string, ord, php_regex, preg_match, preg_replace, strpos, strtolower, + is_string, ord, php_regex, preg_is_match, preg_match, preg_replace, strpos, strtolower, }; /// Driver for GitLab API, use the Git driver for local checkouts. @@ -382,7 +382,7 @@ impl GitLabDriver { // Convert the root identifier to a cacheable commit id let mut identifier = identifier.to_string(); - if preg_match(php_regex!(r"{[a-f0-9]{40}}i"), &identifier).is_none() { + if !preg_is_match(php_regex!(r"{[a-f0-9]{40}}i"), &identifier) { let branches = self.get_branches()?; if let Some(sha) = branches.get(&identifier) { identifier = sha.clone(); diff --git a/crates/shirabe/src/repository/vcs/hg_driver.rs b/crates/shirabe/src/repository/vcs/hg_driver.rs index 977d6b97..54341070 100644 --- a/crates/shirabe/src/repository/vcs/hg_driver.rs +++ b/crates/shirabe/src/repository/vcs/hg_driver.rs @@ -13,7 +13,8 @@ use chrono::{DateTime, FixedOffset, Utc}; use indexmap::IndexMap; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ - PhpMixed, RuntimeException, dirname, is_dir, is_writable, php_regex, preg_match, preg_replace, + PhpMixed, RuntimeException, dirname, is_dir, is_writable, php_regex, preg_is_match, preg_match, + preg_replace, }; #[derive(Debug)] @@ -305,14 +306,12 @@ impl HgDriver { url: &str, deep: bool, ) -> anyhow::Result<bool> { - if preg_match( + if preg_is_match( php_regex!( r"#(^(?:https?|ssh)://(?:[^@]+@)?bitbucket.org|https://(?:.*?)\.kilnhg.com)#i" ), url, - ) - .is_some() - { + ) { return Ok(true); } diff --git a/crates/shirabe/src/repository/vcs/perforce_driver.rs b/crates/shirabe/src/repository/vcs/perforce_driver.rs index 16dad1e9..d55d361c 100644 --- a/crates/shirabe/src/repository/vcs/perforce_driver.rs +++ b/crates/shirabe/src/repository/vcs/perforce_driver.rs @@ -9,7 +9,9 @@ use crate::util::PerforceInterface; use crate::util::ProcessExecutor; use crate::util::http::Response; use indexmap::IndexMap; -use shirabe_php_shim::{BadMethodCallException, PhpMixed, RuntimeException, php_regex, preg_match}; +use shirabe_php_shim::{ + BadMethodCallException, PhpMixed, RuntimeException, php_regex, preg_is_match, +}; #[derive(Debug)] pub struct PerforceDriver { @@ -188,7 +190,7 @@ impl PerforceDriver { url: &str, deep: bool, ) -> anyhow::Result<bool> { - if deep || preg_match(php_regex!(r"#\b(perforce|p4)\b#i"), url).is_some() { + if deep || preg_is_match(php_regex!(r"#\b(perforce|p4)\b#i"), url) { return Ok(Perforce::check_server_exists( url, &mut ProcessExecutor::new(Some(io)), diff --git a/crates/shirabe/src/repository/vcs/svn_driver.rs b/crates/shirabe/src/repository/vcs/svn_driver.rs index 28239452..0431c33b 100644 --- a/crates/shirabe/src/repository/vcs/svn_driver.rs +++ b/crates/shirabe/src/repository/vcs/svn_driver.rs @@ -15,8 +15,8 @@ use chrono::{DateTime, FixedOffset, Utc}; use indexmap::IndexMap; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ - PhpMixed, RuntimeException, php_regex, preg_match, preg_replace, stripos, strrpos, strtr, - substr, trim, + PhpMixed, RuntimeException, php_regex, preg_is_match, preg_match, preg_replace, stripos, + strrpos, strtr, substr, trim, }; #[derive(Debug)] @@ -153,7 +153,7 @@ impl SvnDriver { } fn should_cache(&self, identifier: &str) -> bool { - self.inner.cache.is_some() && preg_match(php_regex!(r"{@\d+$}"), identifier).is_some() + self.inner.cache.is_some() && preg_is_match(php_regex!(r"{@\d+$}"), identifier) } pub fn get_composer_information( @@ -440,7 +440,7 @@ impl SvnDriver { deep: bool, ) -> anyhow::Result<bool> { let url = Self::normalize_url(url); - if preg_match(php_regex!(r"#(^svn://|^svn\+ssh://|svn\.)#i"), &url).is_some() { + if preg_is_match(php_regex!(r"#(^svn://|^svn\+ssh://|svn\.)#i"), &url) { return Ok(true); } diff --git a/crates/shirabe/src/repository/vcs/vcs_driver.rs b/crates/shirabe/src/repository/vcs/vcs_driver.rs index 439275f1..17616c36 100644 --- a/crates/shirabe/src/repository/vcs/vcs_driver.rs +++ b/crates/shirabe/src/repository/vcs/vcs_driver.rs @@ -13,7 +13,7 @@ use crate::util::http::Response; use chrono::{DateTime, FixedOffset}; use indexmap::IndexMap; use shirabe_php_shim::Catch as _; -use shirabe_php_shim::{DATE_RFC3339, PhpMixed, extension_loaded, php_regex, preg_match}; +use shirabe_php_shim::{DATE_RFC3339, PhpMixed, extension_loaded, php_regex, preg_is_match}; #[derive(Debug)] pub struct VcsDriverBase { @@ -56,7 +56,7 @@ impl VcsDriverBase { } pub fn should_cache(&self, identifier: &str) -> bool { - self.cache.is_some() && preg_match(php_regex!("{^[a-f0-9]{40}$}iD"), identifier).is_some() + self.cache.is_some() && preg_is_match(php_regex!("{^[a-f0-9]{40}$}iD"), identifier) } pub fn get_scheme(&self) -> &str { @@ -201,7 +201,7 @@ pub trait VcsDriver: VcsDriverInterface { fn cache_mut(&mut self) -> Option<&mut Cache>; fn should_cache(&self, identifier: &str) -> bool { - self.cache().is_some() && preg_match(php_regex!("{^[a-f0-9]{40}$}iD"), identifier).is_some() + self.cache().is_some() && preg_is_match(php_regex!("{^[a-f0-9]{40}$}iD"), identifier) } fn get_composer_information( diff --git a/crates/shirabe/src/repository/vcs_repository.rs b/crates/shirabe/src/repository/vcs_repository.rs index f22ab618..6cdc957a 100644 --- a/crates/shirabe/src/repository/vcs_repository.rs +++ b/crates/shirabe/src/repository/vcs_repository.rs @@ -29,8 +29,8 @@ use crate::util::Url; use indexmap::IndexMap; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ - InvalidArgumentException, PhpClass, PhpMixed, php_regex, preg_match, preg_replace, str_replace, - strpos, + InvalidArgumentException, PhpClass, PhpMixed, php_regex, preg_is_match, preg_replace, + str_replace, strpos, }; use shirabe_semver::constraint::SimpleConstraint; @@ -510,7 +510,7 @@ impl VcsRepository { // broken package, version doesn't match tag if version_normalized != parsed_tag { if is_very_verbose { - if preg_match(php_regex!(r"{(^dev-|[.-]?dev$)}i"), &parsed_tag).is_some() { + if preg_is_match(php_regex!(r"{(^dev-|[.-]?dev$)}i"), &parsed_tag) { self.io.write_error(&format!( "<warning>Skipped tag {}, invalid tag name, tags can not use dev prefixes or suffixes</warning>", tag |
