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/package/version | |
| 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/package/version')
4 files changed, 38 insertions, 36 deletions
diff --git a/crates/shirabe/src/package/version/version_bumper.rs b/crates/shirabe/src/package/version/version_bumper.rs index 6911fb14..50808916 100644 --- a/crates/shirabe/src/package/version/version_bumper.rs +++ b/crates/shirabe/src/package/version/version_bumper.rs @@ -5,8 +5,9 @@ use crate::package::dumper::ArrayDumper; use crate::package::loader::ArrayLoader; use crate::package::version::VersionParser; use crate::util::Platform; -use shirabe_pcre::{CaptureKey, Preg}; -use shirabe_php_shim::php_regex; +use shirabe_php_shim::{ + CaptureKey, php_regex, preg_match_all_offset_capture, preg_match2, preg_replace, +}; use shirabe_semver::Intervals; use shirabe_semver::constraint::AnyConstraint; @@ -45,12 +46,12 @@ impl VersionBumper { return Ok(pretty_constraint); } - let major = Preg::replace(php_regex!(r"{^([1-9][0-9]*|0\.\d+).*}"), "$1", &version); + let major = preg_replace(php_regex!(r"{^([1-9][0-9]*|0\.\d+).*}"), "$1", &version); let version_without_suffix = - Preg::replace(php_regex!(r"{(?:\.(?:0|9999999))+(-dev)?$}"), "", &version); + preg_replace(php_regex!(r"{(?:\.(?:0|9999999))+(-dev)?$}"), "", &version); let new_pretty_constraint = format!("^{}", version_without_suffix); - if !Preg::is_match(php_regex!(r"{^\^\d+(\.\d+)*$}"), &new_pretty_constraint) { + if preg_match2(php_regex!(r"{^\^\d+(\.\d+)*$}"), &new_pretty_constraint, 0).is_none() { return Ok(pretty_constraint); } @@ -77,7 +78,7 @@ impl VersionBumper { major = major ); - let matches = Preg::is_match_all_with_offsets3(&pattern, &pretty_constraint); + let matches = preg_match_all_offset_capture(&pattern, &pretty_constraint); if matches.occurrence_count() > 0 { let mut modified = pretty_constraint.clone(); let constraint_matches = matches diff --git a/crates/shirabe/src/package/version/version_guesser.rs b/crates/shirabe/src/package/version/version_guesser.rs index c85000e7..fd7fe389 100644 --- a/crates/shirabe/src/package/version/version_guesser.rs +++ b/crates/shirabe/src/package/version/version_guesser.rs @@ -12,11 +12,10 @@ use crate::util::ProcessExecutor; use crate::util::Svn as SvnUtil; use crate::util::sync_executor; use indexmap::IndexMap; -use shirabe_pcre::Preg; use shirabe_php_shim::{ PhpMixed, RuntimeException, array_keys, array_map, array_merge, empty, function_exists, - implode, is_string, json_encode, php_regex, preg_quote, str_replace, strlen, strnatcasecmp, - strpos, substr, trim, usort, + implode, is_string, json_encode, php_regex, preg_match2, 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 @@ -157,12 +156,14 @@ impl VersionGuesser { } if "-dev" == substr(version_data.version.as_deref().unwrap_or(""), -4, None) - && Preg::is_match( + && preg_match2( php_regex!(r"{\.9{7}}"), version_data.version.as_deref().unwrap_or(""), + 0, ) + .is_some() { - version_data.pretty_version = Some(Preg::replace( + version_data.pretty_version = Some(preg_replace( php_regex!(r"{(\.9{7})+}"), ".x", version_data.version.as_deref().unwrap_or(""), @@ -181,12 +182,14 @@ impl VersionGuesser { -4, None, ) - && Preg::is_match( + && preg_match2( php_regex!(r"{\.9{7}}"), version_data.feature_version.as_deref().unwrap_or(""), + 0, ) + .is_some() { - version_data.feature_pretty_version = Some(Preg::replace( + version_data.feature_pretty_version = Some(preg_replace( php_regex!(r"{(\.9{7})+}"), ".x", version_data.feature_version.as_deref().unwrap_or(""), @@ -229,11 +232,12 @@ 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::is_match3( + && let Some(m) = preg_match2( 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(); @@ -256,12 +260,13 @@ impl VersionGuesser { } if !branch.is_empty() - && Preg::is_match3(php_regex!(r"{^ *.+/HEAD }"), &branch).is_none() - && let Some(m) = Preg::is_match3( + && preg_match2(php_regex!(r"{^ *.+/HEAD }"), &branch, 0).is_none() + && let Some(m) = preg_match2( php_regex!( r"{^(?:\* )? *((?:remotes/(?:origin|upstream)/)?[^\s/]+) *([a-f0-9]+) .*$}" ), &branch, + 0, ) { branches.push(m.get(1).unwrap_or_default().to_string()); @@ -499,8 +504,7 @@ impl VersionGuesser { ) .is_some(); if !has_branch_alias || has_self_version { - let branch = - Preg::replace(php_regex!(r"{^dev-}"), "", version.as_deref().unwrap_or("")); + let branch = preg_replace(php_regex!(r"{^dev-}"), "", version.as_deref().unwrap_or("")); let mut length = i64::MAX; // return directly, if branch is configured to be non-feature branch @@ -534,7 +538,7 @@ impl VersionGuesser { for (index, candidate) in branches.iter().enumerate() { let index = index as i64; let candidate_version = - Preg::replace(php_regex!(r"{^remotes/\S+/}"), "", candidate); + preg_replace(php_regex!(r"{^remotes/\S+/}"), "", candidate); // do not compare against itself or other feature branches if candidate == &branch @@ -608,13 +612,10 @@ impl VersionGuesser { non_feature_branches = implode("|", &names); } - !Preg::is_match( - format!( - r"{{^({}|master|main|latest|next|current|support|tip|trunk|default|develop|\d+\..+)$}}", - non_feature_branches, - ), - branch_name.unwrap_or(""), - ) + preg_match2(format!( + r"{{^({}|master|main|latest|next|current|support|tip|trunk|default|develop|\d+\..+)$}}", + non_feature_branches, + ), branch_name.unwrap_or(""), 0).is_none() } fn guess_fossil_version(&mut self, path: &str) -> anyhow::Result<VersionData> { @@ -697,7 +698,7 @@ impl VersionGuesser { trunk_path, branches_path, tags_path, ); - if let Some(matches) = Preg::is_match3(&url_pattern, &output) { + if let Some(matches) = preg_match2(&url_pattern, &output, 0) { let m1 = matches.get(1).unwrap_or_default(); let m2 = matches.get(2); let m3 = matches.get(3); @@ -750,7 +751,7 @@ impl VersionGuesser { .into()); } }; - if let Some(m) = Preg::is_match3(php_regex!(r"{^(\d+(?:\.\d+)*)-dev$}i"), &version) { + if let Some(m) = preg_match2(php_regex!(r"{^(\d+(?:\.\d+)*)-dev$}i"), &version, 0) { 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 c16583e0..ad1a05cf 100644 --- a/crates/shirabe/src/package/version/version_parser.rs +++ b/crates/shirabe/src/package/version/version_parser.rs @@ -2,8 +2,7 @@ use crate::repository::PlatformRepository; use indexmap::IndexMap; -use shirabe_pcre::Preg; -use shirabe_php_shim::php_regex; +use shirabe_php_shim::{php_regex, preg_match2, preg_replace}; use shirabe_semver::Semver; use shirabe_semver::VersionParser as SemverVersionParser; use shirabe_semver::constraint::AnyConstraint; @@ -50,7 +49,7 @@ impl VersionParser { let count = pairs.len(); let mut i = 0_usize; while i < count { - let mut pair = Preg::replace( + let mut pair = preg_replace( php_regex!(r"{^([^=: ]+)[=: ](.*)$}"), "$1 $2", pairs[i].trim(), @@ -58,10 +57,12 @@ impl VersionParser { if !pair.contains(' ') && i + 1 < count && !pairs[i + 1].contains('/') - && !Preg::is_match( + && preg_match2( php_regex!(r"{(?<=[a-z0-9_/-])\*|\*(?=[a-z0-9_/-])}i"), &pairs[i + 1], + 0, ) + .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 1661c749..c7b7b31e 100644 --- a/crates/shirabe/src/package/version/version_selector.rs +++ b/crates/shirabe/src/package/version/version_selector.rs @@ -16,8 +16,7 @@ use crate::repository::PlatformRepository; use crate::repository::RepositoryInterface; use crate::repository::RepositorySetInterface; use indexmap::IndexMap; -use shirabe_pcre::Preg; -use shirabe_php_shim::{CmpOp, php_regex, strtolower, version_compare}; +use shirabe_php_shim::{CmpOp, php_regex, preg_match2, preg_replace, strtolower, version_compare}; use shirabe_semver::constraint::AnyConstraint; use shirabe_semver::constraint::SimpleConstraint; @@ -280,7 +279,7 @@ impl VersionSelector { if let Some(extra) = extra && extra != VersionParser::DEFAULT_BRANCH_ALIAS { - let new_extra = Preg::replace( + let new_extra = preg_replace( php_regex!(r"{^(\d+\.\d+\.\d+)(\.9999999)-dev$}"), "$1.0", &extra, @@ -303,7 +302,7 @@ impl VersionSelector { let semantic_version_parts: Vec<&str> = version.split('.').collect(); if semantic_version_parts.len() == 4 - && Preg::is_match(php_regex!(r"{^\d+\D?}"), semantic_version_parts[3]) + && preg_match2(php_regex!(r"{^\d+\D?}"), semantic_version_parts[3], 0).is_some() { let mut parts: Vec<String> = semantic_version_parts .iter() |
