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 | e093b2be1c333e67c96aebb0a5291bea9ae3d6db (patch) | |
| tree | 0743fad689f419959c3a898605e21485087884fa /crates/shirabe/src/package | |
| parent | 050c56ef263d90d862ef565bc1762909110e02eb (diff) | |
| download | php-shirabe-e093b2be1c333e67c96aebb0a5291bea9ae3d6db.tar.gz php-shirabe-e093b2be1c333e67c96aebb0a5291bea9ae3d6db.tar.zst php-shirabe-e093b2be1c333e67c96aebb0a5291bea9ae3d6db.zip | |
refactor(preg): drop the offset argument from preg_match
Every call site but one passed offset 0. The remaining one, the UTF-8
chunking loop in Application, slices the subject instead: its pattern has
no anchor or lookaround, so matching a suffix is equivalent to starting
the search at that offset.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/package')
11 files changed, 43 insertions, 56 deletions
diff --git a/crates/shirabe/src/package/archiver/archive_manager.rs b/crates/shirabe/src/package/archiver/archive_manager.rs index 4f9e2ddc..83d5cc94 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_match2, + InvalidArgumentException, RuntimeException, bin2hex, file_exists, php_regex, preg_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_match2(php_regex!("{^[a-f0-9]{40}$}"), dist_ref, 0).is_some() { + if preg_match(php_regex!("{^[a-f0-9]{40}$}"), dist_ref).is_some() { 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 16a189a8..8186e95c 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_match2; +use shirabe_php_shim::preg_match; use shirabe_symfony_finder::Glob; #[derive(Debug)] @@ -86,7 +86,7 @@ pub trait BaseExcludeFilter { relative_path }; - if preg_match2(pattern, path, 0).is_some() { + if preg_match(pattern, path).is_some() { exclude = !negate; } } diff --git a/crates/shirabe/src/package/loader/array_loader.rs b/crates/shirabe/src/package/loader/array_loader.rs index b6b5ac1e..00f9026e 100644 --- a/crates/shirabe/src/package/loader/array_loader.rs +++ b/crates/shirabe/src/package/loader/array_loader.rs @@ -19,7 +19,7 @@ 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_match2, preg_replace, stripos, strpos, strtolower, strval, + json_encode, ltrim, php_regex, preg_match, preg_replace, stripos, strpos, strtolower, strval, substr, trigger_error, trim, }; @@ -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_match2(php_regex!(r"/^\d++$/D"), time_str, 0).is_some() { + let time = if preg_match(php_regex!(r"/^\d++$/D"), time_str).is_some() { 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 bc49ffc0..491961a6 100644 --- a/crates/shirabe/src/package/loader/root_package_loader.rs +++ b/crates/shirabe/src/package/loader/root_package_loader.rs @@ -16,7 +16,7 @@ use crate::util::Platform; use crate::util::ProcessExecutor; use indexmap::IndexMap; use shirabe_php_shim::{ - PhpMixed, RuntimeException, UnexpectedValueException, php_regex, preg_match2, preg_replace, + PhpMixed, RuntimeException, UnexpectedValueException, php_regex, preg_match, preg_replace, preg_split, strtolower, }; @@ -252,10 +252,9 @@ impl RootPackageLoader { mut aliases: Vec<IndexMap<String, String>>, ) -> Vec<IndexMap<String, String>> { for (req_name, req_version) in requires { - if let Some(m) = preg_match2( + if let Some(m) = preg_match( php_regex!(r"{(?:^|\| *|, *)([^,\s#|]+)(?:#[^ ]+)? +as +([^,\s|]+)(?:$| *\|| *,)}"), req_version, - 0, ) { let m1 = m.get(1).unwrap_or_default().to_string(); let m2 = m.get(2).unwrap_or_default().to_string(); @@ -317,7 +316,7 @@ impl RootPackageLoader { let mut matched = false; for constraint in &constraints { - if let Some(m) = preg_match2(&pattern, constraint, 0) { + if let Some(m) = preg_match(&pattern, constraint) { let name = strtolower(req_name); let m1 = m.get(1).unwrap_or_default().to_string(); let normalized_m1 = VersionParser::normalize_stability(&m1).unwrap_or_default(); @@ -338,7 +337,7 @@ impl RootPackageLoader { for constraint in &constraints { let req_version_stripped = preg_replace(php_regex!(r"{^([^,\s@]+) as .+$}"), "$1", constraint); - if preg_match2(php_regex!(r"{^[^,\s@]+$}"), &req_version_stripped, 0).is_some() { + if preg_match(php_regex!(r"{^[^,\s@]+$}"), &req_version_stripped).is_some() { let stability_name = VersionParser::parse_stability(&req_version_stripped); if stability_name != "stable" { let name = strtolower(req_name); @@ -363,7 +362,7 @@ impl RootPackageLoader { ) -> IndexMap<String, String> { for (req_name, req_version) in requires { let req_version = preg_replace(php_regex!(r"{^([^,\s@]+) as .+$}"), "$1", req_version); - if let Some(m) = preg_match2(php_regex!(r"{^[^,\s@]+?#([a-f0-9]+)$}"), &req_version, 0) + if let Some(m) = preg_match(php_regex!(r"{^[^,\s@]+?#([a-f0-9]+)$}"), &req_version) && VersionParser::parse_stability(&req_version) == "dev" { 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 5ac1a929..e893756a 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_match2, preg_replace, str_replace, + json_encode, parse_url, php_regex, php_to_string, preg_match, preg_replace, str_replace, strcasecmp, strtolower, strtotime, substr, trigger_error, trim, var_export, }; use shirabe_semver::Intervals; @@ -73,12 +73,11 @@ impl ValidatingArrayLoader { return None; } - if preg_match2( + if preg_match( php_regex!( "{^[a-z0-9](?:[_.-]?[a-z0-9]++)*+/[a-z0-9](?:(?:[_.]|-{1,2})?[a-z0-9]++)*+$}iD" ), name, - 0, ) .is_none() { @@ -102,14 +101,14 @@ impl ValidatingArrayLoader { )); } - if preg_match2(php_regex!("{\\.json$}"), name, 0).is_some() { + if preg_match(php_regex!("{\\.json$}"), name).is_some() { 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_match2(php_regex!("{[A-Z]}"), name, 0).is_some() { + if preg_match(php_regex!("{[A-Z]}"), name).is_some() { if is_link { return Some(format!( "{} is invalid, it should not contain uppercase characters. Please use {} instead.", @@ -143,7 +142,7 @@ impl ValidatingArrayLoader { .as_string() .unwrap_or("") .to_string(); - if preg_match2(format!("{{^{}$}}u", regex), &value, 0).is_none() { + if preg_match(format!("{{^{}$}}u", regex), &value).is_none() { let message = format!( "{} : invalid value ({}), must match {}", property, value, regex @@ -258,7 +257,7 @@ impl ValidatingArrayLoader { if let Some(regex_str) = regex { let value_str = php_to_string(&value); - if preg_match2(format!("{{^{}$}}u", regex_str), &value_str, 0).is_none() { + if preg_match(format!("{{^{}$}}u", regex_str), &value_str).is_none() { self.warnings.borrow_mut().push(format!( "{}.{} : invalid value ({}), must match {}", property, key, value_str, regex_str @@ -1187,8 +1186,7 @@ impl LoaderInterface for ValidatingArrayLoader { self.warnings .borrow_mut() .push(format!("{}.{}", link_type, err)); - } else if preg_match2(php_regex!("{^[A-Za-z0-9_./-]+$}"), &package, 0).is_none() - { + } else if preg_match(php_regex!("{^[A-Za-z0-9_./-]+$}"), &package).is_none() { self.errors.borrow_mut().push(format!( "{}.{} : invalid key, package names must be strings containing only [A-Za-z0-9_./-]", link_type, package @@ -1451,7 +1449,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_match2(php_regex!("{^\\s*-}"), &ref_str, 0).is_some() { + if preg_match(php_regex!("{^\\s*-}"), &ref_str).is_some() { self.errors.borrow_mut().push(format!( "{}.reference : must not start with a \"-\", \"{}\" given", src_type, ref_str @@ -1460,7 +1458,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_match2(php_regex!("{^\\s*-}"), &url_str, 0).is_some() { + if preg_match(php_regex!("{^\\s*-}"), &url_str).is_some() { 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 fee45fef..017bcb6a 100644 --- a/crates/shirabe/src/package/locker.rs +++ b/crates/shirabe/src/package/locker.rs @@ -28,7 +28,7 @@ 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_match2, realpath, strcmp, strtolower, touch2, trim, usort, + is_int, ksort, php_regex, preg_match, realpath, strcmp, strtolower, touch2, trim, usort, }; use shirabe_seld_json_lint::ParsingException; @@ -823,7 +823,7 @@ impl Locker { ), None, ); - if preg_match2(php_regex!(r"{^\s*\d+\s*$}"), &output_str, 0).is_some() { + if preg_match(php_regex!(r"{^\s*\d+\s*$}"), &output_str).is_some() { let ts = trim(&output_str, None).parse::<i64>().unwrap_or(0); datetime = chrono::DateTime::from_timestamp(ts, 0); } @@ -842,10 +842,9 @@ impl Locker { ]), &mut output, path.as_deref(), - )? && let Some(m) = preg_match2( + )? && let Some(m) = preg_match( php_regex!(r"{^\s*(\d+)\s*}"), output.as_string().unwrap_or(""), - 0, ) { let ts = m .get(1) diff --git a/crates/shirabe/src/package/package.rs b/crates/shirabe/src/package/package.rs index ddeb2932..cbc027ba 100644 --- a/crates/shirabe/src/package/package.rs +++ b/crates/shirabe/src/package/package.rs @@ -11,7 +11,7 @@ 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_match2, preg_replace, + E_USER_DEPRECATED, LogicException, PhpMixed, PregMatches, php_regex, preg_match, preg_replace, preg_replace_callback, strpos, trigger_error, }; @@ -416,13 +416,9 @@ 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_match2( - php_regex!( - "{^https?://(?:(?:www\\.)?bitbucket\\.org|(api\\.)?github\\.com|(?:www\\.)?gitlab\\.com)/}i" - ), - &self.get_dist_url().unwrap_or_default(), - 0, - ) + && preg_match(php_regex!( + "{^https?://(?:(?:www\\.)?bitbucket\\.org|(api\\.)?github\\.com|(?:www\\.)?gitlab\\.com)/}i" + ), &self.get_dist_url().unwrap_or_default()) .is_some() { self.set_dist_reference(Some(reference.clone())); diff --git a/crates/shirabe/src/package/version/version_bumper.rs b/crates/shirabe/src/package/version/version_bumper.rs index 50808916..e75dea33 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_all_offset_capture, preg_match2, preg_replace, + CaptureKey, php_regex, preg_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_match2(php_regex!(r"{^\^\d+(\.\d+)*$}"), &new_pretty_constraint, 0).is_none() { + if preg_match(php_regex!(r"{^\^\d+(\.\d+)*$}"), &new_pretty_constraint).is_none() { 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 fd7fe389..668fce02 100644 --- a/crates/shirabe/src/package/version/version_guesser.rs +++ b/crates/shirabe/src/package/version/version_guesser.rs @@ -14,7 +14,7 @@ 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_match2, preg_quote, preg_replace, str_replace, + implode, is_string, json_encode, php_regex, preg_match, preg_quote, preg_replace, str_replace, strlen, strnatcasecmp, strpos, substr, trim, usort, }; @@ -156,10 +156,9 @@ impl VersionGuesser { } if "-dev" == substr(version_data.version.as_deref().unwrap_or(""), -4, None) - && preg_match2( + && preg_match( php_regex!(r"{\.9{7}}"), version_data.version.as_deref().unwrap_or(""), - 0, ) .is_some() { @@ -182,10 +181,9 @@ impl VersionGuesser { -4, None, ) - && preg_match2( + && preg_match( php_regex!(r"{\.9{7}}"), version_data.feature_version.as_deref().unwrap_or(""), - 0, ) .is_some() { @@ -232,12 +230,11 @@ impl VersionGuesser { // find current branch and collect all branch names for branch in self.process.borrow().split_lines(&output) { if !branch.is_empty() - && let Some(m) = preg_match2( + && let Some(m) = preg_match( php_regex!( r"{^(?:\* ) *(\(no branch\)|\(detached from \S+\)|\(HEAD detached at \S+\)|\S+) *([a-f0-9]+) .*$}" ), &branch, - 0, ) { let g1 = m.get(1).unwrap_or_default().to_string(); @@ -260,13 +257,12 @@ impl VersionGuesser { } if !branch.is_empty() - && preg_match2(php_regex!(r"{^ *.+/HEAD }"), &branch, 0).is_none() - && let Some(m) = preg_match2( + && preg_match(php_regex!(r"{^ *.+/HEAD }"), &branch).is_none() + && let Some(m) = preg_match( php_regex!( r"{^(?:\* )? *((?:remotes/(?:origin|upstream)/)?[^\s/]+) *([a-f0-9]+) .*$}" ), &branch, - 0, ) { branches.push(m.get(1).unwrap_or_default().to_string()); @@ -612,10 +608,10 @@ impl VersionGuesser { non_feature_branches = implode("|", &names); } - preg_match2(format!( + preg_match(format!( r"{{^({}|master|main|latest|next|current|support|tip|trunk|default|develop|\d+\..+)$}}", non_feature_branches, - ), branch_name.unwrap_or(""), 0).is_none() + ), branch_name.unwrap_or("")).is_none() } fn guess_fossil_version(&mut self, path: &str) -> anyhow::Result<VersionData> { @@ -698,7 +694,7 @@ impl VersionGuesser { trunk_path, branches_path, tags_path, ); - if let Some(matches) = preg_match2(&url_pattern, &output, 0) { + if let Some(matches) = preg_match(&url_pattern, &output) { let m1 = matches.get(1).unwrap_or_default(); let m2 = matches.get(2); let m3 = matches.get(3); @@ -751,7 +747,7 @@ impl VersionGuesser { .into()); } }; - if let Some(m) = preg_match2(php_regex!(r"{^(\d+(?:\.\d+)*)-dev$}i"), &version, 0) { + if let Some(m) = preg_match(php_regex!(r"{^(\d+(?:\.\d+)*)-dev$}i"), &version) { return Ok(format!("{}.x-dev", m.get(1).unwrap_or_default())); } diff --git a/crates/shirabe/src/package/version/version_parser.rs b/crates/shirabe/src/package/version/version_parser.rs index ad1a05cf..6e7c5825 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_match2, preg_replace}; +use shirabe_php_shim::{php_regex, preg_match, preg_replace}; use shirabe_semver::Semver; use shirabe_semver::VersionParser as SemverVersionParser; use shirabe_semver::constraint::AnyConstraint; @@ -57,10 +57,9 @@ impl VersionParser { if !pair.contains(' ') && i + 1 < count && !pairs[i + 1].contains('/') - && preg_match2( + && preg_match( php_regex!(r"{(?<=[a-z0-9_/-])\*|\*(?=[a-z0-9_/-])}i"), &pairs[i + 1], - 0, ) .is_none() && !PlatformRepository::is_platform_package(&pairs[i + 1]) diff --git a/crates/shirabe/src/package/version/version_selector.rs b/crates/shirabe/src/package/version/version_selector.rs index c7b7b31e..91929c40 100644 --- a/crates/shirabe/src/package/version/version_selector.rs +++ b/crates/shirabe/src/package/version/version_selector.rs @@ -16,7 +16,7 @@ use crate::repository::PlatformRepository; use crate::repository::RepositoryInterface; use crate::repository::RepositorySetInterface; use indexmap::IndexMap; -use shirabe_php_shim::{CmpOp, php_regex, preg_match2, preg_replace, strtolower, version_compare}; +use shirabe_php_shim::{CmpOp, php_regex, preg_match, preg_replace, strtolower, version_compare}; use shirabe_semver::constraint::AnyConstraint; use shirabe_semver::constraint::SimpleConstraint; @@ -302,7 +302,7 @@ impl VersionSelector { let semantic_version_parts: Vec<&str> = version.split('.').collect(); if semantic_version_parts.len() == 4 - && preg_match2(php_regex!(r"{^\d+\D?}"), semantic_version_parts[3], 0).is_some() + && preg_match(php_regex!(r"{^\d+\D?}"), semantic_version_parts[3]).is_some() { let mut parts: Vec<String> = semantic_version_parts .iter() |
