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/package | |
| 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/package')
11 files changed, 51 insertions, 48 deletions
diff --git a/crates/shirabe/src/package/archiver/archive_manager.rs b/crates/shirabe/src/package/archiver/archive_manager.rs index 83d5cc94..ceda099b 100644 --- a/crates/shirabe/src/package/archiver/archive_manager.rs +++ b/crates/shirabe/src/package/archiver/archive_manager.rs @@ -11,7 +11,7 @@ use crate::util::SyncHelper; use crate::util::r#loop::Loop; use indexmap::IndexMap; use shirabe_php_shim::{ - InvalidArgumentException, RuntimeException, bin2hex, file_exists, php_regex, preg_match, + InvalidArgumentException, RuntimeException, bin2hex, file_exists, php_regex, preg_is_match, preg_replace, random_bytes, realpath, sys_get_temp_dir, }; @@ -65,7 +65,7 @@ impl ArchiveManager { let dist_reference = package.get_dist_reference(); if let Some(ref dist_ref) = dist_reference { - if preg_match(php_regex!("{^[a-f0-9]{40}$}"), dist_ref).is_some() { + if preg_is_match(php_regex!("{^[a-f0-9]{40}$}"), dist_ref) { parts.insert("dist_reference".to_string(), dist_ref.to_string()); if let Some(dist_type) = package.get_dist_type() { parts.insert("dist_type".to_string(), dist_type); diff --git a/crates/shirabe/src/package/archiver/base_exclude_filter.rs b/crates/shirabe/src/package/archiver/base_exclude_filter.rs index 8186e95c..d036623c 100644 --- a/crates/shirabe/src/package/archiver/base_exclude_filter.rs +++ b/crates/shirabe/src/package/archiver/base_exclude_filter.rs @@ -1,6 +1,6 @@ //! ref: composer/src/Composer/Package/Archiver/BaseExcludeFilter.php -use shirabe_php_shim::preg_match; +use shirabe_php_shim::preg_is_match; use shirabe_symfony_finder::Glob; #[derive(Debug)] @@ -86,7 +86,7 @@ pub trait BaseExcludeFilter { relative_path }; - if preg_match(pattern, path).is_some() { + if preg_is_match(pattern, path) { exclude = !negate; } } diff --git a/crates/shirabe/src/package/loader/array_loader.rs b/crates/shirabe/src/package/loader/array_loader.rs index 00f9026e..698e1f03 100644 --- a/crates/shirabe/src/package/loader/array_loader.rs +++ b/crates/shirabe/src/package/loader/array_loader.rs @@ -19,8 +19,8 @@ use chrono::Utc; use indexmap::IndexMap; use shirabe_php_shim::{ AnyThrowable, E_USER_DEPRECATED, PhpMixed, UnexpectedValueException, is_scalar, is_string, - json_encode, ltrim, php_regex, preg_match, preg_replace, stripos, strpos, strtolower, strval, - substr, trigger_error, trim, + json_encode, ltrim, php_regex, preg_is_match, preg_replace, stripos, strpos, strtolower, + strval, substr, trigger_error, trim, }; #[derive(Debug)] @@ -339,7 +339,7 @@ impl ArrayLoader { && !shirabe_php_shim::empty(time_value) { let time_str = time_value.as_string().unwrap_or(""); - let time = if preg_match(php_regex!(r"/^\d++$/D"), time_str).is_some() { + let time = if preg_is_match(php_regex!(r"/^\d++$/D"), time_str) { format!("@{}", time_str) } else { time_str.to_string() diff --git a/crates/shirabe/src/package/loader/root_package_loader.rs b/crates/shirabe/src/package/loader/root_package_loader.rs index 491961a6..ece29b41 100644 --- a/crates/shirabe/src/package/loader/root_package_loader.rs +++ b/crates/shirabe/src/package/loader/root_package_loader.rs @@ -16,8 +16,8 @@ use crate::util::Platform; use crate::util::ProcessExecutor; use indexmap::IndexMap; use shirabe_php_shim::{ - PhpMixed, RuntimeException, UnexpectedValueException, php_regex, preg_match, preg_replace, - preg_split, strtolower, + PhpMixed, RuntimeException, UnexpectedValueException, php_regex, preg_is_match, preg_match, + preg_replace, preg_split, strtolower, }; #[derive(Debug)] @@ -337,7 +337,7 @@ impl RootPackageLoader { for constraint in &constraints { let req_version_stripped = preg_replace(php_regex!(r"{^([^,\s@]+) as .+$}"), "$1", constraint); - if preg_match(php_regex!(r"{^[^,\s@]+$}"), &req_version_stripped).is_some() { + if preg_is_match(php_regex!(r"{^[^,\s@]+$}"), &req_version_stripped) { let stability_name = VersionParser::parse_stability(&req_version_stripped); if stability_name != "stable" { let name = strtolower(req_name); diff --git a/crates/shirabe/src/package/loader/validating_array_loader.rs b/crates/shirabe/src/package/loader/validating_array_loader.rs index e893756a..70c09014 100644 --- a/crates/shirabe/src/package/loader/validating_array_loader.rs +++ b/crates/shirabe/src/package/loader/validating_array_loader.rs @@ -10,7 +10,7 @@ use indexmap::IndexMap; use shirabe_php_shim::{ CmpOp, E_USER_DEPRECATED, PHP_EOL, PhpMixed, array_intersect_key, array_values, filter_var_email, get_debug_type, is_array, is_bool, is_int, is_numeric, is_scalar, is_string, - json_encode, parse_url, php_regex, php_to_string, preg_match, preg_replace, str_replace, + json_encode, parse_url, php_regex, php_to_string, preg_is_match, preg_replace, str_replace, strcasecmp, strtolower, strtotime, substr, trigger_error, trim, var_export, }; use shirabe_semver::Intervals; @@ -73,14 +73,12 @@ impl ValidatingArrayLoader { return None; } - if preg_match( + if !preg_is_match( php_regex!( "{^[a-z0-9](?:[_.-]?[a-z0-9]++)*+/[a-z0-9](?:(?:[_.]|-{1,2})?[a-z0-9]++)*+$}iD" ), name, - ) - .is_none() - { + ) { return Some(format!( "{} is invalid, it should have a vendor name, a forward slash, and a package name. The vendor and package name can be words separated by -, . or _. The complete name should match \"^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{{0,2}})[a-z0-9]+)*$\".", name @@ -101,14 +99,14 @@ impl ValidatingArrayLoader { )); } - if preg_match(php_regex!("{\\.json$}"), name).is_some() { + if preg_is_match(php_regex!("{\\.json$}"), name) { return Some(format!( "{} is invalid, package names can not end in .json, consider renaming it or perhaps using a -json suffix instead.", name )); } - if preg_match(php_regex!("{[A-Z]}"), name).is_some() { + if preg_is_match(php_regex!("{[A-Z]}"), name) { if is_link { return Some(format!( "{} is invalid, it should not contain uppercase characters. Please use {} instead.", @@ -142,7 +140,7 @@ impl ValidatingArrayLoader { .as_string() .unwrap_or("") .to_string(); - if preg_match(format!("{{^{}$}}u", regex), &value).is_none() { + if !preg_is_match(format!("{{^{}$}}u", regex), &value) { let message = format!( "{} : invalid value ({}), must match {}", property, value, regex @@ -257,7 +255,7 @@ impl ValidatingArrayLoader { if let Some(regex_str) = regex { let value_str = php_to_string(&value); - if preg_match(format!("{{^{}$}}u", regex_str), &value_str).is_none() { + if !preg_is_match(format!("{{^{}$}}u", regex_str), &value_str) { self.warnings.borrow_mut().push(format!( "{}.{} : invalid value ({}), must match {}", property, key, value_str, regex_str @@ -1186,7 +1184,7 @@ impl LoaderInterface for ValidatingArrayLoader { self.warnings .borrow_mut() .push(format!("{}.{}", link_type, err)); - } else if preg_match(php_regex!("{^[A-Za-z0-9_./-]+$}"), &package).is_none() { + } else if !preg_is_match(php_regex!("{^[A-Za-z0-9_./-]+$}"), &package) { self.errors.borrow_mut().push(format!( "{}.{} : invalid key, package names must be strings containing only [A-Za-z0-9_./-]", link_type, package @@ -1449,7 +1447,7 @@ impl LoaderInterface for ValidatingArrayLoader { } if let Some(ref_val) = section.get("reference").filter(|_| isset("reference")) { let ref_str = php_to_string(ref_val); - if preg_match(php_regex!("{^\\s*-}"), &ref_str).is_some() { + if preg_is_match(php_regex!("{^\\s*-}"), &ref_str) { self.errors.borrow_mut().push(format!( "{}.reference : must not start with a \"-\", \"{}\" given", src_type, ref_str @@ -1458,7 +1456,7 @@ impl LoaderInterface for ValidatingArrayLoader { } if let Some(url_val) = section.get("url").filter(|_| isset("url")) { let url_str = php_to_string(url_val); - if preg_match(php_regex!("{^\\s*-}"), &url_str).is_some() { + if preg_is_match(php_regex!("{^\\s*-}"), &url_str) { self.errors.borrow_mut().push(format!( "{}.url : must not start with a \"-\", \"{}\" given", src_type, url_str diff --git a/crates/shirabe/src/package/locker.rs b/crates/shirabe/src/package/locker.rs index 017bcb6a..ec8d86e7 100644 --- a/crates/shirabe/src/package/locker.rs +++ b/crates/shirabe/src/package/locker.rs @@ -28,7 +28,8 @@ use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ DATE_RFC3339, LogicException, PhpMixed, RuntimeException, array_intersect, array_keys, array_map, array_merge, file_get_contents, filemtime, function_exists, hash, in_array_loose, - is_int, ksort, php_regex, preg_match, realpath, strcmp, strtolower, touch2, trim, usort, + is_int, ksort, php_regex, preg_is_match, preg_match, realpath, strcmp, strtolower, touch2, + trim, usort, }; use shirabe_seld_json_lint::ParsingException; @@ -823,7 +824,7 @@ impl Locker { ), None, ); - if preg_match(php_regex!(r"{^\s*\d+\s*$}"), &output_str).is_some() { + if preg_is_match(php_regex!(r"{^\s*\d+\s*$}"), &output_str) { let ts = trim(&output_str, None).parse::<i64>().unwrap_or(0); datetime = chrono::DateTime::from_timestamp(ts, 0); } diff --git a/crates/shirabe/src/package/package.rs b/crates/shirabe/src/package/package.rs index cbc027ba..388280fd 100644 --- a/crates/shirabe/src/package/package.rs +++ b/crates/shirabe/src/package/package.rs @@ -11,8 +11,8 @@ use crate::util::ComposerMirror; use chrono::{DateTime, Utc}; use indexmap::{IndexMap, IndexSet}; use shirabe_php_shim::{ - E_USER_DEPRECATED, LogicException, PhpMixed, PregMatches, php_regex, preg_match, preg_replace, - preg_replace_callback, strpos, trigger_error, + E_USER_DEPRECATED, LogicException, PhpMixed, PregMatches, php_regex, preg_is_match, + preg_replace, preg_replace_callback, strpos, trigger_error, }; /// Mirror entry, e.g. `['url' => 'https://...', 'preferred' => true]`. @@ -416,10 +416,12 @@ impl Package { // only bitbucket, github and gitlab have auto generated dist URLs that easily allow replacing the reference in the dist URL // TODO generalize this a bit for self-managed/on-prem versions? Some kind of replace token in dist urls which allow this? if self.get_dist_url().is_some() - && preg_match(php_regex!( - "{^https?://(?:(?:www\\.)?bitbucket\\.org|(api\\.)?github\\.com|(?:www\\.)?gitlab\\.com)/}i" - ), &self.get_dist_url().unwrap_or_default()) - .is_some() + && preg_is_match( + php_regex!( + "{^https?://(?:(?:www\\.)?bitbucket\\.org|(api\\.)?github\\.com|(?:www\\.)?gitlab\\.com)/}i" + ), + &self.get_dist_url().unwrap_or_default(), + ) { self.set_dist_reference(Some(reference.clone())); // Regex pattern compatibility: diff --git a/crates/shirabe/src/package/version/version_bumper.rs b/crates/shirabe/src/package/version/version_bumper.rs index e75dea33..075663d4 100644 --- a/crates/shirabe/src/package/version/version_bumper.rs +++ b/crates/shirabe/src/package/version/version_bumper.rs @@ -6,7 +6,7 @@ use crate::package::loader::ArrayLoader; use crate::package::version::VersionParser; use crate::util::Platform; use shirabe_php_shim::{ - CaptureKey, php_regex, preg_match, preg_match_all_offset_capture, preg_replace, + CaptureKey, php_regex, preg_is_match, preg_match_all_offset_capture, preg_replace, }; use shirabe_semver::Intervals; use shirabe_semver::constraint::AnyConstraint; @@ -51,7 +51,7 @@ impl VersionBumper { preg_replace(php_regex!(r"{(?:\.(?:0|9999999))+(-dev)?$}"), "", &version); let new_pretty_constraint = format!("^{}", version_without_suffix); - if preg_match(php_regex!(r"{^\^\d+(\.\d+)*$}"), &new_pretty_constraint).is_none() { + if !preg_is_match(php_regex!(r"{^\^\d+(\.\d+)*$}"), &new_pretty_constraint) { return Ok(pretty_constraint); } diff --git a/crates/shirabe/src/package/version/version_guesser.rs b/crates/shirabe/src/package/version/version_guesser.rs index 668fce02..3c47d5b2 100644 --- a/crates/shirabe/src/package/version/version_guesser.rs +++ b/crates/shirabe/src/package/version/version_guesser.rs @@ -14,8 +14,8 @@ use crate::util::sync_executor; use indexmap::IndexMap; use shirabe_php_shim::{ PhpMixed, RuntimeException, array_keys, array_map, array_merge, empty, function_exists, - implode, is_string, json_encode, php_regex, preg_match, preg_quote, preg_replace, str_replace, - strlen, strnatcasecmp, strpos, substr, trim, usort, + implode, is_string, json_encode, php_regex, preg_is_match, preg_match, preg_quote, + preg_replace, str_replace, strlen, strnatcasecmp, strpos, substr, trim, usort, }; /// Seam over the parts of [`VersionGuesser`] that consumers depend on, so they can be exercised @@ -156,11 +156,10 @@ impl VersionGuesser { } if "-dev" == substr(version_data.version.as_deref().unwrap_or(""), -4, None) - && preg_match( + && preg_is_match( php_regex!(r"{\.9{7}}"), version_data.version.as_deref().unwrap_or(""), ) - .is_some() { version_data.pretty_version = Some(preg_replace( php_regex!(r"{(\.9{7})+}"), @@ -181,11 +180,10 @@ impl VersionGuesser { -4, None, ) - && preg_match( + && preg_is_match( php_regex!(r"{\.9{7}}"), version_data.feature_version.as_deref().unwrap_or(""), ) - .is_some() { version_data.feature_pretty_version = Some(preg_replace( php_regex!(r"{(\.9{7})+}"), @@ -257,7 +255,7 @@ impl VersionGuesser { } if !branch.is_empty() - && preg_match(php_regex!(r"{^ *.+/HEAD }"), &branch).is_none() + && !preg_is_match(php_regex!(r"{^ *.+/HEAD }"), &branch) && let Some(m) = preg_match( php_regex!( r"{^(?:\* )? *((?:remotes/(?:origin|upstream)/)?[^\s/]+) *([a-f0-9]+) .*$}" @@ -608,10 +606,13 @@ impl VersionGuesser { non_feature_branches = implode("|", &names); } - preg_match(format!( - r"{{^({}|master|main|latest|next|current|support|tip|trunk|default|develop|\d+\..+)$}}", - non_feature_branches, - ), branch_name.unwrap_or("")).is_none() + !preg_is_match( + format!( + r"{{^({}|master|main|latest|next|current|support|tip|trunk|default|develop|\d+\..+)$}}", + non_feature_branches, + ), + branch_name.unwrap_or(""), + ) } fn guess_fossil_version(&mut self, path: &str) -> anyhow::Result<VersionData> { diff --git a/crates/shirabe/src/package/version/version_parser.rs b/crates/shirabe/src/package/version/version_parser.rs index 6e7c5825..906e99f9 100644 --- a/crates/shirabe/src/package/version/version_parser.rs +++ b/crates/shirabe/src/package/version/version_parser.rs @@ -2,7 +2,7 @@ use crate::repository::PlatformRepository; use indexmap::IndexMap; -use shirabe_php_shim::{php_regex, preg_match, preg_replace}; +use shirabe_php_shim::{php_regex, preg_is_match, preg_replace}; use shirabe_semver::Semver; use shirabe_semver::VersionParser as SemverVersionParser; use shirabe_semver::constraint::AnyConstraint; @@ -57,11 +57,10 @@ impl VersionParser { if !pair.contains(' ') && i + 1 < count && !pairs[i + 1].contains('/') - && preg_match( + && !preg_is_match( php_regex!(r"{(?<=[a-z0-9_/-])\*|\*(?=[a-z0-9_/-])}i"), &pairs[i + 1], ) - .is_none() && !PlatformRepository::is_platform_package(&pairs[i + 1]) { pair += &format!(" {}", pairs[i + 1]); diff --git a/crates/shirabe/src/package/version/version_selector.rs b/crates/shirabe/src/package/version/version_selector.rs index 91929c40..61435870 100644 --- a/crates/shirabe/src/package/version/version_selector.rs +++ b/crates/shirabe/src/package/version/version_selector.rs @@ -16,7 +16,9 @@ use crate::repository::PlatformRepository; use crate::repository::RepositoryInterface; use crate::repository::RepositorySetInterface; use indexmap::IndexMap; -use shirabe_php_shim::{CmpOp, php_regex, preg_match, preg_replace, strtolower, version_compare}; +use shirabe_php_shim::{ + CmpOp, php_regex, preg_is_match, preg_replace, strtolower, version_compare, +}; use shirabe_semver::constraint::AnyConstraint; use shirabe_semver::constraint::SimpleConstraint; @@ -302,7 +304,7 @@ impl VersionSelector { let semantic_version_parts: Vec<&str> = version.split('.').collect(); if semantic_version_parts.len() == 4 - && preg_match(php_regex!(r"{^\d+\D?}"), semantic_version_parts[3]).is_some() + && preg_is_match(php_regex!(r"{^\d+\D?}"), semantic_version_parts[3]) { let mut parts: Vec<String> = semantic_version_parts .iter() |
