aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/package/version/version_guesser.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-18 01:57:02 +0900
committernsfisis <nsfisis@gmail.com>2026-08-18 01:57:02 +0900
commit530d085d4f3e19f94ac3cf8f8ac3b17000214b2e (patch)
treeb4de2c2443e2bb2cfc692454ac284dc1d2313e59 /crates/shirabe/src/package/version/version_guesser.rs
parent0caac63bacefb9a1f62848636d47fca07f592bba (diff)
downloadphp-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/version_guesser.rs')
-rw-r--r--crates/shirabe/src/package/version/version_guesser.rs45
1 files changed, 23 insertions, 22 deletions
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()));
}